Skip to content

Commit 62f0c46

Browse files
authored
Merge pull request pappasam#304 from danielroseman/declaration
Implement Go to Declaration.
2 parents dff0f12 + 0c90cef commit 62f0c46

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ jedi-language-server aims to support Jedi's capabilities and expose them through
456456
- [completionItem/resolve](https://microsoft.github.io/language-server-protocol/specification#completionItem_resolve)
457457
- [textDocument/codeAction](https://microsoft.github.io/language-server-protocol/specification#textDocument_codeAction) (refactor.inline, refactor.extract)
458458
- [textDocument/completion](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion)
459+
- [textDocument/declaration](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_declaration)
459460
- [textDocument/definition](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition)
460461
- [textDocument/documentHighlight](https://microsoft.github.io/language-server-protocol/specification#textDocument_documentHighlight)
461462
- [textDocument/documentSymbol](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol)

jedi_language_server/server.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
INITIALIZE,
1818
TEXT_DOCUMENT_CODE_ACTION,
1919
TEXT_DOCUMENT_COMPLETION,
20+
TEXT_DOCUMENT_DECLARATION,
2021
TEXT_DOCUMENT_DEFINITION,
2122
TEXT_DOCUMENT_DID_CHANGE,
2223
TEXT_DOCUMENT_DID_CLOSE,
@@ -306,6 +307,23 @@ def signature_help(
306307
)
307308

308309

310+
@SERVER.feature(TEXT_DOCUMENT_DECLARATION)
311+
def declaration(
312+
server: JediLanguageServer, params: TextDocumentPositionParams
313+
) -> Optional[List[Location]]:
314+
"""Support Goto Declaration."""
315+
document = server.workspace.get_text_document(params.text_document.uri)
316+
jedi_script = jedi_utils.script(server.project, document)
317+
jedi_lines = jedi_utils.line_column(params.position)
318+
names = jedi_script.goto(*jedi_lines)
319+
definitions = [
320+
definition
321+
for definition in (jedi_utils.lsp_location(name) for name in names)
322+
if definition is not None
323+
]
324+
return definitions if definitions else None
325+
326+
309327
@SERVER.feature(TEXT_DOCUMENT_DEFINITION)
310328
def definition(
311329
server: JediLanguageServer, params: TextDocumentPositionParams

tests/lsp_test_client/session.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,15 @@ def text_document_signature_help(self, signature_help_params):
152152
)
153153
return fut.result()
154154

155+
def text_document_declaration(self, declaration_params):
156+
"""Sends text document declaration request to LSP server."""
157+
fut = self._send_request(
158+
"textDocument/declaration", params=declaration_params
159+
)
160+
return fut.result()
161+
155162
def text_document_definition(self, definition_params):
156-
"""Sends text document defintion request to LSP server."""
163+
"""Sends text document definition request to LSP server."""
157164
fut = self._send_request(
158165
"textDocument/definition", params=definition_params
159166
)

tests/lsp_tests/test_definition.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,31 @@ def test_definition():
3636
]
3737

3838
assert_that(actual, is_(expected))
39+
40+
41+
def test_declaration():
42+
"""Tests declaration on an imported module.
43+
44+
Test Data: tests/test_data/definition/definition_test1.py
45+
"""
46+
with session.LspSession() as ls_session:
47+
ls_session.initialize()
48+
uri = as_uri(DEFINITION_TEST_ROOT / "definition_test1.py")
49+
actual = ls_session.text_document_declaration(
50+
{
51+
"textDocument": {"uri": uri},
52+
"position": {"line": 5, "character": 0},
53+
}
54+
)
55+
56+
expected = [
57+
{
58+
"uri": uri,
59+
"range": {
60+
"start": {"line": 2, "character": 26},
61+
"end": {"line": 2, "character": 37},
62+
},
63+
}
64+
]
65+
66+
assert_that(actual, is_(expected))

0 commit comments

Comments
 (0)