Skip to content

Commit 4f25a86

Browse files
committed
Framework to queue more locations
1 parent f88b8f2 commit 4f25a86

File tree

6 files changed

+241
-73
lines changed

6 files changed

+241
-73
lines changed

internal/ls/findallreferences.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,29 @@ func getSymbolScope(symbol *ast.Symbol) *ast.Node {
416416

417417
// === functions on (*ls) ===
418418

419-
func (l *LanguageService) ProvideReferences(ctx context.Context, params *lsproto.ReferenceParams) (lsproto.ReferencesResponse, error) {
419+
func (l *LanguageService) ProvideSymbolsAndEntries(ctx context.Context, uri lsproto.DocumentUri, documentPosition lsproto.Position, isRename bool) (*ast.Node, []*SymbolAndEntries, bool) {
420420
// `findReferencedSymbols` except only computes the information needed to return reference locations
421-
program, sourceFile := l.getProgramAndFile(params.TextDocument.Uri)
422-
position := int(l.converters.LineAndCharacterToPosition(sourceFile, params.Position))
421+
program, sourceFile := l.getProgramAndFile(uri)
422+
position := int(l.converters.LineAndCharacterToPosition(sourceFile, documentPosition))
423423

424424
node := astnav.GetTouchingPropertyName(sourceFile, position)
425-
options := refOptions{use: referenceUseReferences}
425+
if isRename && node.Kind != ast.KindIdentifier {
426+
return node, nil, false
427+
}
426428

427-
symbolsAndEntries := l.getReferencedSymbolsForNode(ctx, position, node, program, program.GetSourceFiles(), options, nil)
429+
var options refOptions
430+
if !isRename {
431+
options.use = referenceUseReferences
432+
} else {
433+
options.use = referenceUseRename
434+
options.useAliasesForRename = true
435+
}
436+
437+
return node, l.getReferencedSymbolsForNode(ctx, position, node, program, program.GetSourceFiles(), options, nil), true
438+
}
428439

440+
func (l *LanguageService) ProvideReferencesFromSymbolAndEntries(ctx context.Context, params *lsproto.ReferenceParams, originalNode *ast.Node, symbolsAndEntries []*SymbolAndEntries) (lsproto.ReferencesResponse, error) {
441+
// `findReferencedSymbols` except only computes the information needed to return reference locations
429442
locations := core.FlatMap(symbolsAndEntries, l.convertSymbolAndEntriesToLocations)
430443
return lsproto.LocationsOrNull{Locations: &locations}, nil
431444
}
@@ -466,15 +479,12 @@ func (l *LanguageService) getImplementationReferenceEntries(ctx context.Context,
466479
return core.FlatMap(symbolsAndEntries, func(s *SymbolAndEntries) []*ReferenceEntry { return s.references })
467480
}
468481

469-
func (l *LanguageService) ProvideRename(ctx context.Context, params *lsproto.RenameParams) (lsproto.WorkspaceEditOrNull, error) {
470-
program, sourceFile := l.getProgramAndFile(params.TextDocument.Uri)
471-
position := int(l.converters.LineAndCharacterToPosition(sourceFile, params.Position))
472-
node := astnav.GetTouchingPropertyName(sourceFile, position)
473-
if node.Kind != ast.KindIdentifier {
482+
func (l *LanguageService) ProvideRenameFromSymbolAndEntries(ctx context.Context, params *lsproto.RenameParams, originalNode *ast.Node, symbolsAndEntries []*SymbolAndEntries) (lsproto.WorkspaceEditOrNull, error) {
483+
if originalNode.Kind != ast.KindIdentifier {
474484
return lsproto.WorkspaceEditOrNull{}, nil
475485
}
476-
options := refOptions{use: referenceUseRename, useAliasesForRename: true}
477-
symbolsAndEntries := l.getReferencedSymbolsForNode(ctx, position, node, program, program.GetSourceFiles(), options, nil)
486+
487+
program := l.GetProgram()
478488
entries := core.FlatMap(symbolsAndEntries, func(s *SymbolAndEntries) []*ReferenceEntry { return s.references })
479489
changes := make(map[lsproto.DocumentUri][]*lsproto.TextEdit)
480490
checker, done := program.GetTypeChecker(ctx)
@@ -483,7 +493,7 @@ func (l *LanguageService) ProvideRename(ctx context.Context, params *lsproto.Ren
483493
uri := lsconv.FileNameToDocumentURI(l.getFileNameOfEntry(entry))
484494
textEdit := &lsproto.TextEdit{
485495
Range: *l.getRangeOfEntry(entry),
486-
NewText: l.getTextForRename(node, entry, params.NewName, checker),
496+
NewText: l.getTextForRename(originalNode, entry, params.NewName, checker),
487497
}
488498
changes[uri] = append(changes[uri], textEdit)
489499
}
@@ -903,7 +913,7 @@ func getReferencesForThisKeyword(thisOrSuperKeyword *ast.Node, sourceFiles []*as
903913
if thisParameter == nil {
904914
thisParameter = thisOrSuperKeyword
905915
}
906-
return []*SymbolAndEntries{NewSymbolAndEntries(definitionKindThis, thisParameter, nil, references)}
916+
return []*SymbolAndEntries{NewSymbolAndEntries(definitionKindThis, thisParameter, searchSpaceNode.Symbol(), references)}
907917
}
908918

909919
func getReferencesForSuperKeyword(superKeyword *ast.Node) []*SymbolAndEntries {

internal/lsp/lsproto/_generate/generate.mts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,14 @@ function generateCode() {
526526
writeLine(`\treturn s.TextDocument.Uri`);
527527
writeLine(`}`);
528528
writeLine("");
529+
530+
if (hasTextDocumentPosition(structure)) {
531+
// Generate TextDocumentPosition method
532+
writeLine(`func (s *${structure.name}) TextDocumentPosition() Position {`);
533+
writeLine(`\treturn s.Position`);
534+
writeLine(`}`);
535+
writeLine("");
536+
}
529537
}
530538

531539
// Generate UnmarshalJSONFrom method for structure validation
@@ -880,15 +888,23 @@ function generateCode() {
880888
return parts.join("");
881889
}
882890

883-
function hasTextDocumentURI(structure: Structure) {
891+
function hasSomeProp(structure: Structure, propName: string, propTypeName: string) {
884892
return structure.properties?.some(p =>
885893
!p.optional &&
886-
p.name === "textDocument" &&
894+
p.name === propName &&
887895
p.type.kind === "reference" &&
888-
p.type.name === "TextDocumentIdentifier"
896+
p.type.name === propTypeName
889897
);
890898
}
891899

900+
function hasTextDocumentURI(structure: Structure) {
901+
return hasSomeProp(structure, "textDocument", "TextDocumentIdentifier");
902+
}
903+
904+
function hasTextDocumentPosition(structure: Structure) {
905+
return hasSomeProp(structure, "position", "Position");
906+
}
907+
892908
/**
893909
* Main function
894910
*/

internal/lsp/lsproto/lsp.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ type HasTextDocumentURI interface {
5858
TextDocumentURI() DocumentUri
5959
}
6060

61+
type HasTextDocumentPosition interface {
62+
HasTextDocumentURI
63+
TextDocumentPosition() Position
64+
}
65+
6166
type URI string // !!!
6267

6368
type Method string

internal/lsp/lsproto/lsp_generated.go

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)