Skip to content

Commit d9c40d3

Browse files
committed
Move the orchestrating to ls package
1 parent 9aca74f commit d9c40d3

File tree

9 files changed

+557
-441
lines changed

9 files changed

+557
-441
lines changed

internal/ls/codelens.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55

66
"github.com/microsoft/typescript-go/internal/ast"
77
"github.com/microsoft/typescript-go/internal/core"
8+
"github.com/microsoft/typescript-go/internal/diagnostics"
9+
"github.com/microsoft/typescript-go/internal/locale"
810
"github.com/microsoft/typescript-go/internal/ls/lsutil"
911
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
1012
"github.com/microsoft/typescript-go/internal/scanner"
@@ -52,6 +54,81 @@ func (l *LanguageService) ProvideCodeLenses(ctx context.Context, documentURI lsp
5254
}, nil
5355
}
5456

57+
func (l *LanguageService) ResolveCodeLens(ctx context.Context, codeLens *lsproto.CodeLens, showLocationsCommandName *string, orchestrator CrossProjectOrchestrator) (*lsproto.CodeLens, error) {
58+
uri := codeLens.Data.Uri
59+
textDoc := lsproto.TextDocumentIdentifier{Uri: uri}
60+
locale := locale.FromContext(ctx)
61+
var locs []lsproto.Location
62+
var lensTitle string
63+
switch codeLens.Data.Kind {
64+
case lsproto.CodeLensKindReferences:
65+
referencesResp, err := l.ProvideReferences(ctx, &lsproto.ReferenceParams{
66+
TextDocument: textDoc,
67+
Position: codeLens.Range.Start,
68+
Context: &lsproto.ReferenceContext{
69+
// Don't include the declaration in the references count.
70+
IncludeDeclaration: false,
71+
},
72+
}, orchestrator)
73+
if err != nil {
74+
return nil, err
75+
}
76+
if referencesResp.Locations != nil {
77+
locs = *referencesResp.Locations
78+
}
79+
80+
if len(locs) == 1 {
81+
lensTitle = diagnostics.X_1_reference.Localize(locale)
82+
} else {
83+
lensTitle = diagnostics.X_0_references.Localize(locale, len(locs))
84+
}
85+
case lsproto.CodeLensKindImplementations:
86+
87+
implementations, err := l.provideImplementationsEx(
88+
ctx,
89+
&lsproto.ImplementationParams{
90+
TextDocument: textDoc,
91+
Position: codeLens.Range.Start,
92+
},
93+
// "Force" link support to be false so that we only get `Locations` back,
94+
// and don't include the "current" node in the results.
95+
symbolEntryTransformOptions{
96+
requireLocationsResult: true,
97+
dropOriginNodes: true,
98+
},
99+
orchestrator,
100+
)
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
if implementations.Locations != nil {
106+
locs = *implementations.Locations
107+
}
108+
109+
if len(locs) == 1 {
110+
lensTitle = diagnostics.X_1_implementation.Localize(locale)
111+
} else {
112+
lensTitle = diagnostics.X_0_implementations.Localize(locale, len(locs))
113+
}
114+
}
115+
116+
cmd := &lsproto.Command{
117+
Title: lensTitle,
118+
}
119+
if len(locs) > 0 && showLocationsCommandName != nil {
120+
cmd.Command = *showLocationsCommandName
121+
cmd.Arguments = &[]any{
122+
uri,
123+
codeLens.Range.Start,
124+
locs,
125+
}
126+
}
127+
128+
codeLens.Command = cmd
129+
return codeLens, nil
130+
}
131+
55132
func (l *LanguageService) newCodeLensForNode(fileUri lsproto.DocumentUri, file *ast.SourceFile, node *ast.Node, kind lsproto.CodeLensKind) *lsproto.CodeLens {
56133
nodeForRange := node
57134
nodeName := node.Name()

0 commit comments

Comments
 (0)