vim-cpp-auto-include icon indicating copy to clipboard operation
vim-cpp-auto-include copied to clipboard

Detect usage of std:: and avoid using namespace

Open m42e opened this issue 9 years ago • 3 comments

E.g. goole code style tells you not to do so.

m42e avatar Dec 17 '15 20:12 m42e

To be honest, the current change makes the code harder to read. $1 is discouraged in Ruby and names like regexp and rex confuses people.

Let's do this instead: If std:: is found, do not add using namespace std;

diff --git a/cpp_auto_include.vim b/cpp_auto_include.vim
index 6bcfa34..e593250 100644
--- a/cpp_auto_include.vim
+++ b/cpp_auto_include.vim
@@ -111,6 +111,7 @@ module CppAutoInclude
   ]

   USING_STD       = 'using namespace std;'
+  USING_STD_REGEX = /using namespace std;|std::/

   # do nothing if lines.count > LINES_THRESHOLD
   LINES_THRESHOLD = 1000
@@ -164,7 +165,7 @@ module CppAutoInclude
         end

         # add / remove 'using namespace std'
-        has_std = content[USING_STD]
+        has_std = content[USING_STD_REGEX]

         if use_std && !has_std && !includes.empty?
           VIM::append(includes.last.last+1, USING_STD) 

quark-zju avatar Dec 17 '15 21:12 quark-zju

Ok, i haven't thought about that Solution. Well to be honest I haven't really taken care of the variable names. Thanks.

Am 17.12.2015 um 22:31 schrieb Jun Wu <[email protected] mailto:[email protected] >:

To be honest, the current change make the code hard to read. $1 is discouraged in Ruby and names like regexp and rex confuses people.

Let's do this instead: If std:: is found, do not add using namespace std;

diff --git a/cpp_auto_include.vim b/cpp_auto_include.vim index 6bcfa34..e593250 100644 --- a/cpp_auto_include.vim +++ b/cpp_auto_include.vim @@ -111,6 +111,7 @@ module CppAutoInclude ]

USING_STD = 'using namespace std;'

  • USING_STD_REGEX = /using namespace std;|std::/

do nothing if lines.count > LINES_THRESHOLD

LINES_THRESHOLD = 1000 @@ -164,7 +165,7 @@ module CppAutoInclude end

 # add / remove 'using namespace std'
  •    has_std = content[USING_STD]
    
  •    has_std = content[USING_STD_REGEX]
    
     if use_std && !has_std && !includes.empty?
       VIM::append(includes.last.last+1, USING_STD) 
    

— Reply to this email directly or view it on GitHub.

m42e avatar Dec 17 '15 21:12 m42e

Fixed it

m42e avatar Dec 21 '15 09:12 m42e