Skip to content

Commit 0dff9de

Browse files
committed
didchangeconfigure
1 parent 54168cb commit 0dff9de

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed

internal/fourslash/fourslash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func sendRequest[Params, Resp any](t *testing.T, f *FourslashTest, info lsproto.
282282
req := lsproto.ResponseMessage{
283283
ID: req.ID,
284284
JSONRPC: req.JSONRPC,
285-
Result: []any{&f.userPreferences},
285+
Result: []any{f.userPreferences},
286286
}
287287
f.writeMsg(t, req.Message())
288288
resp = f.readMsg(t)

internal/fourslash/tests/autoImportCompletion_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ a/**/
4343
f.VerifyCompletions(t, "", &fourslash.CompletionsExpectedList{
4444
UserPreferences: &ls.UserPreferences{
4545
// completion autoimport preferences off; this tests if fourslash server communication correctly registers changes in user preferences
46+
IncludeCompletionsForModuleExports: core.TSUnknown,
47+
IncludeCompletionsForImportStatements: core.TSUnknown,
4648
},
4749
IsIncomplete: false,
4850
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{

internal/ls/userpreferences.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,21 @@ func (p *UserPreferences) ModuleSpecifierPreferences() modulespecifiers.UserPref
349349
}
350350

351351
// ------ Parsing Config Response -------
352-
func (p *UserPreferences) Parse(config map[string]interface{}) {
352+
353+
// returns non-nil if should break loop
354+
func (p *UserPreferences) Parse(item any) *UserPreferences {
355+
if item == nil {
356+
// continue
357+
} else if config, ok := item.(map[string]any); ok {
358+
p.parseWorker(config)
359+
} else if item, ok := item.(*UserPreferences); ok {
360+
// case for fourslash
361+
return item.CopyOrDefault()
362+
}
363+
return nil
364+
}
365+
366+
func (p *UserPreferences) parseWorker(config map[string]interface{}) {
353367
// Process unstable preferences first so that they do not overwrite stable properties
354368
if unstable, ok := config["unstable"]; ok {
355369
// unstable properties must be named the same as userPreferences
@@ -526,14 +540,21 @@ func parseBoolWithDefault(val any, defaultV bool) bool {
526540
return defaultV
527541
}
528542

543+
func parseIntWithDefault(val any, defaultV int) int {
544+
if v, ok := val.(int); ok {
545+
return v
546+
}
547+
return defaultV
548+
}
549+
529550
func (p *UserPreferences) set(name string, value any) {
530551
switch strings.ToLower(name) {
531552
case "quotePreference":
532553
p.QuotePreference = parseQuotePreference(value)
533554
case "lazyconfiguredprojectsfromexternalproject":
534555
p.LazyConfiguredProjectsFromExternalProject = parseBoolWithDefault(value, false)
535-
// case "maximumhoverlength":
536-
// p.MaximumHoverLength = tsoptions.ParseInt(value, 500)
556+
case "maximumhoverlength":
557+
p.MaximumHoverLength = parseIntWithDefault(value, 500)
537558
case "includecompletionsformoduleexports":
538559
p.IncludeCompletionsForModuleExports = tsoptions.ParseTristate(value)
539560
case "includecompletionsforimportstatements":

internal/lsp/server.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (s *Server) RefreshDiagnostics(ctx context.Context) error {
218218
return nil
219219
}
220220

221-
func (s *Server) Configure(ctx context.Context) (*ls.UserPreferences, error) {
221+
func (s *Server) RequestConfiguration(ctx context.Context) (*ls.UserPreferences, error) {
222222
result, err := s.sendRequest(ctx, lsproto.MethodWorkspaceConfiguration, &lsproto.ConfigurationParams{
223223
Items: []*lsproto.ConfigurationItem{
224224
{
@@ -233,14 +233,8 @@ func (s *Server) Configure(ctx context.Context) (*ls.UserPreferences, error) {
233233
s.Log(fmt.Sprintf("\n\nconfiguration: %+v, %T\n\n", configs, configs))
234234
userPreferences := ls.NewDefaultUserPreferences()
235235
for _, item := range configs {
236-
if item == nil {
237-
// continue
238-
} else if config, ok := item.(map[string]any); ok {
239-
userPreferences.Parse(config)
240-
} else if item, ok := item.(ls.UserPreferences); ok {
241-
// case for fourslash
242-
userPreferences = &item
243-
break
236+
if parsed := userPreferences.Parse(item); parsed != nil {
237+
return parsed, nil
244238
}
245239
}
246240
return userPreferences, nil
@@ -696,11 +690,15 @@ func (s *Server) handleInitialized(ctx context.Context, params *lsproto.Initiali
696690
NpmExecutor: s,
697691
ParseCache: s.parseCache,
698692
})
699-
userPreferences, err := s.Configure(ctx)
700-
if err != nil {
701-
return err
693+
694+
// request userPreferences if not provided at initialization
695+
if s.session.UserPreferences() == nil {
696+
userPreferences, err := s.RequestConfiguration(ctx)
697+
if err != nil {
698+
return err
699+
}
700+
s.session.Configure(userPreferences)
702701
}
703-
s.session.Configure(userPreferences)
704702
// !!! temporary; remove when we have `handleDidChangeConfiguration`/implicit project config support
705703
if s.compilerOptionsForInferredProjects != nil {
706704
s.session.DidChangeCompilerOptionsForInferredProjects(ctx, s.compilerOptionsForInferredProjects)
@@ -719,12 +717,11 @@ func (s *Server) handleExit(ctx context.Context, params any) error {
719717
}
720718

721719
func (s *Server) handleDidChangeWorkspaceConfiguration(ctx context.Context, params *lsproto.DidChangeConfigurationParams) error {
722-
// !!! update user preferences
723-
// !!! only usable by fourslash
724-
if item, ok := params.Settings.(*ls.UserPreferences); ok {
725-
// case for fourslash
726-
s.session.Configure(item)
720+
userPreferences := s.session.UserPreferences().CopyOrDefault()
721+
if parsed := userPreferences.Parse(params.Settings); parsed != nil {
722+
userPreferences = parsed
727723
}
724+
s.session.Configure(userPreferences)
728725
return nil
729726
}
730727

internal/project/session.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func (s *Session) GetCurrentDirectory() string {
176176
return s.options.CurrentDirectory
177177
}
178178

179+
func (s *Session) UserPreferences() *ls.UserPreferences {
180+
return s.userPreferences
181+
}
182+
179183
// Trace implements module.ResolutionHost
180184
func (s *Session) Trace(msg string) {
181185
panic("ATA module resolution should not use tracing")

0 commit comments

Comments
 (0)