Skip to content

Commit 5470a9d

Browse files
committed
navigate to source when we can when reporting references
1 parent fcfedab commit 5470a9d

File tree

15 files changed

+32
-118
lines changed

15 files changed

+32
-118
lines changed

internal/ls/documenthighlights.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (l *LanguageService) toDocumentHighlight(entry *ReferenceEntry) (string, *l
7575
kind := lsproto.DocumentHighlightKindRead
7676
if entry.kind == entryKindRange {
7777
return entry.fileName, &lsproto.DocumentHighlight{
78-
Range: *entry.textRange,
78+
Range: *l.getRangeOfEntry(entry),
7979
Kind: &kind,
8080
}
8181
}
@@ -86,7 +86,7 @@ func (l *LanguageService) toDocumentHighlight(entry *ReferenceEntry) (string, *l
8686
}
8787

8888
dh := &lsproto.DocumentHighlight{
89-
Range: *entry.textRange,
89+
Range: *l.getRangeOfEntry(entry),
9090
Kind: &kind,
9191
}
9292

internal/ls/findallreferences.go

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ type ReferenceEntry struct {
104104
node *ast.Node
105105
context *ast.Node // !!! ContextWithStartAndEndNode, optional
106106
fileName string
107-
textRange *lsproto.Range
107+
textRange *core.TextRange
108+
lspRange *lsproto.Location
108109
}
109110

110111
func (entry *SymbolAndEntries) canUseDefinitionSymbol() bool {
@@ -126,29 +127,29 @@ func (entry *SymbolAndEntries) canUseDefinitionSymbol() bool {
126127
}
127128

128129
func (l *LanguageService) getRangeOfEntry(entry *ReferenceEntry) *lsproto.Range {
129-
return l.resolveEntry(entry).textRange
130+
return &l.resolveEntry(entry).lspRange.Range
130131
}
131132

132-
func (l *LanguageService) getFileNameOfEntry(entry *ReferenceEntry) string {
133-
return l.resolveEntry(entry).fileName
133+
func (l *LanguageService) getFileNameOfEntry(entry *ReferenceEntry) lsproto.DocumentUri {
134+
return l.resolveEntry(entry).lspRange.Uri
135+
}
136+
137+
func (l *LanguageService) getLocationOfEntry(entry *ReferenceEntry) *lsproto.Location {
138+
return l.resolveEntry(entry).lspRange
134139
}
135140

136141
func (l *LanguageService) resolveEntry(entry *ReferenceEntry) *ReferenceEntry {
137142
if entry.textRange == nil {
138143
sourceFile := ast.GetSourceFileOfNode(entry.node)
139-
entry.textRange = l.getLspRangeOfNode(entry.node, sourceFile, nil /*endNode*/)
144+
textRange := getRangeOfNode(entry.node, sourceFile, nil /*endNode*/)
145+
entry.textRange = &textRange
140146
entry.fileName = sourceFile.FileName()
141147
}
142-
return entry
143-
}
144-
145-
func (l *LanguageService) newRangeEntry(file *ast.SourceFile, start, end int) *ReferenceEntry {
146-
// !!! used in not-yet implemented features
147-
return &ReferenceEntry{
148-
kind: entryKindRange,
149-
fileName: file.FileName(),
150-
textRange: l.createLspRangeFromBounds(start, end, file),
148+
if entry.lspRange == nil {
149+
location := l.getMappedLocation(entry.fileName, *entry.textRange)
150+
entry.lspRange = &location
151151
}
152+
return entry
152153
}
153154

154155
func newNodeEntryWithKind(node *ast.Node, kind entryKind) *ReferenceEntry {
@@ -284,10 +285,9 @@ func (l *LanguageService) getLspRangeOfNode(node *ast.Node, sourceFile *ast.Sour
284285
sourceFile = ast.GetSourceFileOfNode(node)
285286
}
286287
textRange := getRangeOfNode(node, sourceFile, endNode)
287-
return l.createLspRangeFromRange(textRange, sourceFile)
288+
return l.createLspRangeFromBounds(textRange.Pos(), textRange.End(), sourceFile)
288289
}
289290

290-
// `getTextSpan`
291291
func getRangeOfNode(node *ast.Node, sourceFile *ast.SourceFile, endNode *ast.Node) core.TextRange {
292292
if sourceFile == nil {
293293
sourceFile = ast.GetSourceFileOfNode(node)
@@ -447,9 +447,9 @@ func (nld *position) TextDocumentPosition() lsproto.Position { return nld.pos }
447447
func getFileAndStartPosFromDeclaration(declaration *ast.Node) (*ast.SourceFile, core.TextPos) {
448448
file := ast.GetSourceFileOfNode(declaration)
449449
name := core.OrElse(ast.GetNameOfDeclaration(declaration), declaration)
450-
startPos := getStartPosFromNode(name, file)
450+
textRange := getRangeOfNode(name, file, nil /*endNode*/)
451451

452-
return file, startPos
452+
return file, core.TextPos(textRange.Pos())
453453
}
454454

455455
func (l *LanguageService) GetNonLocalDefinition(ctx context.Context, entry *SymbolAndEntries) lsproto.HasTextDocumentPosition {
@@ -579,7 +579,7 @@ func (l *LanguageService) ProvideRenameFromSymbolAndEntries(ctx context.Context,
579579
checker, done := program.GetTypeChecker(ctx)
580580
defer done()
581581
for _, entry := range entries {
582-
uri := lsconv.FileNameToDocumentURI(l.getFileNameOfEntry(entry))
582+
uri := l.getFileNameOfEntry(entry)
583583
textEdit := &lsproto.TextEdit{
584584
Range: *l.getRangeOfEntry(entry),
585585
NewText: l.getTextForRename(originalNode, entry, params.NewName, checker),
@@ -649,41 +649,28 @@ func (l *LanguageService) convertSymbolAndEntriesToLocations(s *SymbolAndEntries
649649
func (l *LanguageService) convertEntriesToLocations(entries []*ReferenceEntry) []lsproto.Location {
650650
locations := make([]lsproto.Location, len(entries))
651651
for i, entry := range entries {
652-
locations[i] = lsproto.Location{
653-
Uri: lsconv.FileNameToDocumentURI(l.getFileNameOfEntry(entry)),
654-
Range: *l.getRangeOfEntry(entry),
655-
}
652+
locations[i] = *l.getLocationOfEntry(entry)
656653
}
657654
return locations
658655
}
659656

660657
func (l *LanguageService) convertEntriesToLocationLinks(entries []*ReferenceEntry) []*lsproto.LocationLink {
661658
links := make([]*lsproto.LocationLink, len(entries))
662659
for i, entry := range entries {
663-
var targetSelectionRange, targetRange *lsproto.Range
660+
661+
// Get the selection range (the actual reference)
662+
targetSelectionRange := &l.getLocationOfEntry(entry).Range
663+
targetRange := targetSelectionRange
664664

665665
// For entries with nodes, compute ranges directly from the node
666666
if entry.node != nil {
667-
sourceFile := ast.GetSourceFileOfNode(entry.node)
668-
entry.fileName = sourceFile.FileName()
669-
670-
// Get the selection range (the actual reference)
671-
selectionTextRange := getRangeOfNode(entry.node, sourceFile, nil /*endNode*/)
672-
targetSelectionRange = l.createLspRangeFromRange(selectionTextRange, sourceFile)
673-
674667
// Get the context range (broader scope including declaration context)
675668
contextNode := core.OrElse(getContextNode(entry.node), entry.node)
676-
contextTextRange := toContextRange(&selectionTextRange, sourceFile, contextNode)
669+
contextTextRange := toContextRange(entry.textRange, l.program.GetSourceFile(entry.fileName), contextNode)
677670
if contextTextRange != nil {
678-
targetRange = l.createLspRangeFromRange(*contextTextRange, sourceFile)
679-
} else {
680-
targetRange = targetSelectionRange
671+
contextLocation := l.getMappedLocation(entry.fileName, *contextTextRange)
672+
targetRange = &contextLocation.Range
681673
}
682-
} else {
683-
// For range entries, use the pre-computed range
684-
l.resolveEntry(entry)
685-
targetSelectionRange = entry.textRange
686-
targetRange = targetSelectionRange
687674
}
688675

689676
links[i] = &lsproto.LocationLink{
@@ -1216,7 +1203,7 @@ func (l *LanguageService) getReferencedSymbolsForModule(ctx context.Context, pro
12161203
return &ReferenceEntry{
12171204
kind: entryKindRange,
12181205
fileName: reference.referencingFile.FileName(),
1219-
textRange: l.createLspRangeFromBounds(reference.ref.Pos(), reference.ref.End(), reference.referencingFile),
1206+
textRange: &reference.ref.TextRange,
12201207
}
12211208
}
12221209
return nil

internal/ls/utilities.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,6 @@ func createRangeFromNode(node *ast.Node, file *ast.SourceFile) core.TextRange {
417417
return core.NewTextRange(scanner.GetTokenPosOfNode(node, file, false /*includeJSDoc*/), node.End())
418418
}
419419

420-
func getStartPosFromNode(node *ast.Node, file *ast.SourceFile) core.TextPos {
421-
return core.TextPos(scanner.GetTokenPosOfNode(node, file, false /*includeJSDoc*/))
422-
}
423-
424420
func (l *LanguageService) createLspRangeFromBounds(start, end int, file *ast.SourceFile) *lsproto.Range {
425421
lspRange := l.converters.ToLSPRange(file, core.NewTextRange(start, end))
426422
return &lspRange

testdata/baselines/reference/fourslash/findAllReferences/autoImportProvider_referencesCrash.baseline.jsonc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// === findAllReferences ===
2-
// === /home/src/workspaces/project/a/index.d.ts ===
3-
// declare class [|A|] {
4-
// }
5-
// //# sourceMappingURL=index.d.ts.map
2+
// === /home/src/workspaces/project/a/index.ts ===
3+
// class A {}[||]
64

75
// === /home/src/workspaces/project/b/b.ts ===
86
// /// <reference path="../a/index.d.ts" />

testdata/baselines/reference/lspservertests/declarationMaps/findAllReferences.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,6 @@ Config::
110110
// export interface IfaceA {}
111111
// export const instanceA: IfaceA = {};
112112

113-
// === /home/src/projects/project/a/bin/a.d.ts ===
114-
// export declare function [|fnA|](): void;
115-
// export interface IfaceA {
116-
// }
117-
// export declare const instanceA: IfaceA;
118-
// //# sourceMappingURL=a.d.ts.map
119-
120113
// === /home/src/projects/project/user/user.ts ===
121114
// import * as a from "../a/bin/a";
122115
// import * as b from "../b/bin/b";

testdata/baselines/reference/lspservertests/declarationMaps/opening-original-location-project-disableSourceOfProjectReferenceRedirect.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ Config::
9292
/user/username/projects/b/tsconfig.json
9393
RetainingOpenFiles:
9494
/user/username/projects/b/b.ts
95-
// === /user/username/projects/a/a.d.ts ===
96-
// export declare class [|A|] {
97-
// }
98-
// //# sourceMappingURL=a.d.ts.map
99-
10095
// === /user/username/projects/a/a.ts ===
10196
// export class [|A|] { }
10297

testdata/baselines/reference/lspservertests/declarationMaps/rename.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,6 @@ Config::
108108
// export interface IfaceA {}
109109
// export const instanceA: IfaceA = {};
110110

111-
// === /home/src/projects/project/a/bin/a.d.ts ===
112-
// export declare function [|fnARENAME|](): void;
113-
// export interface IfaceA {
114-
// }
115-
// export declare const instanceA: IfaceA;
116-
// //# sourceMappingURL=a.d.ts.map
117-
118111
// === /home/src/projects/project/user/user.ts ===
119112
// import * as a from "../a/bin/a";
120113
// import * as b from "../b/bin/b";

testdata/baselines/reference/lspservertests/findAllRefs/find-refs-to-decl-in-other-proj-when-proj-is-loaded-and-refd-proj-loading-is-disabled-and-proj-ref-redirects-are-disabled-and-a-decl-map-is-present.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,3 @@ Config::
142142
// export class [|B|] {
143143
// M() {}
144144
// }
145-
146-
// === /user/username/projects/myproject/b/lib/index.d.ts ===
147-
// export declare class [|B|] {
148-
// M(): void;
149-
// }
150-
// //# sourceMappingURL=index.d.ts.map

testdata/baselines/reference/lspservertests/findAllRefs/find-refs-to-decl-in-other-proj-when-proj-is-loaded-and-refd-proj-loading-is-disabled-and-proj-ref-redirects-are-enabled-and-a-decl-map-is-present.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,3 @@ Config::
142142
// export class [|B|] {
143143
// M() {}
144144
// }
145-
146-
// === /user/username/projects/myproject/b/lib/index.d.ts ===
147-
// export declare class [|B|] {
148-
// M(): void;
149-
// }
150-
// //# sourceMappingURL=index.d.ts.map

testdata/baselines/reference/lspservertests/findAllRefs/find-refs-to-decl-in-other-proj-when-proj-is-loaded-and-refd-proj-loading-is-enabled-and-proj-ref-redirects-are-disabled-and-a-decl-map-is-present.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,3 @@ Config::
142142
// export class [|B|] {
143143
// M() {}
144144
// }
145-
146-
// === /user/username/projects/myproject/b/lib/index.d.ts ===
147-
// export declare class [|B|] {
148-
// M(): void;
149-
// }
150-
// //# sourceMappingURL=index.d.ts.map

0 commit comments

Comments
 (0)