Skip to content

Commit 902fb5b

Browse files
committed
Add textDocument/typeDefinition plugin
1 parent 70d9f12 commit 902fb5b

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

CONFIGURATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This server can be configured using the `workspace/didChangeConfiguration` metho
3333
| `pylsp.plugins.jedi_completion.resolve_at_most` | `integer` | How many labels and snippets (at most) should be resolved? | `25` |
3434
| `pylsp.plugins.jedi_completion.cache_for` | `array` of `string` items | Modules for which labels and snippets should be cached. | `["pandas", "numpy", "tensorflow", "matplotlib"]` |
3535
| `pylsp.plugins.jedi_definition.enabled` | `boolean` | Enable or disable the plugin. | `true` |
36+
| `pylsp.plugins.type_definition.enabled` | `boolean` | Enable or disable the plugin. | `true` |
3637
| `pylsp.plugins.jedi_definition.follow_imports` | `boolean` | The goto call will follow imports. | `true` |
3738
| `pylsp.plugins.jedi_definition.follow_builtin_imports` | `boolean` | If follow_imports is True will decide if it follow builtin imports. | `true` |
3839
| `pylsp.plugins.jedi_definition.follow_builtin_definitions` | `boolean` | Follow builtin and extension definitions to stubs. | `true` |

pylsp/config/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@
225225
"default": true,
226226
"description": "Enable or disable the plugin."
227227
},
228+
"pylsp.plugins.type_definition.enabled": {
229+
"type": "boolean",
230+
"default": true,
231+
"description": "Enable or disable the plugin."
232+
},
228233
"pylsp.plugins.jedi_definition.follow_imports": {
229234
"type": "boolean",
230235
"default": true,

pylsp/hookspecs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def pylsp_definitions(config, workspace, document, position) -> None:
3838
pass
3939

4040

41+
@hookspec(firstresult=True)
42+
def pylsp_type_definition(config, document, position):
43+
pass
44+
45+
4146
@hookspec
4247
def pylsp_dispatchers(config, workspace) -> None:
4348
pass

pylsp/plugins/type_definition.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2017-2020 Palantir Technologies, Inc.
2+
# Copyright 2021- Python Language Server Contributors.
3+
4+
import logging
5+
6+
from pylsp import _utils, hookimpl
7+
8+
9+
log = logging.getLogger(__name__)
10+
11+
12+
def lsp_location(name):
13+
module_path = name.module_path
14+
if module_path is None or name.line is None or name.column is None:
15+
return None
16+
uri = module_path.as_uri()
17+
return {
18+
"uri": str(uri),
19+
"range": {
20+
"start": {"line": name.line - 1, "character": name.column},
21+
"end": {"line": name.line - 1, "character": name.column + len(name.name)},
22+
},
23+
}
24+
25+
26+
@hookimpl
27+
def pylsp_type_definition(config, document, position):
28+
try:
29+
kwargs = _utils.position_to_jedi_linecolumn(document, position)
30+
script = document.jedi_script()
31+
names = script.infer(**kwargs)
32+
definitions = [
33+
definition
34+
for definition in [lsp_location(name) for name in names]
35+
if definition is not None
36+
]
37+
return definitions
38+
except Exception as e:
39+
log.debug("Failed to run type_definition: %s", e)
40+
return []

pylsp/python_lsp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ def capabilities(self):
276276
"documentRangeFormattingProvider": True,
277277
"documentSymbolProvider": True,
278278
"definitionProvider": True,
279+
"typeDefinitionProvider": True,
279280
"executeCommandProvider": {
280281
"commands": flatten(self._hook("pylsp_commands"))
281282
},
@@ -412,6 +413,9 @@ def completion_item_resolve(self, completion_item):
412413
def definitions(self, doc_uri, position):
413414
return flatten(self._hook("pylsp_definitions", doc_uri, position=position))
414415

416+
def type_definition(self, doc_uri, position):
417+
return self._hook("pylsp_type_definition", doc_uri, position=position)
418+
415419
def document_symbols(self, doc_uri):
416420
return flatten(self._hook("pylsp_document_symbols", doc_uri))
417421

@@ -762,6 +766,9 @@ def m_text_document__definition(self, textDocument=None, position=None, **_kwarg
762766
return self._cell_document__definition(document, position, **_kwargs)
763767
return self.definitions(textDocument["uri"], position)
764768

769+
def m_text_document__type_definition(self, textDocument=None, position=None, **_kwargs):
770+
return self.type_definition(textDocument["uri"], position)
771+
765772
def m_text_document__document_highlight(
766773
self, textDocument=None, position=None, **_kwargs
767774
):

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ folding = "pylsp.plugins.folding"
6666
flake8 = "pylsp.plugins.flake8_lint"
6767
jedi_completion = "pylsp.plugins.jedi_completion"
6868
jedi_definition = "pylsp.plugins.definition"
69+
type_definition = "pylsp.plugins.type_definition"
6970
jedi_hover = "pylsp.plugins.hover"
7071
jedi_highlight = "pylsp.plugins.highlight"
7172
jedi_references = "pylsp.plugins.references"

0 commit comments

Comments
 (0)