Skip to content

Commit 69a1621

Browse files
committed
fix: publishDiagnostics starts at 0 and newlines are counted correctly
1 parent 2b35c95 commit 69a1621

File tree

3 files changed

+6
-21
lines changed

3 files changed

+6
-21
lines changed

pylsp/python_lsp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,7 @@ def _lint_notebook_document(self, notebook_document, workspace): # pylint: dis
429429
if offset == 0:
430430
total_source = cell_document.source
431431
else:
432-
maybe_newline = "" if total_source.endswith("\n") else "\n"
433-
total_source += (maybe_newline + cell_document.source)
432+
total_source += ("\n" + cell_document.source)
434433

435434
offset += num_lines
436435

pylsp/workspace.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -519,19 +519,5 @@ def __init__(self, uri, language_id, workspace, source=None, version=None, local
519519
@property
520520
@lock
521521
def line_count(self):
522-
""""
523-
Return the number of lines in the cell document.
524-
525-
This function is used to combine all cell documents into one text document. Note that we need to count a
526-
newline at the end of a non-empty text line as an extra line.
527-
528-
Examples:
529-
- "x\n" is a cell with two lines, "x" and ""
530-
- "x" is a cell with one line, "x"
531-
- "\n" is a cell with one line, ""
532-
"""
533-
if len(self.lines) == 0:
534-
return 1
535-
536-
last_line = self.lines[-1]
537-
return len(self.lines) + (last_line != "\n" and last_line.endswith("\n"))
522+
""""Return the number of lines in the cell document."""
523+
return len(self.source.split('\n'))

test/test_notebook_document.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ def test_notebook_document__did_open(
169169
{
170170
"source": "pyflakes",
171171
"range": {
172-
"start": {"line": 0, "character": 0},
173-
"end": {"line": 0, "character": 11},
172+
"start": {"line": 1, "character": 0},
173+
"end": {"line": 1, "character": 11},
174174
},
175175
"message": "'sys' imported but unused",
176176
"severity": 2,
@@ -190,7 +190,7 @@ def test_notebook_document__did_open(
190190
"start": {"line": 1, "character": 0},
191191
"end": {"line": 1, "character": 11},
192192
},
193-
"message": "E303 too many blank lines (3)",
193+
"message": "E303 too many blank lines (4)",
194194
"code": "E303",
195195
"severity": 2,
196196
},

0 commit comments

Comments
 (0)