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"
|
|
|
|
|
2020-06-12 15:10:06 -06:00
|
|
|
"golang.org/x/tools/go/analysis"
|
2020-07-14 22:19:10 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2019-07-30 12:00:02 -06:00
|
|
|
"golang.org/x/tools/internal/imports"
|
2020-07-14 22:19:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-04-05 13:56:08 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2020-06-12 15:10:06 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-04-05 13:56:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
snapshot, fh, ok, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind)
|
2020-02-13 11:46:49 -07:00
|
|
|
if !ok {
|
2019-08-16 11:49:17 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
uri := fh.URI()
|
2019-09-27 11:17:59 -06:00
|
|
|
|
2019-08-05 17:27:28 -06:00
|
|
|
// Determine the supported actions for this file kind.
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
supportedCodeActions, ok := snapshot.View().Options().SupportedCodeActions[fh.Kind()]
|
2019-08-05 17:27:28 -06:00
|
|
|
if !ok {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
return nil, fmt.Errorf("no supported code actions for %v file kind", fh.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 {
|
2020-03-30 10:45:15 -06:00
|
|
|
return nil, fmt.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
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
switch fh.Kind() {
|
2019-09-18 23:21:54 -06:00
|
|
|
case source.Mod:
|
2020-07-20 14:56:12 -06:00
|
|
|
if diagnostics := params.Context.Diagnostics; len(diagnostics) > 0 {
|
|
|
|
modQuickFixes, err := moduleQuickFixes(ctx, snapshot, diagnostics)
|
|
|
|
if err == source.ErrTmpModfileUnsupported {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-04-01 19:31:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-20 14:56:12 -06:00
|
|
|
codeActions = append(codeActions, modQuickFixes...)
|
|
|
|
}
|
|
|
|
if wanted[protocol.SourceOrganizeImports] {
|
|
|
|
action, err := goModTidy(ctx, snapshot)
|
|
|
|
if err == source.ErrTmpModfileUnsupported {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
codeActions = append(codeActions, *action)
|
2020-01-29 09:16:19 -07:00
|
|
|
}
|
2019-09-18 23:21:54 -06:00
|
|
|
case source.Go:
|
2020-04-29 15:49:22 -06:00
|
|
|
// Don't suggest fixes for generated files, since they are generally
|
|
|
|
// not useful and some editors may apply them automatically on save.
|
|
|
|
if source.IsGenerated(ctx, snapshot, uri) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-03-15 13:41:57 -06:00
|
|
|
diagnostics := params.Context.Diagnostics
|
|
|
|
|
2020-02-26 15:07:02 -07:00
|
|
|
// First, process any missing imports and pair them with the
|
|
|
|
// diagnostics they fix.
|
|
|
|
if wantQuickFixes := wanted[protocol.QuickFix] && len(diagnostics) > 0; wantQuickFixes || wanted[protocol.SourceOrganizeImports] {
|
|
|
|
importEdits, importEditsPerFix, err := source.AllImportsFixes(ctx, snapshot, fh)
|
2020-03-15 13:41:57 -06:00
|
|
|
if err != nil {
|
2020-07-16 18:00:10 -06:00
|
|
|
event.Error(ctx, "imports fixes", err, tag.File.Of(fh.URI().Filename()))
|
2020-03-15 13:41:57 -06:00
|
|
|
}
|
2020-02-26 15:07:02 -07:00
|
|
|
// Separate this into a set of codeActions per diagnostic, where
|
|
|
|
// each action is the addition, removal, or renaming of one import.
|
|
|
|
if wantQuickFixes {
|
|
|
|
for _, importFix := range importEditsPerFix {
|
|
|
|
fixes := importDiagnostics(importFix.Fix, diagnostics)
|
|
|
|
if len(fixes) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
codeActions = append(codeActions, protocol.CodeAction{
|
|
|
|
Title: importFixTitle(importFix.Fix),
|
|
|
|
Kind: protocol.QuickFix,
|
|
|
|
Edit: protocol.WorkspaceEdit{
|
|
|
|
DocumentChanges: documentChanges(fh, importFix.Edits),
|
|
|
|
},
|
|
|
|
Diagnostics: fixes,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Send all of the import edits as one code action if the file is
|
|
|
|
// being organized.
|
|
|
|
if wanted[protocol.SourceOrganizeImports] && len(importEdits) > 0 {
|
|
|
|
codeActions = append(codeActions, protocol.CodeAction{
|
|
|
|
Title: "Organize Imports",
|
|
|
|
Kind: protocol.SourceOrganizeImports,
|
|
|
|
Edit: protocol.WorkspaceEdit{
|
|
|
|
DocumentChanges: documentChanges(fh, importEdits),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
2020-07-22 09:32:32 -06:00
|
|
|
pkgs, err := snapshot.PackagesForFile(ctx, fh.URI())
|
2020-06-12 15:10:06 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-22 09:32:32 -06:00
|
|
|
pkg, err := source.WidestPackage(pkgs)
|
2020-06-12 15:10:06 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-15 13:41:57 -06:00
|
|
|
if (wanted[protocol.QuickFix] || wanted[protocol.SourceFixAll]) && len(diagnostics) > 0 {
|
2020-07-22 09:32:32 -06:00
|
|
|
analysisQuickFixes, highConfidenceEdits, err := analysisFixes(ctx, snapshot, pkg, diagnostics)
|
2019-09-18 23:21:54 -06:00
|
|
|
if err != nil {
|
2020-02-26 15:07:02 -07:00
|
|
|
return nil, err
|
2019-09-18 23:21:54 -06:00
|
|
|
}
|
2020-02-26 15:07:02 -07:00
|
|
|
if wanted[protocol.QuickFix] {
|
|
|
|
// Add the quick fixes reported by go/analysis.
|
|
|
|
codeActions = append(codeActions, analysisQuickFixes...)
|
2020-03-15 13:41:57 -06:00
|
|
|
|
2020-02-26 15:07:02 -07:00
|
|
|
// If there are any diagnostics relating to the go.mod file,
|
|
|
|
// add their corresponding quick fixes.
|
2020-07-20 14:56:12 -06:00
|
|
|
modQuickFixes, err := moduleQuickFixes(ctx, snapshot, diagnostics)
|
2020-02-26 15:07:02 -07:00
|
|
|
if err != nil {
|
2020-07-14 22:19:10 -06:00
|
|
|
// Not a fatal error.
|
|
|
|
event.Error(ctx, "module suggested fixes failed", err, tag.Directory.Of(snapshot.View().Folder()))
|
2019-07-30 12:00:02 -06:00
|
|
|
}
|
2020-07-20 14:56:12 -06:00
|
|
|
codeActions = append(codeActions, modQuickFixes...)
|
2019-07-30 12:00:02 -06:00
|
|
|
}
|
2020-02-26 15:07:02 -07:00
|
|
|
if wanted[protocol.SourceFixAll] && len(highConfidenceEdits) > 0 {
|
|
|
|
codeActions = append(codeActions, protocol.CodeAction{
|
|
|
|
Title: "Simplifications",
|
|
|
|
Kind: protocol.SourceFixAll,
|
|
|
|
Edit: protocol.WorkspaceEdit{
|
|
|
|
DocumentChanges: highConfidenceEdits,
|
|
|
|
},
|
|
|
|
})
|
2020-01-22 12:23:12 -07:00
|
|
|
}
|
2019-04-05 13:56:08 -06:00
|
|
|
}
|
2020-06-12 15:10:06 -06:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
// Add any suggestions that do not necessarily fix any diagnostics.
|
|
|
|
if wanted[protocol.RefactorRewrite] {
|
2020-07-22 09:32:32 -06:00
|
|
|
fixes, err := convenienceFixes(ctx, snapshot, pkg, uri, params.Range)
|
2020-06-12 15:10:06 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
codeActions = append(codeActions, fixes...)
|
2020-06-03 12:56:29 -06:00
|
|
|
}
|
2020-06-24 07:52:23 -06:00
|
|
|
if wanted[protocol.RefactorExtract] {
|
2020-07-22 09:32:32 -06:00
|
|
|
fixes, err := extractionFixes(ctx, snapshot, pkg, uri, params.Range)
|
2020-06-24 07:52:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
codeActions = append(codeActions, fixes...)
|
|
|
|
}
|
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
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-22 09:32:32 -06:00
|
|
|
func analysisFixes(ctx context.Context, snapshot source.Snapshot, pkg source.Package, diagnostics []protocol.Diagnostic) ([]protocol.CodeAction, []protocol.TextDocumentEdit, error) {
|
2020-03-15 13:41:57 -06:00
|
|
|
if len(diagnostics) == 0 {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2020-07-13 17:36:26 -06:00
|
|
|
var (
|
|
|
|
codeActions []protocol.CodeAction
|
|
|
|
sourceFixAllEdits []protocol.TextDocumentEdit
|
|
|
|
)
|
2019-09-24 14:28:59 -06:00
|
|
|
for _, diag := range diagnostics {
|
2020-07-22 09:32:32 -06:00
|
|
|
srcErr, analyzer, ok := findSourceError(ctx, snapshot, pkg.ID(), diag)
|
2020-03-31 21:53:42 -06:00
|
|
|
if !ok {
|
2019-09-24 14:28:59 -06:00
|
|
|
continue
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
2020-07-13 17:36:26 -06:00
|
|
|
// If the suggested fix for the diagnostic is expected to be separate,
|
|
|
|
// see if there are any supported commands available.
|
2020-07-23 21:24:36 -06:00
|
|
|
if analyzer.Command != nil {
|
2020-07-13 17:36:26 -06:00
|
|
|
action, err := diagnosticToCommandCodeAction(ctx, snapshot, srcErr, &diag, protocol.QuickFix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
codeActions = append(codeActions, *action)
|
|
|
|
continue
|
|
|
|
}
|
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 {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
fh, err := snapshot.GetFile(ctx, uri)
|
2019-11-12 15:58:37 -07:00
|
|
|
if err != nil {
|
2020-02-26 15:07:02 -07:00
|
|
|
return nil, nil, err
|
2019-11-12 15:58:37 -07:00
|
|
|
}
|
2020-03-15 13:41:57 -06:00
|
|
|
docChanges := documentChanges(fh, edits)
|
|
|
|
if analyzer.HighConfidence {
|
|
|
|
sourceFixAllEdits = append(sourceFixAllEdits, docChanges...)
|
|
|
|
}
|
|
|
|
action.Edit.DocumentChanges = append(action.Edit.DocumentChanges, docChanges...)
|
2019-11-12 15:58:37 -07:00
|
|
|
}
|
|
|
|
codeActions = append(codeActions, action)
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 13:41:57 -06:00
|
|
|
return codeActions, sourceFixAllEdits, nil
|
2019-06-27 19:26:42 -06:00
|
|
|
}
|
2019-11-12 15:58:37 -07:00
|
|
|
|
2020-03-31 21:53:42 -06:00
|
|
|
func findSourceError(ctx context.Context, snapshot source.Snapshot, pkgID string, diag protocol.Diagnostic) (*source.Error, source.Analyzer, bool) {
|
2020-07-13 17:36:26 -06:00
|
|
|
analyzer := diagnosticToAnalyzer(snapshot, diag.Source, diag.Message)
|
|
|
|
if analyzer == nil {
|
2020-03-31 21:53:42 -06:00
|
|
|
return nil, source.Analyzer{}, false
|
|
|
|
}
|
|
|
|
analysisErrors, err := snapshot.Analyze(ctx, pkgID, analyzer.Analyzer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, source.Analyzer{}, false
|
|
|
|
}
|
|
|
|
for _, err := range analysisErrors {
|
|
|
|
if err.Message != diag.Message {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if protocol.CompareRange(err.Range, diag.Range) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err.Category != analyzer.Analyzer.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// The error matches.
|
|
|
|
return err, *analyzer, true
|
|
|
|
}
|
|
|
|
return nil, source.Analyzer{}, false
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:36:26 -06:00
|
|
|
// diagnosticToAnalyzer return the analyzer associated with a given diagnostic.
|
|
|
|
// It assumes that the diagnostic's source will be the name of the analyzer.
|
|
|
|
// If this changes, this approach will need to be reworked.
|
|
|
|
func diagnosticToAnalyzer(snapshot source.Snapshot, src, msg string) (analyzer *source.Analyzer) {
|
|
|
|
// Make sure that the analyzer we found is enabled.
|
|
|
|
defer func() {
|
|
|
|
if analyzer != nil && !analyzer.Enabled(snapshot) {
|
|
|
|
analyzer = nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if a, ok := snapshot.View().Options().DefaultAnalyzers[src]; ok {
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
if a, ok := snapshot.View().Options().ConvenienceAnalyzers[src]; ok {
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
// Hack: We publish diagnostics with the source "compiler" for type errors,
|
|
|
|
// but these analyzers have different names. Try both possibilities.
|
|
|
|
if a, ok := snapshot.View().Options().TypeErrorAnalyzers[src]; ok {
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
if src != "compiler" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, a := range snapshot.View().Options().TypeErrorAnalyzers {
|
|
|
|
if a.FixesError(msg) {
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 09:32:32 -06:00
|
|
|
func convenienceFixes(ctx context.Context, snapshot source.Snapshot, pkg source.Package, uri span.URI, rng protocol.Range) ([]protocol.CodeAction, error) {
|
2020-06-12 15:10:06 -06:00
|
|
|
var analyzers []*analysis.Analyzer
|
|
|
|
for _, a := range snapshot.View().Options().ConvenienceAnalyzers {
|
2020-07-08 11:39:47 -06:00
|
|
|
if !a.Enabled(snapshot) {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-23 21:24:36 -06:00
|
|
|
if a.Command == nil {
|
2020-07-13 17:36:26 -06:00
|
|
|
event.Error(ctx, "convenienceFixes", fmt.Errorf("no suggested fixes for convenience analyzer %s", a.Analyzer.Name))
|
|
|
|
continue
|
|
|
|
}
|
2020-06-12 15:10:06 -06:00
|
|
|
analyzers = append(analyzers, a.Analyzer)
|
|
|
|
}
|
2020-07-22 09:32:32 -06:00
|
|
|
diagnostics, err := snapshot.Analyze(ctx, pkg.ID(), analyzers...)
|
2020-06-12 15:10:06 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var codeActions []protocol.CodeAction
|
|
|
|
for _, d := range diagnostics {
|
|
|
|
// For now, only show diagnostics for matching lines. Maybe we should
|
|
|
|
// alter this behavior in the future, depending on the user experience.
|
|
|
|
if d.URI != uri {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-24 08:34:05 -06:00
|
|
|
|
|
|
|
if !protocol.Intersect(d.Range, rng) {
|
2020-06-12 15:10:06 -06:00
|
|
|
continue
|
|
|
|
}
|
2020-07-13 17:36:26 -06:00
|
|
|
action, err := diagnosticToCommandCodeAction(ctx, snapshot, d, nil, protocol.RefactorRewrite)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-12 15:10:06 -06:00
|
|
|
}
|
2020-07-13 17:36:26 -06:00
|
|
|
codeActions = append(codeActions, *action)
|
2020-06-12 15:10:06 -06:00
|
|
|
}
|
|
|
|
return codeActions, nil
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:36:26 -06:00
|
|
|
func diagnosticToCommandCodeAction(ctx context.Context, snapshot source.Snapshot, e *source.Error, d *protocol.Diagnostic, kind protocol.CodeActionKind) (*protocol.CodeAction, error) {
|
|
|
|
// The fix depends on the category of the analyzer. The diagnostic may be
|
|
|
|
// nil, so use the error's category.
|
|
|
|
analyzer := diagnosticToAnalyzer(snapshot, e.Category, e.Message)
|
|
|
|
if analyzer == nil {
|
|
|
|
return nil, fmt.Errorf("no convenience analyzer for category %s", e.Category)
|
|
|
|
}
|
2020-07-23 21:24:36 -06:00
|
|
|
if analyzer.Command == nil {
|
2020-07-13 17:36:26 -06:00
|
|
|
return nil, fmt.Errorf("no command for convenience analyzer %s", analyzer.Analyzer.Name)
|
|
|
|
}
|
2020-07-23 21:24:36 -06:00
|
|
|
jsonArgs, err := source.MarshalArgs(e.URI, e.Range)
|
2020-07-13 17:36:26 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var diagnostics []protocol.Diagnostic
|
|
|
|
if d != nil {
|
|
|
|
diagnostics = append(diagnostics, *d)
|
|
|
|
}
|
|
|
|
return &protocol.CodeAction{
|
|
|
|
Title: e.Message,
|
|
|
|
Kind: kind,
|
|
|
|
Diagnostics: diagnostics,
|
|
|
|
Command: &protocol.Command{
|
2020-07-23 21:24:36 -06:00
|
|
|
Command: analyzer.Command.Name,
|
2020-07-13 17:36:26 -06:00
|
|
|
Title: e.Message,
|
|
|
|
Arguments: jsonArgs,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 09:32:32 -06:00
|
|
|
func extractionFixes(ctx context.Context, snapshot source.Snapshot, pkg source.Package, uri span.URI, rng protocol.Range) ([]protocol.CodeAction, error) {
|
2020-07-23 21:24:36 -06:00
|
|
|
if rng.Start == rng.End {
|
2020-06-24 07:52:23 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-07-23 21:24:36 -06:00
|
|
|
fh, err := snapshot.GetFile(ctx, uri)
|
2020-06-24 07:52:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-23 21:24:36 -06:00
|
|
|
jsonArgs, err := source.MarshalArgs(uri, rng)
|
2020-07-10 10:13:40 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-23 21:24:36 -06:00
|
|
|
var actions []protocol.CodeAction
|
|
|
|
for _, command := range []*source.Command{
|
|
|
|
source.CommandExtractFunction,
|
|
|
|
source.CommandExtractVariable,
|
|
|
|
} {
|
|
|
|
if !command.Applies(ctx, snapshot, fh, rng) {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-10 10:13:40 -06:00
|
|
|
actions = append(actions, protocol.CodeAction{
|
2020-07-23 21:24:36 -06:00
|
|
|
Title: command.Title,
|
2020-07-10 10:13:40 -06:00
|
|
|
Kind: protocol.RefactorExtract,
|
2020-07-23 21:24:36 -06:00
|
|
|
Command: &protocol.Command{
|
|
|
|
Command: source.CommandExtractFunction.Name,
|
|
|
|
Arguments: jsonArgs,
|
2020-07-10 10:13:40 -06:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return actions, nil
|
2020-06-24 07:52:23 -06:00
|
|
|
}
|
|
|
|
|
2019-11-12 15:58:37 -07:00
|
|
|
func documentChanges(fh source.FileHandle, edits []protocol.TextEdit) []protocol.TextDocumentEdit {
|
|
|
|
return []protocol.TextDocumentEdit{
|
|
|
|
{
|
|
|
|
TextDocument: protocol.VersionedTextDocumentIdentifier{
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
Version: fh.Version(),
|
2019-11-12 15:58:37 -07:00
|
|
|
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
URI: protocol.URIFromSpanURI(fh.URI()),
|
2019-11-12 15:58:37 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Edits: edits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 14:56:12 -06:00
|
|
|
|
|
|
|
func moduleQuickFixes(ctx context.Context, snapshot source.Snapshot, diagnostics []protocol.Diagnostic) ([]protocol.CodeAction, error) {
|
|
|
|
mth, err := snapshot.ModTidyHandle(ctx)
|
|
|
|
if err == source.ErrTmpModfileUnsupported {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-16 15:37:12 -06:00
|
|
|
errors, err := mth.Tidy(ctx, snapshot)
|
2020-07-20 14:56:12 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pmh := mth.ParseModHandle()
|
|
|
|
var quickFixes []protocol.CodeAction
|
|
|
|
for _, e := range errors {
|
|
|
|
var diag *protocol.Diagnostic
|
|
|
|
for _, d := range diagnostics {
|
|
|
|
if sameDiagnostic(d, e) {
|
|
|
|
diag = &d
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if diag == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, fix := range e.SuggestedFixes {
|
|
|
|
action := protocol.CodeAction{
|
|
|
|
Title: fix.Title,
|
|
|
|
Kind: protocol.QuickFix,
|
|
|
|
Diagnostics: []protocol.Diagnostic{*diag},
|
|
|
|
Edit: protocol.WorkspaceEdit{},
|
|
|
|
}
|
|
|
|
for uri, edits := range fix.Edits {
|
|
|
|
if uri != pmh.Mod().URI() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
action.Edit.DocumentChanges = append(action.Edit.DocumentChanges, protocol.TextDocumentEdit{
|
|
|
|
TextDocument: protocol.VersionedTextDocumentIdentifier{
|
|
|
|
Version: pmh.Mod().Version(),
|
|
|
|
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
|
|
|
|
URI: protocol.URIFromSpanURI(pmh.Mod().URI()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Edits: edits,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
quickFixes = append(quickFixes, action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return quickFixes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sameDiagnostic(d protocol.Diagnostic, e source.Error) bool {
|
|
|
|
return d.Message == e.Message && protocol.CompareRange(d.Range, e.Range) == 0 && d.Source == e.Category
|
|
|
|
}
|
|
|
|
|
|
|
|
func goModTidy(ctx context.Context, snapshot source.Snapshot) (*protocol.CodeAction, error) {
|
|
|
|
mth, err := snapshot.ModTidyHandle(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uri := mth.ParseModHandle().Mod().URI()
|
2020-07-16 15:37:12 -06:00
|
|
|
_, m, _, err := mth.ParseModHandle().Parse(ctx, snapshot)
|
2020-07-20 14:56:12 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
left, err := mth.ParseModHandle().Mod().Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-16 15:37:12 -06:00
|
|
|
right, err := mth.TidiedContent(ctx, snapshot)
|
2020-07-20 14:56:12 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
edits := snapshot.View().Options().ComputeEdits(uri, string(left), string(right))
|
|
|
|
protocolEdits, err := source.ToProtocolEdits(m, edits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &protocol.CodeAction{
|
|
|
|
Title: "Tidy",
|
|
|
|
Kind: protocol.SourceOrganizeImports,
|
|
|
|
Edit: protocol.WorkspaceEdit{
|
|
|
|
DocumentChanges: []protocol.TextDocumentEdit{{
|
|
|
|
TextDocument: protocol.VersionedTextDocumentIdentifier{
|
|
|
|
Version: mth.ParseModHandle().Mod().Version(),
|
|
|
|
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
|
|
|
|
URI: protocol.URIFromSpanURI(uri),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Edits: protocolEdits,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|