diff --git a/python/ycm/tests/vimsupport_test.py b/python/ycm/tests/vimsupport_test.py index 4387ee94c0..ca44a5db48 100644 --- a/python/ycm/tests/vimsupport_test.py +++ b/python/ycm/tests/vimsupport_test.py @@ -667,6 +667,16 @@ def test_ReplaceChunk_BeyondEndOfFile( self ): AssertBuffersAreEqualAsBytes( [ 'first line' ], result_buffer ) + @patch( 'vim.current.window.cursor', ( 1, 1 ) ) + def test_ReplaceChunk_AtOneLinePastEndOfFile( self ): + result_buffer = VimBuffer( 'buffer', contents = [ 'first line' ] ) + start, end = _BuildLocations( 2, 1, 2, 1 ) + vimsupport.ReplaceChunk( start, end, 'second line', result_buffer ) + + AssertBuffersAreEqualAsBytes( [ 'first line', 'second line' ], + result_buffer ) + + @patch( 'vim.current.window.cursor', ( 1, 3 ) ) def test_ReplaceChunk_CursorPosition( self ): result_buffer = VimBuffer( 'buffer', contents = [ 'bar' ] ) diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 2a29b1ede1..6b9042b3de 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -1140,6 +1140,10 @@ def ReplaceChunk( start, end, replacement_text, vim_buffer ): # so we convert to bytes replacement_lines = SplitLines( ToBytes( replacement_text ) ) + if start_line >= len( vim_buffer ): + start_line -= 1 + replacement_lines = [ ToBytes( vim_buffer[ -1 ] ) ] + replacement_lines + # NOTE: Vim buffers are a list of unicode objects on Python 3. start_existing_text = ToBytes( vim_buffer[ start_line ] )[ : start_column ] end_line_text = ToBytes( vim_buffer[ end_line ] )