Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e9fee6a

Browse files
committedJul 28, 2023
Fix go to definition for notebook documents
Fix test
1 parent 46ab3f1 commit e9fee6a

File tree

2 files changed

+47
-54
lines changed

2 files changed

+47
-54
lines changed
 

‎pylsp/python_lsp.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,12 @@ def m_text_document__completion(self, textDocument=None, position=None, **_kwarg
586586
return self.completions(textDocument['uri'], position)
587587

588588
def _cell_document__definition(self, cellDocument=None, position=None, **_kwargs):
589-
# First, we create a temp TextDocument that represents the whole notebook
590-
# contents. We'll use this to send to the hook.
589+
# First, we create a temp TextDocument to send to the hook that represents the whole notebook
590+
# contents.
591591
workspace = self._match_uri_to_workspace(cellDocument.notebook_uri)
592+
notebookDocument = workspace.get_maybe_document(cellDocument.notebook_uri)
593+
if notebookDocument is None:
594+
raise ValueError("Invalid notebook document")
592595

593596
random_uri = str(uuid.uuid4())
594597
# cell_list helps us map the diagnostics back to the correct cell later.
@@ -636,19 +639,26 @@ def _cell_document__definition(self, cellDocument=None, position=None, **_kwargs
636639
# }
637640
print(definitions)
638641
for definition in definitions:
639-
if definition['uri'] == random_uri:
640-
# find what cell the start is in
641-
# make sure the end is inside the cell's line_end
642-
# subtract that cell's line_start from both definition start and end
643-
pass
642+
# TODO: a better test for if a definition is the random_uri
643+
if random_uri in definition['uri']:
644+
# Find the cell the start is in
645+
for cell in cell_list:
646+
# TODO: perhaps it is more correct to check definition['range']['end']['line'] <= cell['line_end'], but
647+
# that would mess up if a definition was split over cells
648+
if cell['line_start'] <= definition['range']['start']['line'] <= cell['line_end']:
649+
definition['uri'] = cell['uri']
650+
definition['range']['start']['line'] -= cell['line_start']
651+
definition['range']['end']['line'] -= cell['line_start']
644652
return definitions
645653
finally:
646654
workspace.rm_document(random_uri)
647655

648656
def m_text_document__definition(self, textDocument=None, position=None, **_kwargs):
649-
if isinstance(textDocument, Cell):
650-
# actually, test to see if the document is a cell document
651-
return self._cell_document__definition(textDocument, position, **_kwargs)
657+
# textDocument here is just a dict with a uri
658+
workspace = self._match_uri_to_workspace(textDocument['uri'])
659+
document = workspace.get_document(textDocument['uri'])
660+
if isinstance(document, Cell):
661+
return self._cell_document__definition(document, position, **_kwargs)
652662
return self.definitions(textDocument['uri'], position)
653663

654664
def m_text_document__document_highlight(self, textDocument=None, position=None, **_kwargs):

‎test/test_notebook_document.py

+27-44
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def test_notebook_definition(
619619
{
620620
"uri": "cell_1_uri",
621621
"languageId": "python",
622-
"text": "x=1",
622+
"text": "y=2\nx=1",
623623
},
624624
{
625625
"uri": "cell_2_uri",
@@ -629,52 +629,35 @@ def test_notebook_definition(
629629
],
630630
},
631631
)
632+
# wait for expected diagnostics messages
632633
wait_for_condition(lambda: mock_notify.call_count >= 2)
633634
assert len(server.workspace.documents) == 3
634635
for uri in ["cell_1_uri", "cell_2_uri", "notebook_uri"]:
635636
assert uri in server.workspace.documents
636637

637-
with patch.object(server._endpoint, "notify") as mock_notify:
638-
client._endpoint.notify(
639-
"textDocument/definition",
640-
{
641-
"textDocument": {
642-
"uri": "cell_2_uri",
643-
},
644-
"position": {
645-
"line": 0,
646-
"character": 1
647-
}
638+
future = client._endpoint.request(
639+
"textDocument/definition",
640+
{
641+
"textDocument": {
642+
"uri": "cell_2_uri",
648643
},
649-
)
650-
raise ValueError(mock_notify.mock_calls)
651-
wait_for_condition(lambda: mock_notify.call_count >= 1)
652-
return
653-
654-
655-
# TODO: definition return
656-
expected_call_args = [
657-
call(
658-
"textDocument/publishDiagnostics",
659-
params={"uri": "cell_1_uri", "diagnostics": []},
660-
),
661-
call(
662-
"textDocument/publishDiagnostics",
663-
params={
664-
"uri": "cell_3_uri",
665-
"diagnostics": [
666-
{
667-
"source": "pycodestyle",
668-
"range": {
669-
"start": {"line": 0, "character": 8},
670-
"end": {"line": 0, "character": 8},
671-
},
672-
"message": "W292 no newline at end of file",
673-
"code": "W292",
674-
"severity": 2,
675-
}
676-
],
677-
},
678-
),
679-
]
680-
mock_notify.assert_has_calls(expected_call_args)
644+
"position": {
645+
"line": 0,
646+
"character": 1
647+
}
648+
},
649+
)
650+
result = future.result(CALL_TIMEOUT_IN_SECONDS)
651+
assert result == [{
652+
'uri': 'cell_1_uri',
653+
'range': {
654+
'start': {
655+
'line': 1,
656+
'character': 0
657+
},
658+
'end': {
659+
'line': 1,
660+
'character': 1
661+
}
662+
}
663+
}]

0 commit comments

Comments
 (0)
Please sign in to comment.