diff --git a/pylsp/plugins/pylint_lint.py b/pylsp/plugins/pylint_lint.py index beffe6f3..722e831b 100644 --- a/pylsp/plugins/pylint_lint.py +++ b/pylsp/plugins/pylint_lint.py @@ -151,7 +151,11 @@ def lint(cls, document, is_saved, flags=""): "line": line, # It's possible that we're linting an empty file. Even an empty # file might fail linting if it isn't named properly. - "character": len(document.lines[line]) if document.lines else 0, + "character": ( + _find_end_of_identifier(document.lines[line], diag["column"]) + if document.lines + else 0 + ), }, } @@ -338,8 +342,9 @@ def _parse_pylint_stdio_result(document, stdout): "start": {"line": line, "character": character}, "end": { "line": line, - # no way to determine the column - "character": len(document.lines[line]) - 1, + "character": _find_end_of_identifier( + document.lines[line], character + ), }, }, "message": msg, @@ -352,3 +357,11 @@ def _parse_pylint_stdio_result(document, stdout): diagnostics.append(diagnostic) return diagnostics + + +def _find_end_of_identifier(string, start): + """Find the end of the identifier starting at the given position.""" + for i in range(len(string), start, -1): + if string[start:i].isidentifier(): + return i + return len(string) - 1