1
1
# Copyright 2017-2020 Palantir Technologies, Inc.
2
2
# Copyright 2021- Python Language Server Contributors.
3
-
3
+ from __future__ import annotations
4
4
import logging
5
+ from typing import Any , Dict , List , TYPE_CHECKING
5
6
from pylsp import hookimpl , uris , _utils
6
7
8
+ if TYPE_CHECKING :
9
+ from jedi .api import Script
10
+ from jedi .api .classes import Name
11
+ from pylsp .config .config import Config
12
+ from pylsp .workspace import Document
13
+
7
14
log = logging .getLogger (__name__ )
8
15
9
16
17
+ MAX_JEDI_GOTO_HOPS = 100
18
+
19
+
20
+ def _resolve_definition (
21
+ maybe_defn : Name , script : Script , settings : Dict [str , Any ]
22
+ ) -> Name :
23
+ for _ in range (MAX_JEDI_GOTO_HOPS ):
24
+ if maybe_defn .is_definition () or maybe_defn .module_path != script .path :
25
+ break
26
+ defns = script .goto (
27
+ follow_imports = settings .get ("follow_imports" , True ),
28
+ follow_builtin_imports = settings .get ("follow_builtin_imports" , True ),
29
+ line = maybe_defn .line ,
30
+ column = maybe_defn .column ,
31
+ )
32
+ if len (defns ) == 1 :
33
+ maybe_defn = defns [0 ]
34
+ else :
35
+ break
36
+ return maybe_defn
37
+
38
+
10
39
@hookimpl
11
- def pylsp_definitions (config , document , position ):
40
+ def pylsp_definitions (
41
+ config : Config , document : Document , position : Dict [str , int ]
42
+ ) -> List [Dict [str , Any ]]:
12
43
settings = config .plugin_settings ("jedi_definition" )
13
44
code_position = _utils .position_to_jedi_linecolumn (document , position )
14
- definitions = document .jedi_script (use_document_path = True ).goto (
45
+ script = document .jedi_script (use_document_path = True )
46
+ definitions = script .goto (
15
47
follow_imports = settings .get ("follow_imports" , True ),
16
48
follow_builtin_imports = settings .get ("follow_builtin_imports" , True ),
17
49
** code_position ,
18
50
)
19
-
51
+ definitions = [ _resolve_definition ( d , script , settings ) for d in definitions ]
20
52
follow_builtin_defns = settings .get ("follow_builtin_definitions" , True )
21
53
return [
22
54
{
@@ -31,7 +63,7 @@ def pylsp_definitions(config, document, position):
31
63
]
32
64
33
65
34
- def _not_internal_definition (definition ) :
66
+ def _not_internal_definition (definition : Name ) -> bool :
35
67
return (
36
68
definition .line is not None
37
69
and definition .column is not None
0 commit comments