瀏覽代碼

Ensure positive position for diagnostic matches

micbou 6 年之前
父節點
當前提交
168c0a334e
共有 2 個文件被更改,包括 25 次插入12 次删除
  1. 18 0
      python/ycm/tests/vimsupport_test.py
  2. 7 12
      python/ycm/vimsupport.py

+ 18 - 0
python/ycm/tests/vimsupport_test.py

@@ -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 ):

+ 7 - 12
python/ycm/vimsupport.py

@@ -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 ):