Skip to content

Commit

Permalink
Auto merge of #2956 - micbou:fix-column-clamping-python3, r=puremourning
Browse files Browse the repository at this point in the history
[READY] Fix column clamping when highlighting diagnostics on Python 3

We need to convert the Vim buffer to bytes in the `LineAndColumnNumbersClamped` function to correctly compute the length of the line as a byte offset (and not a Unicode offset) on Python 3.

Before:

![column-clamping-before](https://user-images.githubusercontent.com/10026824/37876087-01f38dde-3048-11e8-93aa-0ce284b09b55.png)
After:

![column-clamping-after](https://user-images.githubusercontent.com/10026824/37876089-056db49e-3048-11e8-9d84-540fe67e6689.png)

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/valloric/youcompleteme/2956)
<!-- Reviewable:end -->
  • Loading branch information
zzbot authored Mar 25, 2018
2 parents 788c293 + ca8211a commit 37965fe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions python/ycm/tests/vimsupport_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,19 @@ def AddDiagnosticSyntaxMatch_WarningAtEndOfLine_test():
)


def AddDiagnosticSyntaxMatch_UnicodeAtEndOfLine_test():
current_buffer = VimBuffer(
'some_file',
contents = [ 'Highlight unicøde' ]
)

with patch( 'vim.current.buffer', current_buffer ):
assert_that(
vimsupport.GetDiagnosticMatchPattern( 1, 16, 1, 19 ),
equal_to( '\%1l\%16c\_.\{-}\%1l\%19c' )
)


@patch( 'vim.command', new_callable=ExtendedMock )
@patch( 'vim.current', new_callable=ExtendedMock)
def WriteToPreviewWindow_test( vim_current, vim_command ):
Expand Down
4 changes: 3 additions & 1 deletion python/ycm/vimsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ def LineAndColumnNumbersClamped( line_num, column_num ):
if line_num and line_num > max_line:
new_line_num = max_line

max_column = len( vim.current.buffer[ new_line_num - 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

Expand Down

0 comments on commit 37965fe

Please sign in to comment.