Skip to content

Commit

Permalink
gopls/internal/lsp/cache: move SuggestedFixFromCommand into cache
Browse files Browse the repository at this point in the history
Change-Id: Id468b7b2749bae70430bcde70a1d3c5ceaaa92f8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/543919
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Nov 20, 2023
1 parent ab6af7d commit 3c677e3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
16 changes: 16 additions & 0 deletions gopls/internal/lsp/cache/diagnostics.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
6 changes: 3 additions & 3 deletions gopls/internal/lsp/cache/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion gopls/internal/lsp/cache/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ var (
RemoveIntermediateTestVariants = source.RemoveIntermediateTestVariants
IsCommandLineArguments = source.IsCommandLineArguments
BundleQuickFixes = source.BundleQuickFixes
SuggestedFixFromCommand = source.SuggestedFixFromCommand
ToProtocolEdits = source.ToProtocolEdits
NewFilterer = source.NewFilterer
)
Expand Down
10 changes: 5 additions & 5 deletions gopls/internal/lsp/mod/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
})
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions gopls/internal/lsp/source/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c677e3

Please sign in to comment.