Skip to content

Commit 619f348

Browse files
committed
Infer end position for Pylint diagnostics
Pylint only supplies the start position of markers, but not the end. Always extending the marker until the end of the line leads to undesirable highlighting in editors, for example: ```python def my_func(unused_arg, used_arg): print(used_arg) ``` Starting an "unused argument" diagnostic at `unused_arg` and extending It until EOL would also highlight `used_arg`, even though the diagnostic doesn't apply there. With this commit, the end position sent to the editor is automatically determined by looking for an identifier at the indicated start position. If there is no valid identifier at that position, the entire line is highlighted as before.
1 parent 4c0e99b commit 619f348

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pylsp/plugins/pylint_lint.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ def lint(cls, document, is_saved, flags=""):
151151
"line": line,
152152
# It's possible that we're linting an empty file. Even an empty
153153
# file might fail linting if it isn't named properly.
154-
"character": len(document.lines[line]) if document.lines else 0,
154+
"character": (
155+
_find_end_of_identifier(document.lines[line], diag["column"])
156+
if document.lines
157+
else 0
158+
),
155159
},
156160
}
157161

@@ -338,8 +342,9 @@ def _parse_pylint_stdio_result(document, stdout):
338342
"start": {"line": line, "character": character},
339343
"end": {
340344
"line": line,
341-
# no way to determine the column
342-
"character": len(document.lines[line]) - 1,
345+
"character": _find_end_of_identifier(
346+
document.lines[line], character
347+
),
343348
},
344349
},
345350
"message": msg,
@@ -352,3 +357,11 @@ def _parse_pylint_stdio_result(document, stdout):
352357
diagnostics.append(diagnostic)
353358

354359
return diagnostics
360+
361+
362+
def _find_end_of_identifier(string, start):
363+
"""Find the end of the identifier starting at the given position."""
364+
for i in range(len(string), start, -1):
365+
if string[start:i].isidentifier():
366+
return i
367+
return len(string) - 1

0 commit comments

Comments
 (0)