8000 Fix character normalization (#216) · python-lsp/python-lsp-server@4ae91dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ae91dc

Browse files
lgeigergatesn
authored andcommitted
Fix character normalization (#216)
* Fix character normalization If a request is sent from an empty document the language server will throw a exception because `self.lines == []` and therefor `self.lines[0]` throws `IndexError: list index out of range`. * Add test for column clipping
1 parent 7a1eca1 commit 4ae91dc

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

pyls/_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,10 @@ def format_docstring(contents):
126126
contents = contents.replace(' ', u'\u00A0' * 2)
127127
contents = contents.replace('*', '\\*')
128128
return contents
129+
130+
131+
def clip_column(column, lines, line_number):
132+
# Normalise the position as per the LSP that accepts character positions > line length
133+
# https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#position
134+
max_column = len(lines[line_number]) - 1 if len(lines) > line_number else 0
135+
return min(column, max_column)

pyls/workspace.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,5 @@ def jedi_script(self, position=None):
264264
}
265265
if position:
266266
kwargs['line'] = position['line'] + 1
267-
268-
# Normalise the position as per the LSP that accepts character positions > line length
269-
# https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#position
270-
line_len = len(self.lines[position['line']])
271-
kwargs['column'] = min(position['character'], line_len - 1)
267+
kwargs['column'] = _utils.clip_column(position['character'], self.lines, position['line'])
272268
return jedi.Script(**kwargs)

test/test_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ def test_merge_dicts():
4949
{'a': True, 'b': {'x': 123, 'y': {'hello': 'world'}}},
5050
{'a': False, 'b': {'y': [], 'z': 987}}
5151
) == {'a': False, 'b': {'x': 123, 'y': [], 'z': 987}}
52+
53+
54+
def test_clip_column():
55+
assert _utils.clip_column(5, ['123'], 0) == 2
56+
assert _utils.clip_column(2, ['\n', '123'], 1) == 2
57+
assert _utils.clip_column(0, [], 0) == 0

0 commit comments

Comments
 (0)
0