Skip to content

Commit

Permalink
Merge pull request #304 from danielroseman/declaration
Browse files Browse the repository at this point in the history
Implement Go to Declaration.
pappasam authored Feb 25, 2024
2 parents dff0f12 + 0c90cef commit 62f0c46
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -456,6 +456,7 @@ jedi-language-server aims to support Jedi's capabilities and expose them through
- [completionItem/resolve](https://microsoft.github.io/language-server-protocol/specification#completionItem_resolve)
- [textDocument/codeAction](https://microsoft.github.io/language-server-protocol/specification#textDocument_codeAction) (refactor.inline, refactor.extract)
- [textDocument/completion](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion)
- [textDocument/declaration](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_declaration)
- [textDocument/definition](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition)
- [textDocument/documentHighlight](https://microsoft.github.io/language-server-protocol/specification#textDocument_documentHighlight)
- [textDocument/documentSymbol](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol)
18 changes: 18 additions & 0 deletions jedi_language_server/server.py
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
INITIALIZE,
TEXT_DOCUMENT_CODE_ACTION,
TEXT_DOCUMENT_COMPLETION,
TEXT_DOCUMENT_DECLARATION,
TEXT_DOCUMENT_DEFINITION,
TEXT_DOCUMENT_DID_CHANGE,
TEXT_DOCUMENT_DID_CLOSE,
@@ -306,6 +307,23 @@ def signature_help(
)


@SERVER.feature(TEXT_DOCUMENT_DECLARATION)
def declaration(
server: JediLanguageServer, params: TextDocumentPositionParams
) -> Optional[List[Location]]:
"""Support Goto Declaration."""
document = server.workspace.get_text_document(params.text_document.uri)
jedi_script = jedi_utils.script(server.project, document)
jedi_lines = jedi_utils.line_column(params.position)
names = jedi_script.goto(*jedi_lines)
definitions = [
definition
for definition in (jedi_utils.lsp_location(name) for name in names)
if definition is not None
]
return definitions if definitions else None


@SERVER.feature(TEXT_DOCUMENT_DEFINITION)
def definition(
server: JediLanguageServer, params: TextDocumentPositionParams
9 changes: 8 additions & 1 deletion tests/lsp_test_client/session.py
Original file line number Diff line number Diff line change
@@ -152,8 +152,15 @@ def text_document_signature_help(self, signature_help_params):
)
return fut.result()

def text_document_declaration(self, declaration_params):
"""Sends text document declaration request to LSP server."""
fut = self._send_request(
"textDocument/declaration", params=declaration_params
)
return fut.result()

def text_document_definition(self, definition_params):
"""Sends text document defintion request to LSP server."""
"""Sends text document definition request to LSP server."""
fut = self._send_request(
"textDocument/definition", params=definition_params
)
28 changes: 28 additions & 0 deletions tests/lsp_tests/test_definition.py
Original file line number Diff line number Diff line change
@@ -36,3 +36,31 @@ def test_definition():
]

assert_that(actual, is_(expected))


def test_declaration():
"""Tests declaration on an imported module.
Test Data: tests/test_data/definition/definition_test1.py
"""
with session.LspSession() as ls_session:
ls_session.initialize()
uri = as_uri(DEFINITION_TEST_ROOT / "definition_test1.py")
actual = ls_session.text_document_declaration(
{
"textDocument": {"uri": uri},
"position": {"line": 5, "character": 0},
}
)

expected = [
{
"uri": uri,
"range": {
"start": {"line": 2, "character": 26},
"end": {"line": 2, "character": 37},
},
}
]

assert_that(actual, is_(expected))

0 comments on commit 62f0c46

Please sign in to comment.