Skip to content

Commit 693d7fe

Browse files
committed
gopls/internal/settings: simplify SetOptions
This change simplifies the SetOptions machinery by eliminating the (public) OptionsResult and Options types and their methods, and using simpler standalone functions of these forms: setT(dest *T, value any) error asT(value any) (T, error) The code is clearer and significantly shorter. Details: - rename SetOptions to Options.Set. - return only the errors, not the OptionsResults; server.handleOptionResult renamed handleOptionErrors. - remove error result from server.handleOptionResult, per preexisting TODO. - add missing doc comments. - use JSON terminology in error messages. Note, minor behavior changes: - the buildFlags and directoryFilters flags now use asStringSlice (per the preexisting TODO), and also templateExtensions, but this replaces Sprint(x) with asString(x), which is strictly speaking an incompatible change. Change-Id: Ib2169ba8e1db1a34e9bc269e6e8cef3a6763e6e6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/592536 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 977f6f7 commit 693d7fe

File tree

5 files changed

+298
-339
lines changed

5 files changed

+298
-339
lines changed

gopls/internal/cache/session_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,8 @@ replace (
336336
for _, f := range test.folders {
337337
opts := settings.DefaultOptions()
338338
if f.options != nil {
339-
results := settings.SetOptions(opts, f.options(dir))
340-
for _, r := range results {
341-
if r.Error != nil {
342-
t.Fatalf("setting option %v: %v", r.Name, r.Error)
343-
}
339+
for _, err := range opts.Set(f.options(dir)) {
340+
t.Fatal(err)
344341
}
345342
}
346343
env, err := FetchGoEnv(ctx, toURI(f.dir), opts)

gopls/internal/server/general.go

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ package server
1010
import (
1111
"context"
1212
"encoding/json"
13+
"errors"
1314
"fmt"
1415
"go/build"
15-
"log"
1616
"os"
1717
"path"
1818
"path/filepath"
@@ -69,13 +69,10 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ
6969
s.progress.SetSupportsWorkDoneProgress(params.Capabilities.Window.WorkDoneProgress)
7070

7171
options := s.Options().Clone()
72-
// TODO(rfindley): remove the error return from handleOptionResults, and
73-
// eliminate this defer.
72+
// TODO(rfindley): eliminate this defer.
7473
defer func() { s.SetOptions(options) }()
7574

76-
if err := s.handleOptionResults(ctx, settings.SetOptions(options, params.InitializationOptions)); err != nil {
77-
return nil, err
78-
}
75+
s.handleOptionErrors(ctx, options.Set(params.InitializationOptions))
7976
options.ForClientCapabilities(params.ClientInfo, params.Capabilities)
8077

8178
if options.ShowBugReports {
@@ -85,11 +82,7 @@ func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitializ
8582
Type: protocol.Error,
8683
Message: fmt.Sprintf("A bug occurred on the server: %s\nLocation:%s", b.Description, b.Key),
8784
}
88-
go func() {
89-
if err := s.eventuallyShowMessage(context.Background(), msg); err != nil {
90-
log.Printf("error showing bug: %v", err)
91-
}
92-
}()
85+
go s.eventuallyShowMessage(context.Background(), msg)
9386
})
9487
}
9588

@@ -510,33 +503,30 @@ func (s *server) fetchFolderOptions(ctx context.Context, folder protocol.Documen
510503

511504
opts = opts.Clone()
512505
for _, config := range configs {
513-
if err := s.handleOptionResults(ctx, settings.SetOptions(opts, config)); err != nil {
514-
return nil, err
515-
}
506+
s.handleOptionErrors(ctx, opts.Set(config))
516507
}
517508
return opts, nil
518509
}
519510

520-
func (s *server) eventuallyShowMessage(ctx context.Context, msg *protocol.ShowMessageParams) error {
511+
func (s *server) eventuallyShowMessage(ctx context.Context, msg *protocol.ShowMessageParams) {
521512
s.stateMu.Lock()
522513
defer s.stateMu.Unlock()
523514
if s.state == serverInitialized {
524-
return s.client.ShowMessage(ctx, msg)
515+
_ = s.client.ShowMessage(ctx, msg) // ignore error
525516
}
526517
s.notifications = append(s.notifications, msg)
527-
return nil
528518
}
529519

530-
func (s *server) handleOptionResults(ctx context.Context, results settings.OptionResults) error {
531-
var warnings, errors []string
532-
for _, result := range results {
533-
switch result.Error.(type) {
534-
case nil:
535-
// nothing to do
536-
case *settings.SoftError:
537-
warnings = append(warnings, result.Error.Error())
538-
default:
539-
errors = append(errors, result.Error.Error())
520+
func (s *server) handleOptionErrors(ctx context.Context, optionErrors []error) {
521+
var warnings, errs []string
522+
for _, err := range optionErrors {
523+
if err == nil {
524+
panic("nil error passed to handleOptionErrors")
525+
}
526+
if errors.Is(err, new(settings.SoftError)) {
527+
warnings = append(warnings, err.Error())
528+
} else {
529+
errs = append(errs, err.Error())
540530
}
541531
}
542532

@@ -548,10 +538,10 @@ func (s *server) handleOptionResults(ctx context.Context, results settings.Optio
548538
// individual viewsettings.
549539
var msgs []string
550540
msgType := protocol.Warning
551-
if len(errors) > 0 {
541+
if len(errs) > 0 {
552542
msgType = protocol.Error
553-
sort.Strings(errors)
554-
msgs = append(msgs, errors...)
543+
sort.Strings(errs)
544+
msgs = append(msgs, errs...)
555545
}
556546
if len(warnings) > 0 {
557547
sort.Strings(warnings)
@@ -565,10 +555,8 @@ func (s *server) handleOptionResults(ctx context.Context, results settings.Optio
565555
Type: msgType,
566556
Message: combined,
567557
}
568-
return s.eventuallyShowMessage(ctx, params)
558+
s.eventuallyShowMessage(ctx, params)
569559
}
570-
571-
return nil
572560
}
573561

574562
// fileOf returns the file for a given URI and its snapshot.

0 commit comments

Comments
 (0)