Skip to content

Commit dd043f4

Browse files
authored
Fully resolve LSP client caps to non-pointers, pass by context (#2095)
1 parent 77ea74f commit dd043f4

File tree

13 files changed

+1657
-271
lines changed

13 files changed

+1657
-271
lines changed

internal/ls/completions.go

Lines changed: 63 additions & 95 deletions
Large diffs are not rendered by default.

internal/ls/definition.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ func (l *LanguageService) ProvideDefinition(
1717
ctx context.Context,
1818
documentURI lsproto.DocumentUri,
1919
position lsproto.Position,
20-
clientSupportsLink bool,
2120
) (lsproto.DefinitionResponse, error) {
21+
caps := lsproto.GetClientCapabilities(ctx)
22+
clientSupportsLink := caps.TextDocument.Definition.LinkSupport
23+
2224
program, file := l.getProgramAndFile(documentURI)
2325
node := astnav.GetTouchingPropertyName(file, int(l.converters.LineAndCharacterToPosition(file, position)))
2426
if node.Kind == ast.KindSourceFile {
@@ -68,8 +70,10 @@ func (l *LanguageService) ProvideTypeDefinition(
6870
ctx context.Context,
6971
documentURI lsproto.DocumentUri,
7072
position lsproto.Position,
71-
clientSupportsLink bool,
72-
) (lsproto.DefinitionResponse, error) {
73+
) (lsproto.TypeDefinitionResponse, error) {
74+
caps := lsproto.GetClientCapabilities(ctx)
75+
clientSupportsLink := caps.TextDocument.TypeDefinition.LinkSupport
76+
7377
program, file := l.getProgramAndFile(documentURI)
7478
node := astnav.GetTouchingPropertyName(file, int(l.converters.LineAndCharacterToPosition(file, position)))
7579
if node.Kind == ast.KindSourceFile {
@@ -279,8 +283,3 @@ func getDeclarationsFromType(t *checker.Type) []*ast.Node {
279283
}
280284
return result
281285
}
282-
283-
func clientSupportsLink(clientCapabilities *lsproto.DefinitionClientCapabilities) bool {
284-
return clientCapabilities != nil &&
285-
ptrIsTrue(clientCapabilities.LinkSupport)
286-
}

internal/ls/diagnostics.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
1313
)
1414

15-
func (l *LanguageService) ProvideDiagnostics(ctx context.Context, uri lsproto.DocumentUri, clientOptions *lsproto.DiagnosticClientCapabilities) (lsproto.DocumentDiagnosticResponse, error) {
15+
func (l *LanguageService) ProvideDiagnostics(ctx context.Context, uri lsproto.DocumentUri) (lsproto.DocumentDiagnosticResponse, error) {
1616
program, file := l.getProgramAndFile(uri)
1717

1818
diagnostics := make([][]*ast.Diagnostic, 0, 4)
@@ -28,26 +28,27 @@ func (l *LanguageService) ProvideDiagnostics(ctx context.Context, uri lsproto.Do
2828

2929
return lsproto.RelatedFullDocumentDiagnosticReportOrUnchangedDocumentDiagnosticReport{
3030
FullDocumentDiagnosticReport: &lsproto.RelatedFullDocumentDiagnosticReport{
31-
Items: l.toLSPDiagnostics(clientOptions, diagnostics...),
31+
Items: l.toLSPDiagnostics(ctx, diagnostics...),
3232
},
3333
}, nil
3434
}
3535

36-
func (l *LanguageService) toLSPDiagnostics(clientOptions *lsproto.DiagnosticClientCapabilities, diagnostics ...[]*ast.Diagnostic) []*lsproto.Diagnostic {
36+
func (l *LanguageService) toLSPDiagnostics(ctx context.Context, diagnostics ...[]*ast.Diagnostic) []*lsproto.Diagnostic {
3737
size := 0
3838
for _, diagSlice := range diagnostics {
3939
size += len(diagSlice)
4040
}
4141
lspDiagnostics := make([]*lsproto.Diagnostic, 0, size)
4242
for _, diagSlice := range diagnostics {
4343
for _, diag := range diagSlice {
44-
lspDiagnostics = append(lspDiagnostics, l.toLSPDiagnostic(clientOptions, diag))
44+
lspDiagnostics = append(lspDiagnostics, l.toLSPDiagnostic(ctx, diag))
4545
}
4646
}
4747
return lspDiagnostics
4848
}
4949

50-
func (l *LanguageService) toLSPDiagnostic(clientOptions *lsproto.DiagnosticClientCapabilities, diagnostic *ast.Diagnostic) *lsproto.Diagnostic {
50+
func (l *LanguageService) toLSPDiagnostic(ctx context.Context, diagnostic *ast.Diagnostic) *lsproto.Diagnostic {
51+
clientOptions := lsproto.GetClientCapabilities(ctx).TextDocument.Diagnostic
5152
var severity lsproto.DiagnosticSeverity
5253
switch diagnostic.Category() {
5354
case diagnostics.CategorySuggestion:
@@ -61,7 +62,7 @@ func (l *LanguageService) toLSPDiagnostic(clientOptions *lsproto.DiagnosticClien
6162
}
6263

6364
var relatedInformation []*lsproto.DiagnosticRelatedInformation
64-
if clientOptions != nil && ptrIsTrue(clientOptions.RelatedInformation) {
65+
if clientOptions.RelatedInformation {
6566
relatedInformation = make([]*lsproto.DiagnosticRelatedInformation, 0, len(diagnostic.RelatedInformation()))
6667
for _, related := range diagnostic.RelatedInformation() {
6768
relatedInformation = append(relatedInformation, &lsproto.DiagnosticRelatedInformation{
@@ -75,7 +76,7 @@ func (l *LanguageService) toLSPDiagnostic(clientOptions *lsproto.DiagnosticClien
7576
}
7677

7778
var tags []lsproto.DiagnosticTag
78-
if clientOptions != nil && clientOptions.TagSupport != nil && (diagnostic.ReportsUnnecessary() || diagnostic.ReportsDeprecated()) {
79+
if len(clientOptions.TagSupport.ValueSet) > 0 && (diagnostic.ReportsUnnecessary() || diagnostic.ReportsDeprecated()) {
7980
tags = make([]lsproto.DiagnosticTag, 0, 2)
8081
if diagnostic.ReportsUnnecessary() && slices.Contains(clientOptions.TagSupport.ValueSet, lsproto.DiagnosticTagUnnecessary) {
8182
tags = append(tags, lsproto.DiagnosticTagUnnecessary)

internal/ls/findallreferences.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (l *LanguageService) ProvideReferences(ctx context.Context, params *lsproto
430430
return lsproto.LocationsOrNull{Locations: &locations}, nil
431431
}
432432

433-
func (l *LanguageService) ProvideImplementations(ctx context.Context, params *lsproto.ImplementationParams, clientSupportsLink bool) (lsproto.ImplementationResponse, error) {
433+
func (l *LanguageService) ProvideImplementations(ctx context.Context, params *lsproto.ImplementationParams) (lsproto.ImplementationResponse, error) {
434434
program, sourceFile := l.getProgramAndFile(params.TextDocument.Uri)
435435
position := int(l.converters.LineAndCharacterToPosition(sourceFile, params.Position))
436436
node := astnav.GetTouchingPropertyName(sourceFile, position)
@@ -452,7 +452,7 @@ func (l *LanguageService) ProvideImplementations(ctx context.Context, params *ls
452452
}
453453
}
454454

455-
if clientSupportsLink {
455+
if lsproto.GetClientCapabilities(ctx).TextDocument.Implementation.LinkSupport {
456456
links := l.convertEntriesToLocationLinks(entries)
457457
return lsproto.LocationOrLocationsOrDefinitionLinksOrNull{DefinitionLinks: &links}, nil
458458
}

internal/ls/hover.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ const (
1818
typeFormatFlags = checker.TypeFormatFlagsUseAliasDefinedOutsideCurrentScope
1919
)
2020

21-
func (l *LanguageService) ProvideHover(ctx context.Context, documentURI lsproto.DocumentUri, position lsproto.Position, contentFormat lsproto.MarkupKind) (lsproto.HoverResponse, error) {
21+
func (l *LanguageService) ProvideHover(ctx context.Context, documentURI lsproto.DocumentUri, position lsproto.Position) (lsproto.HoverResponse, error) {
22+
caps := lsproto.GetClientCapabilities(ctx)
23+
contentFormat := lsproto.PreferredMarkupKind(caps.TextDocument.Hover.ContentFormat)
24+
2225
program, file := l.getProgramAndFile(documentURI)
2326
node := astnav.GetTouchingPropertyName(file, int(l.converters.LineAndCharacterToPosition(file, position)))
2427
if node.Kind == ast.KindSourceFile {

internal/ls/signaturehelp.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,14 @@ func (l *LanguageService) ProvideSignatureHelp(
4343
documentURI lsproto.DocumentUri,
4444
position lsproto.Position,
4545
context *lsproto.SignatureHelpContext,
46-
clientOptions *lsproto.SignatureHelpClientCapabilities,
47-
docFormat lsproto.MarkupKind,
4846
) (lsproto.SignatureHelpResponse, error) {
4947
program, sourceFile := l.getProgramAndFile(documentURI)
5048
items := l.GetSignatureHelpItems(
5149
ctx,
5250
int(l.converters.LineAndCharacterToPosition(sourceFile, position)),
5351
program,
5452
sourceFile,
55-
context,
56-
clientOptions,
57-
docFormat)
53+
context)
5854
return lsproto.SignatureHelpOrNull{SignatureHelp: items}, nil
5955
}
6056

@@ -64,8 +60,6 @@ func (l *LanguageService) GetSignatureHelpItems(
6460
program *compiler.Program,
6561
sourceFile *ast.SourceFile,
6662
context *lsproto.SignatureHelpContext,
67-
clientOptions *lsproto.SignatureHelpClientCapabilities,
68-
docFormat lsproto.MarkupKind,
6963
) *lsproto.SignatureHelp {
7064
typeChecker, done := program.GetTypeCheckerForFile(ctx, sourceFile)
7165
defer done()
@@ -143,12 +137,12 @@ func (l *LanguageService) GetSignatureHelpItems(
143137

144138
// return typeChecker.runWithCancellationToken(cancellationToken, typeChecker =>
145139
if candidateInfo.candidateInfo != nil {
146-
return l.createSignatureHelpItems(candidateInfo.candidateInfo.candidates, candidateInfo.candidateInfo.resolvedSignature, argumentInfo, sourceFile, typeChecker, onlyUseSyntacticOwners, clientOptions, docFormat)
140+
return l.createSignatureHelpItems(ctx, candidateInfo.candidateInfo.candidates, candidateInfo.candidateInfo.resolvedSignature, argumentInfo, sourceFile, typeChecker, onlyUseSyntacticOwners)
147141
}
148-
return createTypeHelpItems(candidateInfo.typeInfo, argumentInfo, sourceFile, clientOptions, typeChecker)
142+
return createTypeHelpItems(candidateInfo.typeInfo, argumentInfo, sourceFile, typeChecker)
149143
}
150144

151-
func createTypeHelpItems(symbol *ast.Symbol, argumentInfo *argumentListInfo, sourceFile *ast.SourceFile, clientOptions *lsproto.SignatureHelpClientCapabilities, c *checker.Checker) *lsproto.SignatureHelp {
145+
func createTypeHelpItems(symbol *ast.Symbol, argumentInfo *argumentListInfo, sourceFile *ast.SourceFile, c *checker.Checker) *lsproto.SignatureHelp {
152146
typeParameters := c.GetLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol)
153147
if typeParameters == nil {
154148
return nil
@@ -205,7 +199,10 @@ func getTypeHelpItem(symbol *ast.Symbol, typeParameter []*checker.Type, enclosin
205199
}
206200
}
207201

208-
func (l *LanguageService) createSignatureHelpItems(candidates []*checker.Signature, resolvedSignature *checker.Signature, argumentInfo *argumentListInfo, sourceFile *ast.SourceFile, c *checker.Checker, useFullPrefix bool, clientOptions *lsproto.SignatureHelpClientCapabilities, docFormat lsproto.MarkupKind) *lsproto.SignatureHelp {
202+
func (l *LanguageService) createSignatureHelpItems(ctx context.Context, candidates []*checker.Signature, resolvedSignature *checker.Signature, argumentInfo *argumentListInfo, sourceFile *ast.SourceFile, c *checker.Checker, useFullPrefix bool) *lsproto.SignatureHelp {
203+
caps := lsproto.GetClientCapabilities(ctx)
204+
docFormat := lsproto.PreferredMarkupKind(caps.TextDocument.SignatureHelp.SignatureInformation.DocumentationFormat)
205+
209206
enclosingDeclaration := getEnclosingDeclarationFromInvocation(argumentInfo.invocation)
210207
if enclosingDeclaration == nil {
211208
return nil

internal/ls/string_completions.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func (l *LanguageService) getStringLiteralCompletions(
4646
position int,
4747
contextToken *ast.Node,
4848
compilerOptions *core.CompilerOptions,
49-
clientOptions *lsproto.CompletionClientCapabilities,
5049
) *lsproto.CompletionList {
5150
// !!! reference comment
5251
if IsInString(file, position, contextToken) {
@@ -65,7 +64,6 @@ func (l *LanguageService) getStringLiteralCompletions(
6564
file,
6665
position,
6766
compilerOptions,
68-
clientOptions,
6967
)
7068
}
7169
return nil
@@ -78,7 +76,6 @@ func (l *LanguageService) convertStringLiteralCompletions(
7876
file *ast.SourceFile,
7977
position int,
8078
options *core.CompilerOptions,
81-
clientOptions *lsproto.CompletionClientCapabilities,
8279
) *lsproto.CompletionList {
8380
if completion == nil {
8481
return nil
@@ -88,7 +85,7 @@ func (l *LanguageService) convertStringLiteralCompletions(
8885
switch {
8986
case completion.fromPaths != nil:
9087
completion := completion.fromPaths
91-
return l.convertPathCompletions(completion, file, position, clientOptions)
88+
return l.convertPathCompletions(ctx, completion, file, position)
9289
case completion.fromProperties != nil:
9390
completion := completion.fromProperties
9491
data := &completionDataData{
@@ -105,11 +102,10 @@ func (l *LanguageService) convertStringLiteralCompletions(
105102
position,
106103
file,
107104
options,
108-
clientOptions,
109105
)
110106
defaultCommitCharacters := getDefaultCommitCharacters(completion.hasIndexSignature)
111107
itemDefaults := l.setItemDefaults(
112-
clientOptions,
108+
ctx,
113109
position,
114110
file,
115111
items,
@@ -134,6 +130,7 @@ func (l *LanguageService) convertStringLiteralCompletions(
134130
items := core.Map(completion.types, func(t *checker.StringLiteralType) *lsproto.CompletionItem {
135131
name := printer.EscapeString(t.AsLiteralType().Value().(string), quoteChar)
136132
return l.createLSPCompletionItem(
133+
ctx,
137134
name,
138135
"", /*insertText*/
139136
"", /*filterText*/
@@ -145,7 +142,6 @@ func (l *LanguageService) convertStringLiteralCompletions(
145142
nil, /*labelDetails*/
146143
file,
147144
position,
148-
clientOptions,
149145
false, /*isMemberCompletion*/
150146
false, /*isSnippet*/
151147
false, /*hasAction*/
@@ -156,7 +152,7 @@ func (l *LanguageService) convertStringLiteralCompletions(
156152
})
157153
defaultCommitCharacters := getDefaultCommitCharacters(completion.isNewIdentifier)
158154
itemDefaults := l.setItemDefaults(
159-
clientOptions,
155+
ctx,
160156
position,
161157
file,
162158
items,
@@ -174,16 +170,17 @@ func (l *LanguageService) convertStringLiteralCompletions(
174170
}
175171

176172
func (l *LanguageService) convertPathCompletions(
173+
ctx context.Context,
177174
pathCompletions []*pathCompletion,
178175
file *ast.SourceFile,
179176
position int,
180-
clientOptions *lsproto.CompletionClientCapabilities,
181177
) *lsproto.CompletionList {
182178
isNewIdentifierLocation := true // The user may type in a path that doesn't yet exist, creating a "new identifier" with respect to the collection of identifiers the server is aware of.
183179
defaultCommitCharacters := getDefaultCommitCharacters(isNewIdentifierLocation)
184180
items := core.Map(pathCompletions, func(pathCompletion *pathCompletion) *lsproto.CompletionItem {
185181
replacementSpan := l.createLspRangeFromBounds(pathCompletion.textRange.Pos(), pathCompletion.textRange.End(), file)
186182
return l.createLSPCompletionItem(
183+
ctx,
187184
pathCompletion.name,
188185
"", /*insertText*/
189186
"", /*filterText*/
@@ -195,7 +192,6 @@ func (l *LanguageService) convertPathCompletions(
195192
nil, /*labelDetails*/
196193
file,
197194
position,
198-
clientOptions,
199195
false, /*isMemberCompletion*/
200196
false, /*isSnippet*/
201197
false, /*hasAction*/
@@ -205,7 +201,7 @@ func (l *LanguageService) convertPathCompletions(
205201
)
206202
})
207203
itemDefaults := l.setItemDefaults(
208-
clientOptions,
204+
ctx,
209205
position,
210206
file,
211207
items,

internal/ls/symbols.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
"github.com/microsoft/typescript-go/internal/stringutil"
1919
)
2020

21-
func (l *LanguageService) ProvideDocumentSymbols(ctx context.Context, documentURI lsproto.DocumentUri, hierarchicalSupport bool) (lsproto.DocumentSymbolResponse, error) {
21+
func (l *LanguageService) ProvideDocumentSymbols(ctx context.Context, documentURI lsproto.DocumentUri) (lsproto.DocumentSymbolResponse, error) {
2222
_, file := l.getProgramAndFile(documentURI)
23-
if hierarchicalSupport {
23+
if lsproto.GetClientCapabilities(ctx).TextDocument.DocumentSymbol.HierarchicalDocumentSymbolSupport {
2424
symbols := l.getDocumentSymbolsForChildren(ctx, file.AsNode())
2525
return lsproto.SymbolInformationsOrDocumentSymbolsOrNull{DocumentSymbols: &symbols}, nil
2626
}

0 commit comments

Comments
 (0)