2019-04-05 13:56:08 -06:00
|
|
|
// Copyright 2018 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 lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-27 19:26:42 -06:00
|
|
|
"fmt"
|
2019-08-14 13:24:21 -06:00
|
|
|
"sort"
|
2019-04-05 13:56:08 -06:00
|
|
|
"strings"
|
|
|
|
|
2019-07-30 12:00:02 -06:00
|
|
|
"golang.org/x/tools/internal/imports"
|
2019-04-05 13:56:08 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-07-14 21:08:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-04-05 13:56:08 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-04-05 13:56:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
|
2019-08-05 17:27:28 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-11-15 10:43:45 -07:00
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-17 16:57:54 -07:00
|
|
|
snapshot := view.Snapshot()
|
2020-01-10 15:37:29 -07:00
|
|
|
fh, err := snapshot.GetFile(uri)
|
2019-08-16 11:49:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
|
2019-08-05 17:27:28 -06:00
|
|
|
// Determine the supported actions for this file kind.
|
2019-11-12 15:58:37 -07:00
|
|
|
supportedCodeActions, ok := view.Options().SupportedCodeActions[fh.Identity().Kind]
|
2019-08-05 17:27:28 -06:00
|
|
|
if !ok {
|
2019-11-12 15:58:37 -07:00
|
|
|
return nil, fmt.Errorf("no supported code actions for %v file kind", fh.Identity().Kind)
|
2019-08-05 17:27:28 -06:00
|
|
|
}
|
2019-06-27 19:26:42 -06:00
|
|
|
|
|
|
|
// The Only field of the context specifies which code actions the client wants.
|
|
|
|
// If Only is empty, assume that the client wants all of the possible code actions.
|
|
|
|
var wanted map[protocol.CodeActionKind]bool
|
|
|
|
if len(params.Context.Only) == 0 {
|
2019-08-05 17:27:28 -06:00
|
|
|
wanted = supportedCodeActions
|
2019-06-27 19:26:42 -06:00
|
|
|
} else {
|
|
|
|
wanted = make(map[protocol.CodeActionKind]bool)
|
|
|
|
for _, only := range params.Context.Only {
|
2019-08-05 17:27:28 -06:00
|
|
|
wanted[only] = supportedCodeActions[only]
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(wanted) == 0 {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("no supported code action to execute for %s, wanted %v", uri, params.Context.Only)
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
|
|
|
|
2019-04-05 13:56:08 -06:00
|
|
|
var codeActions []protocol.CodeAction
|
2019-11-12 15:58:37 -07:00
|
|
|
switch fh.Identity().Kind {
|
2019-09-18 23:21:54 -06:00
|
|
|
case source.Mod:
|
|
|
|
if !wanted[protocol.SourceOrganizeImports] {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
codeActions = append(codeActions, protocol.CodeAction{
|
|
|
|
Title: "Tidy",
|
|
|
|
Kind: protocol.SourceOrganizeImports,
|
2019-11-27 12:14:59 -07:00
|
|
|
Command: &protocol.Command{
|
2019-12-17 16:57:54 -07:00
|
|
|
Title: "Tidy",
|
|
|
|
Command: "tidy",
|
|
|
|
Arguments: []interface{}{fh.Identity().URI},
|
2019-09-18 23:21:54 -06:00
|
|
|
},
|
|
|
|
})
|
|
|
|
case source.Go:
|
2019-12-17 16:57:54 -07:00
|
|
|
edits, editsPerFix, err := source.AllImportsFixes(ctx, snapshot, fh)
|
2019-09-04 11:16:09 -06:00
|
|
|
if err != nil {
|
2019-09-18 23:21:54 -06:00
|
|
|
return nil, err
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
2019-09-24 14:28:59 -06:00
|
|
|
if diagnostics := params.Context.Diagnostics; wanted[protocol.QuickFix] && len(diagnostics) > 0 {
|
2019-09-18 23:21:54 -06:00
|
|
|
// First, add the quick fixes reported by go/analysis.
|
2019-12-17 16:57:54 -07:00
|
|
|
qf, err := quickFixes(ctx, snapshot, fh, diagnostics)
|
2019-09-18 23:21:54 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "quick fixes failed", err, telemetry.File.Of(uri))
|
|
|
|
}
|
|
|
|
codeActions = append(codeActions, qf...)
|
|
|
|
|
|
|
|
// If we also have diagnostics for missing imports, we can associate them with quick fixes.
|
2019-09-24 14:28:59 -06:00
|
|
|
if findImportErrors(diagnostics) {
|
2019-09-18 23:21:54 -06:00
|
|
|
// Separate this into a set of codeActions per diagnostic, where
|
|
|
|
// each action is the addition, removal, or renaming of one import.
|
|
|
|
for _, importFix := range editsPerFix {
|
|
|
|
// Get the diagnostics this fix would affect.
|
2019-09-24 14:28:59 -06:00
|
|
|
if fixDiagnostics := importDiagnostics(importFix.Fix, diagnostics); len(fixDiagnostics) > 0 {
|
2019-09-18 23:21:54 -06:00
|
|
|
codeActions = append(codeActions, protocol.CodeAction{
|
|
|
|
Title: importFixTitle(importFix.Fix),
|
|
|
|
Kind: protocol.QuickFix,
|
2019-11-17 12:29:15 -07:00
|
|
|
Edit: protocol.WorkspaceEdit{
|
2019-11-12 15:58:37 -07:00
|
|
|
DocumentChanges: documentChanges(fh, importFix.Edits),
|
2019-07-30 12:00:02 -06:00
|
|
|
},
|
2019-09-18 23:21:54 -06:00
|
|
|
Diagnostics: fixDiagnostics,
|
|
|
|
})
|
|
|
|
}
|
2019-07-30 12:00:02 -06:00
|
|
|
}
|
|
|
|
}
|
2019-04-05 13:56:08 -06:00
|
|
|
}
|
2019-10-23 13:07:22 -06:00
|
|
|
if wanted[protocol.SourceOrganizeImports] && len(edits) > 0 {
|
2019-09-18 23:21:54 -06:00
|
|
|
codeActions = append(codeActions, protocol.CodeAction{
|
|
|
|
Title: "Organize Imports",
|
|
|
|
Kind: protocol.SourceOrganizeImports,
|
2019-11-17 12:29:15 -07:00
|
|
|
Edit: protocol.WorkspaceEdit{
|
2019-11-12 15:58:37 -07:00
|
|
|
DocumentChanges: documentChanges(fh, edits),
|
2019-06-27 19:26:42 -06:00
|
|
|
},
|
2019-09-18 23:21:54 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Unsupported file kind for a code action.
|
|
|
|
return nil, nil
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
2019-04-05 13:56:08 -06:00
|
|
|
return codeActions, nil
|
|
|
|
}
|
|
|
|
|
2019-09-18 23:21:54 -06:00
|
|
|
func (s *Server) getSupportedCodeActions() []protocol.CodeActionKind {
|
|
|
|
allCodeActionKinds := make(map[protocol.CodeActionKind]struct{})
|
|
|
|
for _, kinds := range s.session.Options().SupportedCodeActions {
|
|
|
|
for kind := range kinds {
|
|
|
|
allCodeActionKinds[kind] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var result []protocol.CodeActionKind
|
|
|
|
for kind := range allCodeActionKinds {
|
|
|
|
result = append(result, kind)
|
|
|
|
}
|
|
|
|
sort.Slice(result, func(i, j int) bool {
|
|
|
|
return result[i] < result[j]
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:00:02 -06:00
|
|
|
type protocolImportFix struct {
|
|
|
|
fix *imports.ImportFix
|
|
|
|
edits []protocol.TextEdit
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:28:20 -06:00
|
|
|
// findImports determines if a given diagnostic represents an error that could
|
|
|
|
// be fixed by organizing imports.
|
|
|
|
// TODO(rstambler): We need a better way to check this than string matching.
|
|
|
|
func findImportErrors(diagnostics []protocol.Diagnostic) bool {
|
|
|
|
for _, diagnostic := range diagnostics {
|
|
|
|
// "undeclared name: X" may be an unresolved import.
|
|
|
|
if strings.HasPrefix(diagnostic.Message, "undeclared name: ") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// "could not import: X" may be an invalid import.
|
|
|
|
if strings.HasPrefix(diagnostic.Message, "could not import: ") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// "X imported but not used" is an unused import.
|
2019-07-30 09:10:00 -06:00
|
|
|
// "X imported but not used as Y" is an unused import.
|
|
|
|
if strings.Contains(diagnostic.Message, " imported but not used") {
|
2019-04-16 15:28:20 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2019-04-05 13:56:08 -06:00
|
|
|
}
|
2019-06-27 19:26:42 -06:00
|
|
|
|
2019-07-30 12:00:02 -06:00
|
|
|
func importFixTitle(fix *imports.ImportFix) string {
|
|
|
|
var str string
|
|
|
|
switch fix.FixType {
|
|
|
|
case imports.AddImport:
|
|
|
|
str = fmt.Sprintf("Add import: %s %q", fix.StmtInfo.Name, fix.StmtInfo.ImportPath)
|
|
|
|
case imports.DeleteImport:
|
|
|
|
str = fmt.Sprintf("Delete import: %s %q", fix.StmtInfo.Name, fix.StmtInfo.ImportPath)
|
|
|
|
case imports.SetImportName:
|
|
|
|
str = fmt.Sprintf("Rename import: %s %q", fix.StmtInfo.Name, fix.StmtInfo.ImportPath)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func importDiagnostics(fix *imports.ImportFix, diagnostics []protocol.Diagnostic) (results []protocol.Diagnostic) {
|
|
|
|
for _, diagnostic := range diagnostics {
|
|
|
|
switch {
|
|
|
|
// "undeclared name: X" may be an unresolved import.
|
|
|
|
case strings.HasPrefix(diagnostic.Message, "undeclared name: "):
|
|
|
|
ident := strings.TrimPrefix(diagnostic.Message, "undeclared name: ")
|
|
|
|
if ident == fix.IdentName {
|
|
|
|
results = append(results, diagnostic)
|
|
|
|
}
|
|
|
|
// "could not import: X" may be an invalid import.
|
|
|
|
case strings.HasPrefix(diagnostic.Message, "could not import: "):
|
|
|
|
ident := strings.TrimPrefix(diagnostic.Message, "could not import: ")
|
|
|
|
if ident == fix.IdentName {
|
|
|
|
results = append(results, diagnostic)
|
|
|
|
}
|
|
|
|
// "X imported but not used" is an unused import.
|
|
|
|
// "X imported but not used as Y" is an unused import.
|
|
|
|
case strings.Contains(diagnostic.Message, " imported but not used"):
|
|
|
|
idx := strings.Index(diagnostic.Message, " imported but not used")
|
|
|
|
importPath := diagnostic.Message[:idx]
|
|
|
|
if importPath == fmt.Sprintf("%q", fix.StmtInfo.ImportPath) {
|
|
|
|
results = append(results, diagnostic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2019-12-17 16:57:54 -07:00
|
|
|
func quickFixes(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle, diagnostics []protocol.Diagnostic) ([]protocol.CodeAction, error) {
|
2019-06-27 19:26:42 -06:00
|
|
|
var codeActions []protocol.CodeAction
|
2019-11-20 12:26:02 -07:00
|
|
|
|
2019-11-29 23:17:57 -07:00
|
|
|
phs, err := snapshot.PackageHandles(ctx, fh)
|
2019-09-09 17:26:26 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-24 14:28:59 -06:00
|
|
|
// We get the package that source.Diagnostics would've used. This is hack.
|
|
|
|
// TODO(golang/go#32443): The correct solution will be to cache diagnostics per-file per-snapshot.
|
2020-01-14 16:29:21 -07:00
|
|
|
ph, err := source.WidestPackageHandle(phs)
|
2019-10-10 13:22:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-24 14:28:59 -06:00
|
|
|
for _, diag := range diagnostics {
|
2019-11-21 16:55:49 -07:00
|
|
|
// This code assumes that the analyzer name is the Source of the diagnostic.
|
|
|
|
// If this ever changes, this will need to be addressed.
|
2019-11-29 23:17:57 -07:00
|
|
|
srcErr, err := snapshot.FindAnalysisError(ctx, ph.ID(), diag.Source, diag.Message, diag.Range)
|
2019-06-27 19:26:42 -06:00
|
|
|
if err != nil {
|
2019-09-24 14:28:59 -06:00
|
|
|
continue
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
2019-10-24 13:44:41 -06:00
|
|
|
for _, fix := range srcErr.SuggestedFixes {
|
2019-11-12 15:58:37 -07:00
|
|
|
action := protocol.CodeAction{
|
2019-09-24 14:28:59 -06:00
|
|
|
Title: fix.Title,
|
|
|
|
Kind: protocol.QuickFix,
|
|
|
|
Diagnostics: []protocol.Diagnostic{diag},
|
2019-11-17 12:29:15 -07:00
|
|
|
Edit: protocol.WorkspaceEdit{},
|
2019-11-12 15:58:37 -07:00
|
|
|
}
|
|
|
|
for uri, edits := range fix.Edits {
|
2020-01-10 15:37:29 -07:00
|
|
|
fh, err := snapshot.GetFile(uri)
|
2019-11-12 15:58:37 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "no file", err, telemetry.URI.Of(uri))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
action.Edit.DocumentChanges = append(action.Edit.DocumentChanges, documentChanges(fh, edits)...)
|
|
|
|
}
|
|
|
|
codeActions = append(codeActions, action)
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return codeActions, nil
|
|
|
|
}
|
2019-11-12 15:58:37 -07:00
|
|
|
|
|
|
|
func documentChanges(fh source.FileHandle, edits []protocol.TextEdit) []protocol.TextDocumentEdit {
|
|
|
|
return []protocol.TextDocumentEdit{
|
|
|
|
{
|
|
|
|
TextDocument: protocol.VersionedTextDocumentIdentifier{
|
|
|
|
Version: fh.Identity().Version,
|
|
|
|
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
|
|
|
|
URI: protocol.NewURI(fh.Identity().URI),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Edits: edits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|