8
8
"github.com/microsoft/typescript-go/internal/astnav"
9
9
"github.com/microsoft/typescript-go/internal/checker"
10
10
"github.com/microsoft/typescript-go/internal/core"
11
+ "github.com/microsoft/typescript-go/internal/debug"
11
12
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
12
13
"github.com/microsoft/typescript-go/internal/scanner"
13
14
)
@@ -22,28 +23,54 @@ func (l *LanguageService) ProvideDefinition(ctx context.Context, documentURI lsp
22
23
c , done := program .GetTypeCheckerForFile (ctx , file )
23
24
defer done ()
24
25
26
+ return l .getMappedDefinition (l .provideDefinitions (c , node )), nil
27
+ }
28
+
29
+ func (l * LanguageService ) getMappedDefinition (definitions lsproto.DefinitionResponse ) lsproto.DefinitionResponse {
30
+ if definitions .Location != nil {
31
+ definitions .Location = l .getMappedLocation (definitions .Location )
32
+ }
33
+ if definitions .Locations != nil {
34
+ for i , loc := range * definitions .Locations {
35
+ (* definitions .Locations )[i ] = * l .getMappedLocation (& loc )
36
+ }
37
+ }
38
+ if definitions .DefinitionLinks != nil {
39
+ for i , link := range * definitions .DefinitionLinks {
40
+ mappedTarget := l .getMappedLocation (& lsproto.Location {Uri : link .TargetUri , Range : link .TargetRange })
41
+ mappedSelection := l .getMappedLocation (& lsproto.Location {Uri : link .TargetUri , Range : link .TargetSelectionRange })
42
+ debug .Assert (mappedTarget .Uri == mappedSelection .Uri , "target and selection should be in same file" )
43
+ (* definitions .DefinitionLinks )[i ].TargetUri = mappedTarget .Uri
44
+ (* definitions .DefinitionLinks )[i ].TargetRange = mappedTarget .Range
45
+ (* definitions .DefinitionLinks )[i ].TargetSelectionRange = mappedSelection .Range
46
+ }
47
+ }
48
+ return definitions
49
+ }
50
+
51
+ func (l * LanguageService ) provideDefinitions (c * checker.Checker , node * ast.Node ) lsproto.DefinitionResponse {
25
52
if node .Kind == ast .KindOverrideKeyword {
26
53
if sym := getSymbolForOverriddenMember (c , node ); sym != nil {
27
- return l .createLocationsFromDeclarations (sym .Declarations ), nil
54
+ return l .createLocationsFromDeclarations (sym .Declarations )
28
55
}
29
56
}
30
57
31
58
if ast .IsJumpStatementTarget (node ) {
32
59
if label := getTargetLabel (node .Parent , node .Text ()); label != nil {
33
- return l .createLocationsFromDeclarations ([]* ast.Node {label }), nil
60
+ return l .createLocationsFromDeclarations ([]* ast.Node {label })
34
61
}
35
62
}
36
63
37
64
if node .Kind == ast .KindCaseKeyword || node .Kind == ast .KindDefaultKeyword && ast .IsDefaultClause (node .Parent ) {
38
65
if stmt := ast .FindAncestor (node .Parent , ast .IsSwitchStatement ); stmt != nil {
39
66
file := ast .GetSourceFileOfNode (stmt )
40
- return l .createLocationFromFileAndRange (file , scanner .GetRangeOfTokenAtPosition (file , stmt .Pos ())), nil
67
+ return l .createLocationFromFileAndRange (file , scanner .GetRangeOfTokenAtPosition (file , stmt .Pos ()))
41
68
}
42
69
}
43
70
44
71
if node .Kind == ast .KindReturnKeyword || node .Kind == ast .KindYieldKeyword || node .Kind == ast .KindAwaitKeyword {
45
72
if fn := ast .FindAncestor (node , ast .IsFunctionLikeDeclaration ); fn != nil {
46
- return l .createLocationsFromDeclarations ([]* ast.Node {fn }), nil
73
+ return l .createLocationsFromDeclarations ([]* ast.Node {fn })
47
74
}
48
75
}
49
76
@@ -54,7 +81,7 @@ func (l *LanguageService) ProvideDefinition(ctx context.Context, documentURI lsp
54
81
nonFunctionDeclarations := core .Filter (slices .Clip (declarations ), func (node * ast.Node ) bool { return ! ast .IsFunctionLike (node ) })
55
82
declarations = append (nonFunctionDeclarations , calledDeclaration )
56
83
}
57
- return l .createLocationsFromDeclarations (declarations ), nil
84
+ return l .createLocationsFromDeclarations (declarations )
58
85
}
59
86
60
87
func (l * LanguageService ) ProvideTypeDefinition (ctx context.Context , documentURI lsproto.DocumentUri , position lsproto.Position ) (lsproto.DefinitionResponse , error ) {
0 commit comments