diff --git a/python/ycm/tests/vimsupport_test.py b/python/ycm/tests/vimsupport_test.py index d7a7d468..b4367f8b 100644 --- a/python/ycm/tests/vimsupport_test.py +++ b/python/ycm/tests/vimsupport_test.py @@ -1537,28 +1537,41 @@ def SelectFromList_Negative_test( vim_eval ): @patch( 'ycm.vimsupport.SearchInCurrentBuffer', return_value = 0 ) @patch( 'vim.current' ) def InsertNamespace_insert_test( vim_current, *args ): - contents = [ 'var taco = Math' ] + contents = [ '', + 'namespace Taqueria {', + '', + ' int taco = Math' ] vim_current.buffer = VimBuffer( '', contents = contents ) vimsupport.InsertNamespace( 'System' ) expected_buffer = [ 'using System;', - 'var taco = Math' ] + '', + 'namespace Taqueria {', + '', + ' int taco = Math' ] AssertBuffersAreEqualAsBytes( expected_buffer, vim_current.buffer ) @patch( 'ycm.vimsupport.VariableExists', return_value = False ) -@patch( 'ycm.vimsupport.SearchInCurrentBuffer', return_value = 1 ) +@patch( 'ycm.vimsupport.SearchInCurrentBuffer', return_value = 2 ) @patch( 'vim.current' ) def InsertNamespace_append_test( vim_current, *args ): - contents = [ 'using System;', '', 'var taco;', 'var salad = new List'] + contents = [ 'namespace Taqueria {', + ' using System;', + '', + ' class Tasty {', + ' int taco;', + ' List salad = new List'] vim_current.buffer = VimBuffer( '', contents = contents ) vimsupport.InsertNamespace( 'System.Collections' ) - expected_buffer = [ 'using System;', - 'using System.Collections;', + expected_buffer = [ 'namespace Taqueria {', + ' using System;', + ' using System.Collections;', '', - 'var taco;', - 'var salad = new List' ] + ' class Tasty {', + ' int taco;', + ' List salad = new List'] AssertBuffersAreEqualAsBytes( expected_buffer, vim_current.buffer ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 650ad1b3..30f9a452 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -846,9 +846,11 @@ def InsertNamespace( namespace ): return pattern = '^\s*using\(\s\+[a-zA-Z0-9]\+\s\+=\)\?\s\+[a-zA-Z0-9.]\+\s*;\s*' + existing_indent = '' line = SearchInCurrentBuffer( pattern ) - existing_line = LineTextInCurrentBuffer( line ) - existing_indent = re.sub( r"\S.*", "", existing_line ) + if line > 0: + existing_line = LineTextInCurrentBuffer( line ) + existing_indent = re.sub( r"\S.*", "", existing_line ) new_line = "{0}using {1};\n\n".format( existing_indent, namespace ) replace_pos = { 'line_num': line + 1, 'column_num': 1 } ReplaceChunk( replace_pos, replace_pos, new_line, 0, 0, vim.current.buffer ) @@ -856,11 +858,14 @@ def InsertNamespace( namespace ): def SearchInCurrentBuffer( pattern ): + """ Returns the 1-indexed line on which the pattern matches + (going UP from the current position) or 0 if not found """ return GetIntValue( "search('{0}', 'Wcnb')".format( EscapeForVim( pattern ))) def LineTextInCurrentBuffer( line ): - return vim.current.buffer[ line ] + """ Returns the text on the 1-indexed line (NOT 0-indexed) """ + return vim.current.buffer[ line - 1 ] def ClosePreviewWindow():