internal/lsp: use the view options, not the session options

Previous changes to the config mechanism made the config options
per-view, not per-session. We should now make sure to obey config
changes per-view. This does not fix the configuration handling for
"watchChangedFile" however. This should be done in a future CL.

Change-Id: I73f6236386c36d2587fdb9c0601670833a4366c3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/194818
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2019-09-11 18:21:01 -04:00
parent 63a3583f64
commit c7d52e45e2
9 changed files with 41 additions and 26 deletions

View File

@ -117,6 +117,10 @@ func (v *view) Options() source.Options {
return v.options return v.options
} }
func (v *view) SetOptions(options source.Options) {
v.options = options
}
// Config returns the configuration used for the view's interaction with the // Config returns the configuration used for the view's interaction with the
// go/packages API. It is shared across all views. // go/packages API. It is shared across all views.
func (v *view) Config(ctx context.Context) *packages.Config { func (v *view) Config(ctx context.Context) *packages.Config {

View File

@ -47,7 +47,7 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara
// Determine the supported actions for this file kind. // Determine the supported actions for this file kind.
fileKind := f.Handle(ctx).Kind() fileKind := f.Handle(ctx).Kind()
supportedCodeActions, ok := s.session.Options().SupportedCodeActions[fileKind] supportedCodeActions, ok := view.Options().SupportedCodeActions[fileKind]
if !ok { if !ok {
return nil, fmt.Errorf("no supported code actions for %v file kind", fileKind) return nil, fmt.Errorf("no supported code actions for %v file kind", fileKind)
} }

View File

@ -19,7 +19,7 @@ import (
func (s *Server) completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) { func (s *Server) completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
uri := span.NewURI(params.TextDocument.URI) uri := span.NewURI(params.TextDocument.URI)
view := s.session.ViewOf(uri) view := s.session.ViewOf(uri)
options := s.session.Options() options := view.Options()
f, err := getGoFile(ctx, view, uri) f, err := getGoFile(ctx, view, uri)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -20,7 +20,7 @@ const (
) )
// Version is a manually-updated mechanism for tracking versions. // Version is a manually-updated mechanism for tracking versions.
var Version = "v0.1.4" var Version = "v0.1.5"
// This writes the version and environment information to a writer. // This writes the version and environment information to a writer.
func PrintVersionInfo(w io.Writer, verbose bool, mode PrintMode) { func PrintVersionInfo(w io.Writer, verbose bool, mode PrintMode) {

View File

@ -27,7 +27,7 @@ func (s *Server) Diagnostics(ctx context.Context, view source.View, uri span.URI
if !ok { if !ok {
return return
} }
reports, err := source.Diagnostics(ctx, view, gof, s.session.Options().DisabledAnalyses) reports, err := source.Diagnostics(ctx, view, gof, view.Options().DisabledAnalyses)
if err != nil { if err != nil {
log.Error(ctx, "failed to compute diagnostics", err, telemetry.File) log.Error(ctx, "failed to compute diagnostics", err, telemetry.File)
return return

View File

@ -15,7 +15,7 @@ func (s *Server) foldingRange(ctx context.Context, params *protocol.FoldingRange
if err != nil { if err != nil {
return nil, err return nil, err
} }
ranges, err := source.FoldingRange(ctx, view, f, s.session.Options().LineFoldingOnly) ranges, err := source.FoldingRange(ctx, view, f, view.Options().LineFoldingOnly)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -34,15 +34,14 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
if err != nil { if err != nil {
return nil, err return nil, err
} }
contents := s.toProtocolHoverContents(ctx, hover) contents := s.toProtocolHoverContents(ctx, hover, view.Options())
return &protocol.Hover{ return &protocol.Hover{
Contents: contents, Contents: contents,
Range: &rng, Range: &rng,
}, nil }, nil
} }
func (s *Server) toProtocolHoverContents(ctx context.Context, h *source.HoverInformation) protocol.MarkupContent { func (s *Server) toProtocolHoverContents(ctx context.Context, h *source.HoverInformation, options source.Options) protocol.MarkupContent {
options := s.session.Options()
content := protocol.MarkupContent{ content := protocol.MarkupContent{
Kind: options.PreferredContentFormat, Kind: options.PreferredContentFormat,
} }

View File

@ -110,14 +110,14 @@ func (r *runner) Diagnostics(t *testing.T, data tests.Diagnostics) {
} }
func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests.CompletionSnippets, items tests.CompletionItems) { func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests.CompletionSnippets, items tests.CompletionItems) {
original := r.server.session.Options()
modified := original
defer func() { r.server.session.SetOptions(original) }()
// Set this as a default.
modified.Completion.Documentation = true
for src, test := range data { for src, test := range data {
view := r.server.session.ViewOf(src.URI())
original := view.Options()
modified := original
// Set this as a default.
modified.Completion.Documentation = true
var want []source.CompletionItem var want []source.CompletionItem
for _, pos := range test.CompletionItems { for _, pos := range test.CompletionItems {
want = append(want, *items[pos]) want = append(want, *items[pos])
@ -126,7 +126,7 @@ func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests
modified.Completion.Deep = strings.Contains(string(src.URI()), "deepcomplete") modified.Completion.Deep = strings.Contains(string(src.URI()), "deepcomplete")
modified.Completion.FuzzyMatching = strings.Contains(string(src.URI()), "fuzzymatch") modified.Completion.FuzzyMatching = strings.Contains(string(src.URI()), "fuzzymatch")
modified.Completion.Unimported = strings.Contains(string(src.URI()), "unimported") modified.Completion.Unimported = strings.Contains(string(src.URI()), "unimported")
r.server.session.SetOptions(modified) view.SetOptions(modified)
list := r.runCompletion(t, src) list := r.runCompletion(t, src)
@ -149,15 +149,22 @@ func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests
t.Errorf("%s: %s", src, msg) t.Errorf("%s: %s", src, msg)
} }
} }
view.SetOptions(original)
} }
modified.InsertTextFormat = protocol.SnippetTextFormat
for _, usePlaceholders := range []bool{true, false} { for _, usePlaceholders := range []bool{true, false} {
for src, want := range snippets { for src, want := range snippets {
view := r.server.session.ViewOf(src.URI())
original := view.Options()
modified := original
modified.InsertTextFormat = protocol.SnippetTextFormat
modified.Completion.Deep = strings.Contains(string(src.URI()), "deepcomplete") modified.Completion.Deep = strings.Contains(string(src.URI()), "deepcomplete")
modified.Completion.FuzzyMatching = strings.Contains(string(src.URI()), "fuzzymatch") modified.Completion.FuzzyMatching = strings.Contains(string(src.URI()), "fuzzymatch")
modified.Completion.Unimported = strings.Contains(string(src.URI()), "unimported") modified.Completion.Unimported = strings.Contains(string(src.URI()), "unimported")
modified.Completion.Placeholders = usePlaceholders modified.Completion.Placeholders = usePlaceholders
r.server.session.SetOptions(modified) view.SetOptions(modified)
list := r.runCompletion(t, src) list := r.runCompletion(t, src)
@ -181,6 +188,7 @@ func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests
if expected != got.TextEdit.NewText { if expected != got.TextEdit.NewText {
t.Errorf("%s: expected snippet %q, got %q", src, expected, got.TextEdit.NewText) t.Errorf("%s: expected snippet %q, got %q", src, expected, got.TextEdit.NewText)
} }
view.SetOptions(original)
} }
} }
} }
@ -306,16 +314,15 @@ func summarizeCompletionItems(i int, want []source.CompletionItem, got []protoco
} }
func (r *runner) FoldingRange(t *testing.T, data tests.FoldingRanges) { func (r *runner) FoldingRange(t *testing.T, data tests.FoldingRanges) {
original := r.server.session.Options()
modified := original
defer func() { r.server.session.SetOptions(original) }()
for _, spn := range data { for _, spn := range data {
uri := spn.URI() uri := spn.URI()
view := r.server.session.ViewOf(uri)
original := view.Options()
modified := original
// Test all folding ranges. // Test all folding ranges.
modified.LineFoldingOnly = false modified.LineFoldingOnly = false
r.server.session.SetOptions(modified) view.SetOptions(modified)
ranges, err := r.server.FoldingRange(r.ctx, &protocol.FoldingRangeParams{ ranges, err := r.server.FoldingRange(r.ctx, &protocol.FoldingRangeParams{
TextDocument: protocol.TextDocumentIdentifier{ TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.NewURI(uri), URI: protocol.NewURI(uri),
@ -329,7 +336,7 @@ func (r *runner) FoldingRange(t *testing.T, data tests.FoldingRanges) {
// Test folding ranges with lineFoldingOnly = true. // Test folding ranges with lineFoldingOnly = true.
modified.LineFoldingOnly = true modified.LineFoldingOnly = true
r.server.session.SetOptions(modified) view.SetOptions(modified)
ranges, err = r.server.FoldingRange(r.ctx, &protocol.FoldingRangeParams{ ranges, err = r.server.FoldingRange(r.ctx, &protocol.FoldingRangeParams{
TextDocument: protocol.TextDocumentIdentifier{ TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.NewURI(uri), URI: protocol.NewURI(uri),
@ -340,7 +347,7 @@ func (r *runner) FoldingRange(t *testing.T, data tests.FoldingRanges) {
continue continue
} }
r.foldingRanges(t, "foldingRange-lineFolding", uri, ranges) r.foldingRanges(t, "foldingRange-lineFolding", uri, ranges)
view.SetOptions(original)
} }
} }

View File

@ -245,8 +245,13 @@ type View interface {
// Note: the process env contains cached module and filesystem state. // Note: the process env contains cached module and filesystem state.
RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error, opts *imports.Options) error RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error, opts *imports.Options) error
// Options returns a copy of the ViewOptions for this view. // Options returns a copy of the Options for this view.
Options() Options Options() Options
// SetOptions sets the options of this view to new values.
// Warning: Do not use this, unless in a test.
// This function does not correctly invalidate the view when needed.
SetOptions(Options)
} }
// File represents a source file of any type. // File represents a source file of any type.