Merge pull request #3380 from micbou/diagnostic-match-positive-position

[READY] Ensure positive position for diagnostic matches
This commit is contained in:
mergify[bot] 2019-05-08 21:40:51 +00:00 committed by GitHub
commit f7e0a6cb75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.current', new_callable=ExtendedMock )
def WriteToPreviewWindow_test( vim_current, vim_command ):

View File

@ -275,14 +275,16 @@ def GetDiagnosticMatchPattern( line_num,
line_end_num = None,
column_end_num = None ):
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 )
# -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 - 1 )
column_end_num += 1
column_end_num = max( column_end_num + 1, 1 )
return '\\%{}l\\%{}c\\_.\\{{-}}\\%{}l\\%{}c'.format( line_num,
column_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
# the buffer. Numbers are 1-based byte offsets.
def LineAndColumnNumbersClamped( line_num, column_num ):
new_line_num = line_num
new_column_num = column_num
max_line = len( vim.current.buffer )
if line_num and line_num > max_line:
new_line_num = max_line
line_num = max( min( line_num, len( vim.current.buffer ) ), 1 )
# Vim buffers are a list of byte objects on Python 2 but Unicode objects on
# Python 3.
max_column = len( ToBytes( vim.current.buffer[ new_line_num - 1 ] ) )
if column_num and column_num > max_column:
new_column_num = max_column
max_column = len( ToBytes( vim.current.buffer[ line_num - 1 ] ) )
return new_line_num, new_column_num
return line_num, min( column_num, max_column )
def SetLocationList( diagnostics ):