From 186fcf30e5da15424a47c0f25d7cbf704ad32473 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 5 Jan 2024 14:53:00 -0500 Subject: [PATCH] gopls/internal/lsp/source: factor edit conversion utils Change-Id: I85107134c426476f08a06940d53fc912f268ef60 Reviewed-on: https://go-review.googlesource.com/c/tools/+/556575 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/lsp/protocol/edits.go | 25 +++++++++++++++++++ gopls/internal/lsp/source/change_quote.go | 12 +-------- gopls/internal/lsp/source/change_signature.go | 10 +------- gopls/internal/server/code_action.go | 14 +---------- gopls/internal/server/command.go | 23 ++--------------- gopls/internal/settings/settings.go | 5 +--- 6 files changed, 31 insertions(+), 58 deletions(-) diff --git a/gopls/internal/lsp/protocol/edits.go b/gopls/internal/lsp/protocol/edits.go index 71d4427c73c..53fd4cf94e3 100644 --- a/gopls/internal/lsp/protocol/edits.go +++ b/gopls/internal/lsp/protocol/edits.go @@ -101,3 +101,28 @@ func AsAnnotatedTextEdits(edits []TextEdit) []Or_TextDocumentEdit_edits_Elem { } return result } + +// TextEditsToDocumentChanges converts a set of edits within the +// specified (versioned) file to a singleton list of DocumentChanges +// (as required for a WorkspaceEdit). +func TextEditsToDocumentChanges(uri DocumentURI, version int32, edits []TextEdit) []DocumentChanges { + return []DocumentChanges{{ + TextDocumentEdit: &TextDocumentEdit{ + TextDocument: OptionalVersionedTextDocumentIdentifier{ + Version: version, + TextDocumentIdentifier: TextDocumentIdentifier{URI: uri}, + }, + Edits: AsAnnotatedTextEdits(edits), + }, + }} +} + +// TextDocumentEditsToDocumentChanges wraps each TextDocumentEdit in a DocumentChange. +func TextDocumentEditsToDocumentChanges(edits []TextDocumentEdit) []DocumentChanges { + changes := []DocumentChanges{} // non-nil + for _, edit := range edits { + edit := edit + changes = append(changes, DocumentChanges{TextDocumentEdit: &edit}) + } + return changes +} diff --git a/gopls/internal/lsp/source/change_quote.go b/gopls/internal/lsp/source/change_quote.go index 57dffdbc399..ad9a127b3b9 100644 --- a/gopls/internal/lsp/source/change_quote.go +++ b/gopls/internal/lsp/source/change_quote.go @@ -78,17 +78,7 @@ func ConvertStringLiteral(pgf *ParsedGoFile, fh file.Handle, rng protocol.Range) Title: title, Kind: protocol.RefactorRewrite, Edit: &protocol.WorkspaceEdit{ - DocumentChanges: []protocol.DocumentChanges{ - { - TextDocumentEdit: &protocol.TextDocumentEdit{ - TextDocument: protocol.OptionalVersionedTextDocumentIdentifier{ - Version: fh.Version(), - TextDocumentIdentifier: protocol.TextDocumentIdentifier{URI: fh.URI()}, - }, - Edits: protocol.AsAnnotatedTextEdits(pedits), - }, - }, - }, + DocumentChanges: protocol.TextEditsToDocumentChanges(fh.URI(), fh.Version(), pedits), }, }, true } diff --git a/gopls/internal/lsp/source/change_signature.go b/gopls/internal/lsp/source/change_signature.go index 76ea0d248de..b745dd182d0 100644 --- a/gopls/internal/lsp/source/change_signature.go +++ b/gopls/internal/lsp/source/change_signature.go @@ -170,15 +170,7 @@ func RemoveUnusedParameter(ctx context.Context, fh file.Handle, rng protocol.Ran if err != nil { return nil, fmt.Errorf("computing edits for %s: %v", uri, err) } - changes = append(changes, protocol.DocumentChanges{ - TextDocumentEdit: &protocol.TextDocumentEdit{ - TextDocument: protocol.OptionalVersionedTextDocumentIdentifier{ - Version: fh.Version(), - TextDocumentIdentifier: protocol.TextDocumentIdentifier{URI: uri}, - }, - Edits: protocol.AsAnnotatedTextEdits(pedits), - }, - }) + changes = append(changes, protocol.TextEditsToDocumentChanges(fh.URI(), fh.Version(), pedits)...) } return changes, nil } diff --git a/gopls/internal/server/code_action.go b/gopls/internal/server/code_action.go index 28daef58c88..291de86c952 100644 --- a/gopls/internal/server/code_action.go +++ b/gopls/internal/server/code_action.go @@ -687,19 +687,7 @@ func refactorInline(pkg *cache.Package, pgf *source.ParsedGoFile, rng protocol.R } func documentChanges(fh file.Handle, edits []protocol.TextEdit) []protocol.DocumentChanges { - return []protocol.DocumentChanges{ - { - TextDocumentEdit: &protocol.TextDocumentEdit{ - TextDocument: protocol.OptionalVersionedTextDocumentIdentifier{ - Version: fh.Version(), - TextDocumentIdentifier: protocol.TextDocumentIdentifier{ - URI: fh.URI(), - }, - }, - Edits: protocol.AsAnnotatedTextEdits(edits), - }, - }, - } + return protocol.TextEditsToDocumentChanges(fh.URI(), fh.Version(), edits) } // codeActionsMatchingDiagnostics fetches code actions for the provided diff --git a/gopls/internal/server/command.go b/gopls/internal/server/command.go index 0e0bbbf949e..094e50c7730 100644 --- a/gopls/internal/server/command.go +++ b/gopls/internal/server/command.go @@ -453,19 +453,7 @@ func (c *commandHandler) RemoveDependency(ctx context.Context, args command.Remo } response, err := c.s.client.ApplyEdit(ctx, &protocol.ApplyWorkspaceEditParams{ Edit: protocol.WorkspaceEdit{ - DocumentChanges: []protocol.DocumentChanges{ - { - TextDocumentEdit: &protocol.TextDocumentEdit{ - TextDocument: protocol.OptionalVersionedTextDocumentIdentifier{ - Version: deps.fh.Version(), - TextDocumentIdentifier: protocol.TextDocumentIdentifier{ - URI: deps.fh.URI(), - }, - }, - Edits: protocol.AsAnnotatedTextEdits(edits), - }, - }, - }, + DocumentChanges: documentChanges(deps.fh, edits), }, }) if err != nil { @@ -719,16 +707,9 @@ func applyFileEdits(ctx context.Context, cli protocol.Client, edits []protocol.T if len(edits) == 0 { return nil } - documentChanges := []protocol.DocumentChanges{} // must be a slice - for _, change := range edits { - change := change - documentChanges = append(documentChanges, protocol.DocumentChanges{ - TextDocumentEdit: &change, - }) - } response, err := cli.ApplyEdit(ctx, &protocol.ApplyWorkspaceEditParams{ Edit: protocol.WorkspaceEdit{ - DocumentChanges: documentChanges, + DocumentChanges: protocol.TextDocumentEditsToDocumentChanges(edits), }, }) if err != nil { diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go index 9f5bb808768..9c784134c9e 100644 --- a/gopls/internal/settings/settings.go +++ b/gopls/internal/settings/settings.go @@ -1473,10 +1473,7 @@ func defaultAnalyzers() map[string]*Analyzer { unusedwrite.Analyzer.Name: {Analyzer: unusedwrite.Analyzer, Enabled: false}, useany.Analyzer.Name: {Analyzer: useany.Analyzer, Enabled: false}, timeformat.Analyzer.Name: {Analyzer: timeformat.Analyzer, Enabled: true}, - embeddirective.Analyzer.Name: { - Analyzer: embeddirective.Analyzer, - Enabled: true, - }, + embeddirective.Analyzer.Name: {Analyzer: embeddirective.Analyzer, Enabled: true}, // gofmt -s suite: simplifycompositelit.Analyzer.Name: {