From 3c677e3f7d23999e4a31018a5f6bfade055408b7 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Mon, 20 Nov 2023 16:23:21 -0500 Subject: [PATCH] gopls/internal/lsp/cache: move SuggestedFixFromCommand into cache Change-Id: Id468b7b2749bae70430bcde70a1d3c5ceaaa92f8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/543919 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/lsp/cache/diagnostics.go | 16 ++++++++++++++++ gopls/internal/lsp/cache/errors.go | 6 +++--- gopls/internal/lsp/cache/pkg.go | 1 - gopls/internal/lsp/mod/diagnostics.go | 10 +++++----- gopls/internal/lsp/source/fix.go | 8 -------- 5 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 gopls/internal/lsp/cache/diagnostics.go diff --git a/gopls/internal/lsp/cache/diagnostics.go b/gopls/internal/lsp/cache/diagnostics.go new file mode 100644 index 00000000000..e7c2ca7a669 --- /dev/null +++ b/gopls/internal/lsp/cache/diagnostics.go @@ -0,0 +1,16 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cache + +import "golang.org/x/tools/gopls/internal/lsp/protocol" + +// SuggestedFixFromCommand returns a suggested fix to run the given command. +func SuggestedFixFromCommand(cmd protocol.Command, kind protocol.CodeActionKind) SuggestedFix { + return SuggestedFix{ + Title: cmd.Title, + Command: &cmd, + ActionKind: kind, + } +} diff --git a/gopls/internal/lsp/cache/errors.go b/gopls/internal/lsp/cache/errors.go index 5620b0519ef..e99f6700303 100644 --- a/gopls/internal/lsp/cache/errors.go +++ b/gopls/internal/lsp/cache/errors.go @@ -198,7 +198,7 @@ func goGetQuickFixes(moduleMode bool, uri protocol.DocumentURI, pkg string) ([]s if err != nil { return nil, err } - return []source.SuggestedFix{source.SuggestedFixFromCommand(cmd, protocol.QuickFix)}, nil + return []source.SuggestedFix{SuggestedFixFromCommand(cmd, protocol.QuickFix)}, nil } func editGoDirectiveQuickFix(moduleMode bool, uri protocol.DocumentURI, version string) ([]source.SuggestedFix, error) { @@ -214,7 +214,7 @@ func editGoDirectiveQuickFix(moduleMode bool, uri protocol.DocumentURI, version if err != nil { return nil, err } - return []source.SuggestedFix{source.SuggestedFixFromCommand(cmd, protocol.QuickFix)}, nil + return []source.SuggestedFix{SuggestedFixFromCommand(cmd, protocol.QuickFix)}, nil } // encodeDiagnostics gob-encodes the given diagnostics. @@ -366,7 +366,7 @@ func toSourceDiagnostic(srcAnalyzer *settings.Analyzer, gobDiag *gobDiagnostic) log.Fatalf("internal error in NewApplyFixCommand: %v", err) } for _, kind := range kinds { - fixes = append(fixes, source.SuggestedFixFromCommand(cmd, kind)) + fixes = append(fixes, SuggestedFixFromCommand(cmd, kind)) } } diag.SuggestedFixes = fixes diff --git a/gopls/internal/lsp/cache/pkg.go b/gopls/internal/lsp/cache/pkg.go index b06ddf2e509..2ab4694ff18 100644 --- a/gopls/internal/lsp/cache/pkg.go +++ b/gopls/internal/lsp/cache/pkg.go @@ -75,7 +75,6 @@ var ( RemoveIntermediateTestVariants = source.RemoveIntermediateTestVariants IsCommandLineArguments = source.IsCommandLineArguments BundleQuickFixes = source.BundleQuickFixes - SuggestedFixFromCommand = source.SuggestedFixFromCommand ToProtocolEdits = source.ToProtocolEdits NewFilterer = source.NewFilterer ) diff --git a/gopls/internal/lsp/mod/diagnostics.go b/gopls/internal/lsp/mod/diagnostics.go index fafe684acd3..d524134ea92 100644 --- a/gopls/internal/lsp/mod/diagnostics.go +++ b/gopls/internal/lsp/mod/diagnostics.go @@ -173,7 +173,7 @@ func ModUpgradeDiagnostics(ctx context.Context, snapshot *cache.Snapshot, fh fil Severity: protocol.SeverityInformation, Source: source.UpgradeNotification, Message: fmt.Sprintf("%v can be upgraded", req.Mod.Path), - SuggestedFixes: []source.SuggestedFix{source.SuggestedFixFromCommand(cmd, protocol.QuickFix)}, + SuggestedFixes: []source.SuggestedFix{cache.SuggestedFixFromCommand(cmd, protocol.QuickFix)}, }) } @@ -274,7 +274,7 @@ func ModVulnerabilityDiagnostics(ctx context.Context, snapshot *cache.Snapshot, if err != nil { return nil, err // TODO: bug report } - sf := source.SuggestedFixFromCommand(cmd, protocol.QuickFix) + sf := cache.SuggestedFixFromCommand(cmd, protocol.QuickFix) switch _, typ := foundVuln(finding); typ { case vulnImported: infoFixes = append(infoFixes, sf) @@ -301,7 +301,7 @@ func ModVulnerabilityDiagnostics(ctx context.Context, snapshot *cache.Snapshot, if err != nil { return nil, err // TODO: bug report } - sf := source.SuggestedFixFromCommand(latest, protocol.QuickFix) + sf := cache.SuggestedFixFromCommand(latest, protocol.QuickFix) if len(warningFixes) > 0 { warningFixes = append(warningFixes, sf) } @@ -454,7 +454,7 @@ func suggestGovulncheckAction(fromGovulncheck bool, uri protocol.DocumentURI) (s if err != nil { return source.SuggestedFix{}, err } - return source.SuggestedFixFromCommand(resetVulncheck, protocol.QuickFix), nil + return cache.SuggestedFixFromCommand(resetVulncheck, protocol.QuickFix), nil } vulncheck, err := command.NewRunGovulncheckCommand("Run govulncheck to verify", command.VulncheckArgs{ URI: uri, @@ -463,7 +463,7 @@ func suggestGovulncheckAction(fromGovulncheck bool, uri protocol.DocumentURI) (s if err != nil { return source.SuggestedFix{}, err } - return source.SuggestedFixFromCommand(vulncheck, protocol.QuickFix), nil + return cache.SuggestedFixFromCommand(vulncheck, protocol.QuickFix), nil } func getVulnMessage(mod string, vulns []string, used, fromGovulncheck bool) string { diff --git a/gopls/internal/lsp/source/fix.go b/gopls/internal/lsp/source/fix.go index 2725754a3ce..b44a25d7df8 100644 --- a/gopls/internal/lsp/source/fix.go +++ b/gopls/internal/lsp/source/fix.go @@ -94,14 +94,6 @@ func singleFile(sf singleFileFixFunc) suggestedFixFunc { } } -func SuggestedFixFromCommand(cmd protocol.Command, kind protocol.CodeActionKind) SuggestedFix { - return SuggestedFix{ - Title: cmd.Title, - Command: &cmd, - ActionKind: kind, - } -} - // CanFix returns true if Analyzer.Fix can fix the Diagnostic. // // It returns true by default: only if the analyzer is configured explicitly to