11# Copyright 2017-2020 Palantir Technologies, Inc.
22# Copyright 2021- Python Language Server Contributors.
3-
3+ from __future__ import annotations
44import logging
5+ from typing import Any , Dict , List , TYPE_CHECKING
56from pylsp import hookimpl , uris , _utils
67
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+
714log = logging .getLogger (__name__ )
815
916
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+
1039@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 ]]:
1243 settings = config .plugin_settings ("jedi_definition" )
1344 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 (
1547 follow_imports = settings .get ("follow_imports" , True ),
1648 follow_builtin_imports = settings .get ("follow_builtin_imports" , True ),
1749 ** code_position ,
1850 )
19-
51+ definitions = [ _resolve_definition ( d , script , settings ) for d in definitions ]
2052 follow_builtin_defns = settings .get ("follow_builtin_definitions" , True )
2153 return [
2254 {
@@ -31,7 +63,7 @@ def pylsp_definitions(config, document, position):
3163 ]
3264
3365
34- def _not_internal_definition (definition ) :
66+ def _not_internal_definition (definition : Name ) -> bool :
3567 return (
3668 definition .line is not None
3769 and definition .column is not None
0 commit comments