2019-06-11 13:09:43 -06:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2019-06-18 08:23:37 -06:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
2019-06-27 12:01:56 -06:00
|
|
|
"bytes"
|
2019-06-18 08:23:37 -06:00
|
|
|
"context"
|
2019-06-20 13:24:17 -06:00
|
|
|
"go/ast"
|
2019-06-27 12:01:56 -06:00
|
|
|
"go/format"
|
2019-06-18 08:23:37 -06:00
|
|
|
"go/token"
|
|
|
|
"go/types"
|
2019-06-20 13:24:17 -06:00
|
|
|
"regexp"
|
2020-07-01 22:57:55 -06:00
|
|
|
"strings"
|
2019-06-18 08:23:37 -06:00
|
|
|
|
2019-06-11 13:09:43 -06:00
|
|
|
"golang.org/x/tools/go/types/typeutil"
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2019-08-19 17:28:08 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/diff"
|
2019-08-22 11:31:03 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-06-18 08:23:37 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-06-11 13:09:43 -06:00
|
|
|
"golang.org/x/tools/refactor/satisfy"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-06-18 08:23:37 -06:00
|
|
|
)
|
|
|
|
|
2019-06-11 13:09:43 -06:00
|
|
|
type renamer struct {
|
2019-06-21 15:00:02 -06:00
|
|
|
ctx context.Context
|
2019-06-11 13:09:43 -06:00
|
|
|
fset *token.FileSet
|
|
|
|
refs []*ReferenceInfo
|
|
|
|
objsToUpdate map[types.Object]bool
|
|
|
|
hadConflicts bool
|
|
|
|
errors string
|
|
|
|
from, to string
|
|
|
|
satisfyConstraints map[satisfy.Constraint]bool
|
|
|
|
packages map[*types.Package]Package // may include additional packages that are a rdep of pkg
|
|
|
|
msets typeutil.MethodSetCache
|
|
|
|
changeMethods bool
|
|
|
|
}
|
|
|
|
|
2019-08-22 11:31:03 -06:00
|
|
|
type PrepareItem struct {
|
2019-09-05 18:04:28 -06:00
|
|
|
Range protocol.Range
|
2019-08-22 11:31:03 -06:00
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
func PrepareRename(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position) (*PrepareItem, error) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.PrepareRename")
|
2019-08-22 11:31:03 -06:00
|
|
|
defer done()
|
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
qos, err := qualifiedObjsAtProtocolPos(ctx, s, f, pp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-08-22 11:31:03 -06:00
|
|
|
}
|
2020-03-26 21:25:15 -06:00
|
|
|
node, obj, pkg := qos[0].node, qos[0].obj, qos[0].sourcePkg
|
|
|
|
mr, err := posToMappedRange(s.View(), pkg, node.Pos(), node.End())
|
2019-12-17 22:06:31 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-08-22 11:31:03 -06:00
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
rng, err := mr.Range()
|
2019-09-05 18:04:28 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-26 21:25:15 -06:00
|
|
|
if _, isImport := node.(*ast.ImportSpec); isImport {
|
2019-12-17 22:06:31 -07:00
|
|
|
// We're not really renaming the import path.
|
|
|
|
rng.End = rng.Start
|
|
|
|
}
|
2019-08-22 11:31:03 -06:00
|
|
|
return &PrepareItem{
|
2019-09-05 18:04:28 -06:00
|
|
|
Range: rng,
|
2020-03-26 21:25:15 -06:00
|
|
|
Text: obj.Name(),
|
2019-08-22 11:31:03 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 08:23:37 -06:00
|
|
|
// Rename returns a map of TextEdits for each file modified when renaming a given identifier within a package.
|
2019-12-17 22:06:31 -07:00
|
|
|
func Rename(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position, newName string) (map[span.URI][]protocol.TextEdit, error) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.Rename")
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
2019-07-11 19:05:55 -06:00
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
qos, err := qualifiedObjsAtProtocolPos(ctx, s, f, pp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-08-21 20:40:36 -06:00
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
|
|
|
|
obj := qos[0].obj
|
|
|
|
pkg := qos[0].pkg
|
|
|
|
|
|
|
|
if obj.Name() == newName {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("old and new names are the same: %s", newName)
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
2019-08-22 11:31:03 -06:00
|
|
|
if !isValidIdentifier(newName) {
|
2019-12-17 22:06:31 -07:00
|
|
|
return nil, errors.Errorf("invalid identifier to rename: %q", newName)
|
2019-06-11 13:09:43 -06:00
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
if pkg == nil || pkg.IsIllTyped() {
|
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, errors.Errorf("package for %s is ill typed", f.URI())
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
2020-03-26 21:25:15 -06:00
|
|
|
refs, err := references(ctx, s, qos, true)
|
2019-06-18 08:23:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-11 13:09:43 -06:00
|
|
|
r := renamer{
|
2019-06-27 12:09:03 -06:00
|
|
|
ctx: ctx,
|
2019-12-17 22:06:31 -07:00
|
|
|
fset: s.View().Session().Cache().FileSet(),
|
2019-06-11 13:09:43 -06:00
|
|
|
refs: refs,
|
|
|
|
objsToUpdate: make(map[types.Object]bool),
|
2019-12-17 22:06:31 -07:00
|
|
|
from: obj.Name(),
|
2019-06-11 13:09:43 -06:00
|
|
|
to: newName,
|
|
|
|
packages: make(map[*types.Package]Package),
|
|
|
|
}
|
2020-07-11 22:26:29 -06:00
|
|
|
|
|
|
|
// A renaming initiated at an interface method indicates the
|
|
|
|
// intention to rename abstract and concrete methods as needed
|
|
|
|
// to preserve assignability.
|
|
|
|
for _, ref := range refs {
|
|
|
|
if obj, ok := ref.obj.(*types.Func); ok {
|
|
|
|
recv := obj.Type().(*types.Signature).Recv()
|
|
|
|
if recv != nil && isInterface(recv.Type().Underlying()) {
|
|
|
|
r.changeMethods = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-08 19:53:01 -06:00
|
|
|
for _, from := range refs {
|
2019-08-15 08:29:18 -06:00
|
|
|
r.packages[from.pkg.GetTypes()] = from.pkg
|
2019-07-08 19:53:01 -06:00
|
|
|
}
|
2019-06-11 13:09:43 -06:00
|
|
|
|
|
|
|
// Check that the renaming of the identifier is ok.
|
2019-08-15 08:29:18 -06:00
|
|
|
for _, ref := range refs {
|
|
|
|
r.check(ref.obj)
|
2019-08-16 10:23:59 -06:00
|
|
|
if r.hadConflicts { // one error is enough.
|
|
|
|
break
|
|
|
|
}
|
2019-06-11 13:09:43 -06:00
|
|
|
}
|
|
|
|
if r.hadConflicts {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf(r.errors)
|
2019-06-11 13:09:43 -06:00
|
|
|
}
|
|
|
|
|
2019-07-15 15:02:40 -06:00
|
|
|
changes, err := r.update()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-06 12:55:14 -06:00
|
|
|
result := make(map[span.URI][]protocol.TextEdit)
|
|
|
|
for uri, edits := range changes {
|
2019-09-16 16:17:51 -06:00
|
|
|
// These edits should really be associated with FileHandles for maximal correctness.
|
|
|
|
// For now, this is good enough.
|
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 := s.GetFile(ctx, uri)
|
2019-09-06 12:55:14 -06:00
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
data, err := fh.Read()
|
2019-09-16 16:17:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
converter := span.NewContentConverter(uri.Filename(), data)
|
|
|
|
m := &protocol.ColumnMapper{
|
|
|
|
URI: uri,
|
|
|
|
Converter: converter,
|
|
|
|
Content: data,
|
|
|
|
}
|
|
|
|
// Sort the edits first.
|
|
|
|
diff.SortTextEdits(edits)
|
2019-09-06 12:55:14 -06:00
|
|
|
protocolEdits, err := ToProtocolEdits(m, edits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[uri] = protocolEdits
|
2019-07-15 15:02:40 -06:00
|
|
|
}
|
2019-09-06 12:55:14 -06:00
|
|
|
return result, nil
|
2019-06-11 13:09:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename all references to the identifier.
|
2019-08-19 17:28:08 -06:00
|
|
|
func (r *renamer) update() (map[span.URI][]diff.TextEdit, error) {
|
|
|
|
result := make(map[span.URI][]diff.TextEdit)
|
2019-07-08 19:53:01 -06:00
|
|
|
seen := make(map[span.Span]bool)
|
2019-06-11 13:09:43 -06:00
|
|
|
|
2019-06-24 15:48:30 -06:00
|
|
|
docRegexp, err := regexp.Compile(`\b` + r.from + `\b`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-11 13:09:43 -06:00
|
|
|
for _, ref := range r.refs {
|
2019-08-26 22:26:45 -06:00
|
|
|
refSpan, err := ref.spanRange.Span()
|
2019-06-18 08:23:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-08 19:53:01 -06:00
|
|
|
if seen[refSpan] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[refSpan] = true
|
2019-06-18 08:23:37 -06:00
|
|
|
|
2019-06-27 12:01:56 -06:00
|
|
|
// Renaming a types.PkgName may result in the addition or removal of an identifier,
|
|
|
|
// so we deal with this separately.
|
|
|
|
if pkgName, ok := ref.obj.(*types.PkgName); ok && ref.isDeclaration {
|
|
|
|
edit, err := r.updatePkgName(pkgName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[refSpan.URI()] = append(result[refSpan.URI()], *edit)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the identifier with r.to.
|
2019-08-19 17:28:08 -06:00
|
|
|
edit := diff.TextEdit{
|
2019-06-18 08:23:37 -06:00
|
|
|
Span: refSpan,
|
2019-06-11 13:09:43 -06:00
|
|
|
NewText: r.to,
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
2019-06-27 12:09:03 -06:00
|
|
|
|
2019-06-11 13:09:43 -06:00
|
|
|
result[refSpan.URI()] = append(result[refSpan.URI()], edit)
|
2019-06-20 13:24:17 -06:00
|
|
|
|
2019-06-27 12:01:56 -06:00
|
|
|
if !ref.isDeclaration || ref.ident == nil { // uses do not have doc comments to update.
|
2019-06-27 10:17:07 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-15 08:29:18 -06:00
|
|
|
doc := r.docComment(ref.pkg, ref.ident)
|
2019-06-27 12:01:56 -06:00
|
|
|
if doc == nil {
|
2019-06-27 10:17:07 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-06-27 12:01:56 -06:00
|
|
|
// Perform the rename in doc comments declared in the original package.
|
2020-07-01 22:57:55 -06:00
|
|
|
// go/parser strips out \r\n returns from the comment text, so go
|
|
|
|
// line-by-line through the comment text to get the correct positions.
|
2019-06-27 10:17:07 -06:00
|
|
|
for _, comment := range doc.List {
|
2020-07-01 22:57:55 -06:00
|
|
|
lines := strings.Split(comment.Text, "\n")
|
|
|
|
tok := r.fset.File(comment.Pos())
|
|
|
|
commentLine := tok.Position(comment.Pos()).Line
|
|
|
|
for i, line := range lines {
|
|
|
|
lineStart := comment.Pos()
|
|
|
|
if i > 0 {
|
|
|
|
lineStart = tok.LineStart(commentLine + i)
|
|
|
|
}
|
|
|
|
for _, locs := range docRegexp.FindAllIndex([]byte(line), -1) {
|
|
|
|
rng := span.NewRange(r.fset, lineStart+token.Pos(locs[0]), lineStart+token.Pos(locs[1]))
|
|
|
|
spn, err := rng.Span()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[spn.URI()] = append(result[spn.URI()], diff.TextEdit{
|
|
|
|
Span: spn,
|
|
|
|
NewText: r.to,
|
|
|
|
})
|
2019-06-20 13:24:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
|
|
|
|
2019-06-11 13:09:43 -06:00
|
|
|
return result, nil
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
2019-06-20 13:24:17 -06:00
|
|
|
|
|
|
|
// docComment returns the doc for an identifier.
|
|
|
|
func (r *renamer) docComment(pkg Package, id *ast.Ident) *ast.CommentGroup {
|
2020-03-27 10:52:40 -06:00
|
|
|
_, nodes, _ := pathEnclosingInterval(r.fset, pkg, id.Pos(), id.End())
|
2019-06-20 13:24:17 -06:00
|
|
|
for _, node := range nodes {
|
|
|
|
switch decl := node.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
return decl.Doc
|
|
|
|
case *ast.Field:
|
|
|
|
return decl.Doc
|
|
|
|
case *ast.GenDecl:
|
|
|
|
return decl.Doc
|
|
|
|
// For {Type,Value}Spec, if the doc on the spec is absent,
|
|
|
|
// search for the enclosing GenDecl
|
|
|
|
case *ast.TypeSpec:
|
|
|
|
if decl.Doc != nil {
|
|
|
|
return decl.Doc
|
|
|
|
}
|
|
|
|
case *ast.ValueSpec:
|
|
|
|
if decl.Doc != nil {
|
|
|
|
return decl.Doc
|
|
|
|
}
|
|
|
|
case *ast.Ident:
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-27 12:01:56 -06:00
|
|
|
|
|
|
|
// updatePkgName returns the updates to rename a pkgName in the import spec
|
2019-08-19 17:28:08 -06:00
|
|
|
func (r *renamer) updatePkgName(pkgName *types.PkgName) (*diff.TextEdit, error) {
|
2019-06-27 12:01:56 -06:00
|
|
|
// Modify ImportSpec syntax to add or remove the Name as needed.
|
|
|
|
pkg := r.packages[pkgName.Pkg()]
|
2020-03-27 10:52:40 -06:00
|
|
|
_, path, _ := pathEnclosingInterval(r.fset, pkg, pkgName.Pos(), pkgName.Pos())
|
2019-06-27 12:01:56 -06:00
|
|
|
if len(path) < 2 {
|
2019-07-09 15:52:23 -06:00
|
|
|
return nil, errors.Errorf("no path enclosing interval for %s", pkgName.Name())
|
2019-06-27 12:01:56 -06:00
|
|
|
}
|
|
|
|
spec, ok := path[1].(*ast.ImportSpec)
|
|
|
|
if !ok {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("failed to update PkgName for %s", pkgName.Name())
|
2019-06-27 12:01:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var astIdent *ast.Ident // will be nil if ident is removed
|
|
|
|
if pkgName.Imported().Name() != r.to {
|
|
|
|
// ImportSpec.Name needed
|
|
|
|
astIdent = &ast.Ident{NamePos: spec.Path.Pos(), Name: r.to}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a copy of the ident that just has the name and path.
|
|
|
|
updated := &ast.ImportSpec{
|
|
|
|
Name: astIdent,
|
|
|
|
Path: spec.Path,
|
|
|
|
EndPos: spec.EndPos,
|
|
|
|
}
|
|
|
|
|
|
|
|
rng := span.NewRange(r.fset, spec.Pos(), spec.End())
|
|
|
|
spn, err := rng.Span()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
format.Node(&buf, r.fset, updated)
|
|
|
|
newText := buf.String()
|
|
|
|
|
2019-08-19 17:28:08 -06:00
|
|
|
return &diff.TextEdit{
|
2019-06-27 12:01:56 -06:00
|
|
|
Span: spn,
|
|
|
|
NewText: newText,
|
|
|
|
}, nil
|
|
|
|
}
|