Skip to content

Commit cea534e

Browse files
Handle configuration changes in LSP for 'typescript.*' options. (#2088)
1 parent 4329af4 commit cea534e

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

internal/fourslash/fourslash.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -365,19 +365,24 @@ func sendRequest[Params, Resp any](t *testing.T, f *FourslashTest, info lsproto.
365365
// !!! remove if `config` is handled in initialization and there are no other server-initiated requests
366366
if resp.Kind == lsproto.MessageKindRequest {
367367
req := resp.AsRequest()
368-
switch req.Method {
369-
case lsproto.MethodWorkspaceConfiguration:
370-
req := lsproto.ResponseMessage{
371-
ID: req.ID,
372-
JSONRPC: req.JSONRPC,
373-
Result: []any{f.userPreferences},
374-
}
375-
f.writeMsg(t, req.Message())
376-
resp = f.readMsg(t)
377-
default:
378-
// other types of requests not yet used in fourslash; implement them if needed
379-
t.Fatalf("Unexpected request received: %s", req.Method)
368+
369+
assert.Equal(t, req.Method, lsproto.MethodWorkspaceConfiguration, "Unexpected request received: %s", req.Method)
370+
res := lsproto.ResponseMessage{
371+
ID: req.ID,
372+
JSONRPC: req.JSONRPC,
373+
Result: []any{f.userPreferences},
380374
}
375+
f.writeMsg(t, res.Message())
376+
req = f.readMsg(t).AsRequest()
377+
378+
assert.Equal(t, req.Method, lsproto.MethodClientRegisterCapability, "Unexpected request received: %s", req.Method)
379+
res = lsproto.ResponseMessage{
380+
ID: req.ID,
381+
JSONRPC: req.JSONRPC,
382+
Result: lsproto.Null{},
383+
}
384+
f.writeMsg(t, res.Message())
385+
resp = f.readMsg(t)
381386
}
382387

383388
if resp == nil {
@@ -413,9 +418,16 @@ func (f *FourslashTest) readMsg(t *testing.T) *lsproto.Message {
413418
}
414419

415420
func (f *FourslashTest) Configure(t *testing.T, config *lsutil.UserPreferences) {
421+
// !!!
422+
// Callers to this function may need to consider
423+
// sending a more specific configuration for 'javascript'
424+
// or 'js/ts' as well. For now, we only send 'typescript',
425+
// and most tests probably just want this.
416426
f.userPreferences = config
417427
sendNotification(t, f, lsproto.WorkspaceDidChangeConfigurationInfo, &lsproto.DidChangeConfigurationParams{
418-
Settings: config,
428+
Settings: map[string]any{
429+
"typescript": config,
430+
},
419431
})
420432
}
421433

internal/lsp/server.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,9 +742,29 @@ func (s *Server) handleInitialized(ctx context.Context, params *lsproto.Initiali
742742
return err
743743
}
744744
s.session.InitializeWithConfig(userPreferences)
745+
746+
_, err = s.sendRequest(ctx, lsproto.MethodClientRegisterCapability, &lsproto.RegistrationParams{
747+
Registrations: []*lsproto.Registration{
748+
{
749+
Id: "typescript-config-watch-id",
750+
Method: string(lsproto.MethodWorkspaceDidChangeConfiguration),
751+
RegisterOptions: ptrTo(any(lsproto.DidChangeConfigurationRegistrationOptions{
752+
Section: &lsproto.StringOrStrings{
753+
// !!! Both the 'javascript' and 'js/ts' scopes need to be watched for settings as well.
754+
Strings: &[]string{"typescript"},
755+
},
756+
})),
757+
},
758+
},
759+
})
760+
if err != nil {
761+
return fmt.Errorf("failed to register configuration change watcher: %w", err)
762+
}
745763
}
746764

747-
// !!! temporary; remove when we have `handleDidChangeConfiguration`/implicit project config support
765+
// !!! temporary.
766+
// Remove when we have `handleDidChangeConfiguration`/implicit project config support
767+
// derived from 'js/ts.implicitProjectConfig.*'.
748768
if s.compilerOptionsForInferredProjects != nil {
749769
s.session.DidChangeCompilerOptionsForInferredProjects(ctx, s.compilerOptionsForInferredProjects)
750770
}
@@ -762,9 +782,14 @@ func (s *Server) handleExit(ctx context.Context, params any) error {
762782
}
763783

764784
func (s *Server) handleDidChangeWorkspaceConfiguration(ctx context.Context, params *lsproto.DidChangeConfigurationParams) error {
765-
// !!! only implemented because needed for fourslash
785+
settings, ok := params.Settings.(map[string]any)
786+
if !ok {
787+
return nil
788+
}
789+
// !!! Both the 'javascript' and 'js/ts' scopes need to be checked for settings as well.
790+
tsSettings := settings["typescript"]
766791
userPreferences := s.session.UserPreferences()
767-
if parsed := userPreferences.Parse(params.Settings); parsed != nil {
792+
if parsed := userPreferences.Parse(tsSettings); parsed != nil {
768793
userPreferences = parsed
769794
}
770795
s.session.Configure(userPreferences)

0 commit comments

Comments
 (0)