Skip to content

Commit 1e9a51f

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

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

pylsp/python_lsp.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -434,23 +434,25 @@ def _lint_notebook_document(self, notebook_document, workspace): # pylint: dis
434434
offset += num_lines
435435

436436
workspace.put_document(random_uri, total_source)
437-
document_diagnostics = flatten(self._hook('pylsp_lint', random_uri, is_saved=True))
438-
439-
# Now we need to map the diagnostics back to the correct cell and publish them.
440-
# Note: this is O(n*m) in the number of cells and diagnostics, respectively.
441-
for cell in cell_list:
442-
cell_diagnostics = []
443-
for diagnostic in document_diagnostics:
444-
if diagnostic['range']['start']['line'] > cell['line_end'] \
445-
or diagnostic['range']['end']['line'] < cell['line_start']:
446-
continue
447-
diagnostic['range']['start']['line'] = diagnostic['range']['start']['line'] - cell['line_start']
448-
diagnostic['range']['end']['line'] = diagnostic['range']['end']['line'] - cell['line_start']
449-
cell_diagnostics.append(diagnostic)
450-
451-
workspace.publish_diagnostics(cell['uri'], cell_diagnostics)
452437

453-
workspace.rm_document(random_uri)
438+
try:
439+
document_diagnostics = flatten(self._hook('pylsp_lint', random_uri, is_saved=True))
440+
441+
# Now we need to map the diagnostics back to the correct cell and publish them.
442+
# Note: this is O(n*m) in the number of cells and diagnostics, respectively.
443+
for cell in cell_list:
444+
cell_diagnostics = []
445+
for diagnostic in document_diagnostics:
446+
if diagnostic['range']['start']['line'] > cell['line_end'] \
447+
or diagnostic['range']['end']['line'] < cell['line_start']:
448+
continue
449+
diagnostic['range']['start']['line'] = diagnostic['range']['start']['line'] - cell['line_start']
450+
diagnostic['range']['end']['line'] = diagnostic['range']['end']['line'] - cell['line_start']
451+
cell_diagnostics.append(diagnostic)
452+
453+
workspace.publish_diagnostics(cell['uri'], cell_diagnostics)
454+
finally:
455+
workspace.rm_document(random_uri)
454456

455457
def references(self, doc_uri, position, exclude_declaration):
456458
return flatten(self._hook(

0 commit comments

Comments
 (0)