Ensure positive position for diagnostic matches

This commit is contained in:
micbou 2019-04-25 02:55:34 +02:00
parent 944e7f5383
commit 168c0a334e
No known key found for this signature in database
GPG Key ID: C7E8FD1F3BDA1E05
2 changed files with 25 additions and 12 deletions

View File

@ -1373,6 +1373,24 @@ def AddDiagnosticSyntaxMatch_UnicodeAtEndOfLine_test():
) )
def AddDiagnosticSyntaxMatch_NonPositivePosition_test():
current_buffer = VimBuffer(
'some_file',
contents = [ 'Some contents' ]
)
with patch( 'vim.current.buffer', current_buffer ):
assert_that(
vimsupport.GetDiagnosticMatchPattern( 0, 0, 0, 0 ),
equal_to( '\\%1l\\%1c\\_.\\{-}\\%1l\\%1c' )
)
assert_that(
vimsupport.GetDiagnosticMatchPattern( -1, -2, -3, -4 ),
equal_to( '\\%1l\\%1c\\_.\\{-}\\%1l\\%1c' )
)
@patch( 'vim.command', new_callable=ExtendedMock ) @patch( 'vim.command', new_callable=ExtendedMock )
@patch( 'vim.current', new_callable=ExtendedMock ) @patch( 'vim.current', new_callable=ExtendedMock )
def WriteToPreviewWindow_test( vim_current, vim_command ): def WriteToPreviewWindow_test( vim_current, vim_command ):

View File

@ -275,14 +275,16 @@ def GetDiagnosticMatchPattern( line_num,
line_end_num = None, line_end_num = None,
column_end_num = None ): column_end_num = None ):
line_num, column_num = LineAndColumnNumbersClamped( line_num, column_num ) line_num, column_num = LineAndColumnNumbersClamped( line_num, column_num )
column_num = max( column_num, 1 )
if not line_end_num or not column_end_num: if line_end_num is None or column_end_num is None:
return '\\%{}l\\%{}c'.format( line_num, column_num ) return '\\%{}l\\%{}c'.format( line_num, column_num )
# -1 and then +1 to account for column end not included in the range. # -1 and then +1 to account for column end not included in the range.
line_end_num, column_end_num = LineAndColumnNumbersClamped( line_end_num, column_end_num = LineAndColumnNumbersClamped(
line_end_num, column_end_num - 1 ) line_end_num, column_end_num - 1 )
column_end_num += 1 column_end_num = max( column_end_num + 1, 1 )
return '\\%{}l\\%{}c\\_.\\{{-}}\\%{}l\\%{}c'.format( line_num, return '\\%{}l\\%{}c\\_.\\{{-}}\\%{}l\\%{}c'.format( line_num,
column_num, column_num,
line_end_num, line_end_num,
@ -292,20 +294,13 @@ def GetDiagnosticMatchPattern( line_num,
# Clamps the line and column numbers so that they are not past the contents of # Clamps the line and column numbers so that they are not past the contents of
# the buffer. Numbers are 1-based byte offsets. # the buffer. Numbers are 1-based byte offsets.
def LineAndColumnNumbersClamped( line_num, column_num ): def LineAndColumnNumbersClamped( line_num, column_num ):
new_line_num = line_num line_num = max( min( line_num, len( vim.current.buffer ) ), 1 )
new_column_num = column_num
max_line = len( vim.current.buffer )
if line_num and line_num > max_line:
new_line_num = max_line
# Vim buffers are a list of byte objects on Python 2 but Unicode objects on # Vim buffers are a list of byte objects on Python 2 but Unicode objects on
# Python 3. # Python 3.
max_column = len( ToBytes( vim.current.buffer[ new_line_num - 1 ] ) ) max_column = len( ToBytes( vim.current.buffer[ line_num - 1 ] ) )
if column_num and column_num > max_column:
new_column_num = max_column
return new_line_num, new_column_num return line_num, min( column_num, max_column )
def SetLocationList( diagnostics ): def SetLocationList( diagnostics ):