Skip to content

Commit c7d52e4

Browse files
committed
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 <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 63a3583 commit c7d52e4

File tree

9 files changed

+40
-25
lines changed

9 files changed

+40
-25
lines changed

internal/lsp/cache/view.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ func (v *view) Options() source.Options {
117117
return v.options
118118
}
119119

120+
func (v *view) SetOptions(options source.Options) {
121+
v.options = options
122+
}
123+
120124
// Config returns the configuration used for the view's interaction with the
121125
// go/packages API. It is shared across all views.
122126
func (v *view) Config(ctx context.Context) *packages.Config {

internal/lsp/code_action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara
4747

4848
// Determine the supported actions for this file kind.
4949
fileKind := f.Handle(ctx).Kind()
50-
supportedCodeActions, ok := s.session.Options().SupportedCodeActions[fileKind]
50+
supportedCodeActions, ok := view.Options().SupportedCodeActions[fileKind]
5151
if !ok {
5252
return nil, fmt.Errorf("no supported code actions for %v file kind", fileKind)
5353
}

internal/lsp/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
func (s *Server) completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
2020
uri := span.NewURI(params.TextDocument.URI)
2121
view := s.session.ViewOf(uri)
22-
options := s.session.Options()
22+
options := view.Options()
2323
f, err := getGoFile(ctx, view, uri)
2424
if err != nil {
2525
return nil, err

internal/lsp/debug/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
)
2121

2222
// Version is a manually-updated mechanism for tracking versions.
23-
var Version = "v0.1.4"
23+
var Version = "v0.1.5"
2424

2525
// This writes the version and environment information to a writer.
2626
func PrintVersionInfo(w io.Writer, verbose bool, mode PrintMode) {

internal/lsp/diagnostics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (s *Server) Diagnostics(ctx context.Context, view source.View, uri span.URI
2727
if !ok {
2828
return
2929
}
30-
reports, err := source.Diagnostics(ctx, view, gof, s.session.Options().DisabledAnalyses)
30+
reports, err := source.Diagnostics(ctx, view, gof, view.Options().DisabledAnalyses)
3131
if err != nil {
3232
log.Error(ctx, "failed to compute diagnostics", err, telemetry.File)
3333
return

internal/lsp/folding_range.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (s *Server) foldingRange(ctx context.Context, params *protocol.FoldingRange
1515
if err != nil {
1616
return nil, err
1717
}
18-
ranges, err := source.FoldingRange(ctx, view, f, s.session.Options().LineFoldingOnly)
18+
ranges, err := source.FoldingRange(ctx, view, f, view.Options().LineFoldingOnly)
1919
if err != nil {
2020
return nil, err
2121
}

internal/lsp/hover.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
3434
if err != nil {
3535
return nil, err
3636
}
37-
contents := s.toProtocolHoverContents(ctx, hover)
37+
contents := s.toProtocolHoverContents(ctx, hover, view.Options())
3838
return &protocol.Hover{
3939
Contents: contents,
4040
Range: &rng,
4141
}, nil
4242
}
4343

44-
func (s *Server) toProtocolHoverContents(ctx context.Context, h *source.HoverInformation) protocol.MarkupContent {
45-
options := s.session.Options()
44+
func (s *Server) toProtocolHoverContents(ctx context.Context, h *source.HoverInformation, options source.Options) protocol.MarkupContent {
4645
content := protocol.MarkupContent{
4746
Kind: options.PreferredContentFormat,
4847
}

internal/lsp/lsp_test.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ func (r *runner) Diagnostics(t *testing.T, data tests.Diagnostics) {
110110
}
111111

112112
func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests.CompletionSnippets, items tests.CompletionItems) {
113-
original := r.server.session.Options()
114-
modified := original
115-
defer func() { r.server.session.SetOptions(original) }()
113+
for src, test := range data {
114+
view := r.server.session.ViewOf(src.URI())
115+
original := view.Options()
116+
modified := original
116117

117-
// Set this as a default.
118-
modified.Completion.Documentation = true
118+
// Set this as a default.
119+
modified.Completion.Documentation = true
119120

120-
for src, test := range data {
121121
var want []source.CompletionItem
122122
for _, pos := range test.CompletionItems {
123123
want = append(want, *items[pos])
@@ -126,7 +126,7 @@ func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests
126126
modified.Completion.Deep = strings.Contains(string(src.URI()), "deepcomplete")
127127
modified.Completion.FuzzyMatching = strings.Contains(string(src.URI()), "fuzzymatch")
128128
modified.Completion.Unimported = strings.Contains(string(src.URI()), "unimported")
129-
r.server.session.SetOptions(modified)
129+
view.SetOptions(modified)
130130

131131
list := r.runCompletion(t, src)
132132

@@ -149,15 +149,22 @@ func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests
149149
t.Errorf("%s: %s", src, msg)
150150
}
151151
}
152+
view.SetOptions(original)
152153
}
153-
modified.InsertTextFormat = protocol.SnippetTextFormat
154+
154155
for _, usePlaceholders := range []bool{true, false} {
156+
155157
for src, want := range snippets {
158+
view := r.server.session.ViewOf(src.URI())
159+
original := view.Options()
160+
modified := original
161+
162+
modified.InsertTextFormat = protocol.SnippetTextFormat
156163
modified.Completion.Deep = strings.Contains(string(src.URI()), "deepcomplete")
157164
modified.Completion.FuzzyMatching = strings.Contains(string(src.URI()), "fuzzymatch")
158165
modified.Completion.Unimported = strings.Contains(string(src.URI()), "unimported")
159166
modified.Completion.Placeholders = usePlaceholders
160-
r.server.session.SetOptions(modified)
167+
view.SetOptions(modified)
161168

162169
list := r.runCompletion(t, src)
163170

@@ -181,6 +188,7 @@ func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests
181188
if expected != got.TextEdit.NewText {
182189
t.Errorf("%s: expected snippet %q, got %q", src, expected, got.TextEdit.NewText)
183190
}
191+
view.SetOptions(original)
184192
}
185193
}
186194
}
@@ -306,16 +314,15 @@ func summarizeCompletionItems(i int, want []source.CompletionItem, got []protoco
306314
}
307315

308316
func (r *runner) FoldingRange(t *testing.T, data tests.FoldingRanges) {
309-
original := r.server.session.Options()
310-
modified := original
311-
defer func() { r.server.session.SetOptions(original) }()
312-
313317
for _, spn := range data {
314318
uri := spn.URI()
319+
view := r.server.session.ViewOf(uri)
320+
original := view.Options()
321+
modified := original
315322

316323
// Test all folding ranges.
317324
modified.LineFoldingOnly = false
318-
r.server.session.SetOptions(modified)
325+
view.SetOptions(modified)
319326
ranges, err := r.server.FoldingRange(r.ctx, &protocol.FoldingRangeParams{
320327
TextDocument: protocol.TextDocumentIdentifier{
321328
URI: protocol.NewURI(uri),
@@ -329,7 +336,7 @@ func (r *runner) FoldingRange(t *testing.T, data tests.FoldingRanges) {
329336

330337
// Test folding ranges with lineFoldingOnly = true.
331338
modified.LineFoldingOnly = true
332-
r.server.session.SetOptions(modified)
339+
view.SetOptions(modified)
333340
ranges, err = r.server.FoldingRange(r.ctx, &protocol.FoldingRangeParams{
334341
TextDocument: protocol.TextDocumentIdentifier{
335342
URI: protocol.NewURI(uri),
@@ -340,7 +347,7 @@ func (r *runner) FoldingRange(t *testing.T, data tests.FoldingRanges) {
340347
continue
341348
}
342349
r.foldingRanges(t, "foldingRange-lineFolding", uri, ranges)
343-
350+
view.SetOptions(original)
344351
}
345352
}
346353

internal/lsp/source/view.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,13 @@ type View interface {
245245
// Note: the process env contains cached module and filesystem state.
246246
RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error, opts *imports.Options) error
247247

248-
// Options returns a copy of the ViewOptions for this view.
248+
// Options returns a copy of the Options for this view.
249249
Options() Options
250+
251+
// SetOptions sets the options of this view to new values.
252+
// Warning: Do not use this, unless in a test.
253+
// This function does not correctly invalidate the view when needed.
254+
SetOptions(Options)
250255
}
251256

252257
// File represents a source file of any type.

0 commit comments

Comments
 (0)