Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Go to Declaration. #304

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions jedi_language_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion tests/lsp_test_client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
28 changes: 28 additions & 0 deletions tests/lsp_tests/test_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Loading