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"
12
11
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
13
12
"github.com/microsoft/typescript-go/internal/scanner"
14
13
)
@@ -23,54 +22,28 @@ func (l *LanguageService) ProvideDefinition(ctx context.Context, documentURI lsp
23
22
c , done := program .GetTypeCheckerForFile (ctx , file )
24
23
defer done ()
25
24
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 {
52
25
if node .Kind == ast .KindOverrideKeyword {
53
26
if sym := getSymbolForOverriddenMember (c , node ); sym != nil {
54
- return l .createLocationsFromDeclarations (sym .Declarations )
27
+ return l .createLocationsFromDeclarations (sym .Declarations ), nil
55
28
}
56
29
}
57
30
58
31
if ast .IsJumpStatementTarget (node ) {
59
32
if label := getTargetLabel (node .Parent , node .Text ()); label != nil {
60
- return l .createLocationsFromDeclarations ([]* ast.Node {label })
33
+ return l .createLocationsFromDeclarations ([]* ast.Node {label }), nil
61
34
}
62
35
}
63
36
64
37
if node .Kind == ast .KindCaseKeyword || node .Kind == ast .KindDefaultKeyword && ast .IsDefaultClause (node .Parent ) {
65
38
if stmt := ast .FindAncestor (node .Parent , ast .IsSwitchStatement ); stmt != nil {
66
39
file := ast .GetSourceFileOfNode (stmt )
67
- return l .createLocationFromFileAndRange (file , scanner .GetRangeOfTokenAtPosition (file , stmt .Pos ()))
40
+ return l .createLocationFromFileAndRange (file , scanner .GetRangeOfTokenAtPosition (file , stmt .Pos ())), nil
68
41
}
69
42
}
70
43
71
44
if node .Kind == ast .KindReturnKeyword || node .Kind == ast .KindYieldKeyword || node .Kind == ast .KindAwaitKeyword {
72
45
if fn := ast .FindAncestor (node , ast .IsFunctionLikeDeclaration ); fn != nil {
73
- return l .createLocationsFromDeclarations ([]* ast.Node {fn })
46
+ return l .createLocationsFromDeclarations ([]* ast.Node {fn }), nil
74
47
}
75
48
}
76
49
@@ -81,9 +54,31 @@ func (l *LanguageService) provideDefinitions(c *checker.Checker, node *ast.Node)
81
54
nonFunctionDeclarations := core .Filter (slices .Clip (declarations ), func (node * ast.Node ) bool { return ! ast .IsFunctionLike (node ) })
82
55
declarations = append (nonFunctionDeclarations , calledDeclaration )
83
56
}
84
- return l .createLocationsFromDeclarations (declarations )
57
+ return l .createLocationsFromDeclarations (declarations ), nil
85
58
}
86
59
60
+ // func (l *LanguageService) getMappedDefinition(definitions lsproto.DefinitionResponse) lsproto.DefinitionResponse {
61
+ // if definitions.Location != nil {
62
+ // definitions.Location = l.getMappedLocation(definitions.Location)
63
+ // }
64
+ // if definitions.Locations != nil {
65
+ // for i, loc := range *definitions.Locations {
66
+ // (*definitions.Locations)[i] = *l.getMappedLocation(&loc)
67
+ // }
68
+ // }
69
+ // if definitions.DefinitionLinks != nil {
70
+ // for i, link := range *definitions.DefinitionLinks {
71
+ // mappedTarget := l.getMappedLocation(&lsproto.Location{Uri: link.TargetUri, Range: link.TargetRange})
72
+ // mappedSelection := l.getMappedLocation(&lsproto.Location{Uri: link.TargetUri, Range: link.TargetSelectionRange})
73
+ // debug.Assert(mappedTarget.Uri == mappedSelection.Uri, "target and selection should be in same file")
74
+ // (*definitions.DefinitionLinks)[i].TargetUri = mappedTarget.Uri
75
+ // (*definitions.DefinitionLinks)[i].TargetRange = mappedTarget.Range
76
+ // (*definitions.DefinitionLinks)[i].TargetSelectionRange = mappedSelection.Range
77
+ // }
78
+ // }
79
+ // return definitions
80
+ // }
81
+
87
82
func (l * LanguageService ) ProvideTypeDefinition (ctx context.Context , documentURI lsproto.DocumentUri , position lsproto.Position ) (lsproto.DefinitionResponse , error ) {
88
83
program , file := l .getProgramAndFile (documentURI )
89
84
node := astnav .GetTouchingPropertyName (file , int (l .converters .LineAndCharacterToPosition (file , position )))
@@ -131,20 +126,17 @@ func (l *LanguageService) createLocationsFromDeclarations(declarations []*ast.No
131
126
for _ , decl := range declarations {
132
127
file := ast .GetSourceFileOfNode (decl )
133
128
name := core .OrElse (ast .GetNameOfDeclaration (decl ), decl )
134
- locations = core .AppendIfUnique (locations , lsproto.Location {
135
- Uri : FileNameToDocumentURI (file .FileName ()),
136
- Range : * l .createLspRangeFromNode (name , file ),
137
- })
129
+ nodeRange := createRangeFromNode (name , file )
130
+ mappedLocation := l .getMappedLocation (file .FileName (), nodeRange )
131
+ locations = core .AppendIfUnique (locations , mappedLocation )
138
132
}
139
133
return lsproto.LocationOrLocationsOrDefinitionLinksOrNull {Locations : & locations }
140
134
}
141
135
142
136
func (l * LanguageService ) createLocationFromFileAndRange (file * ast.SourceFile , textRange core.TextRange ) lsproto.DefinitionResponse {
137
+ mappedLocation := l .getMappedLocation (file .FileName (), textRange )
143
138
return lsproto.LocationOrLocationsOrDefinitionLinksOrNull {
144
- Location : & lsproto.Location {
145
- Uri : FileNameToDocumentURI (file .FileName ()),
146
- Range : * l .createLspRangeFromBounds (textRange .Pos (), textRange .End (), file ),
147
- },
139
+ Location : & mappedLocation ,
148
140
}
149
141
}
150
142
0 commit comments