mirror of
https://github.com/golang/go
synced 2024-11-18 23:05:06 -07:00
1fa568393b
Before renaming a variable, check the package to make sure that this renaming would not result in a conflict that could break the program. All of the implementation is taken from "refactor/rename" with the dependency on "go/loader" removed. Change-Id: Ib0782ec8f247a6df1750f2c8213f69186699ce1a Reviewed-on: https://go-review.googlesource.com/c/tools/+/183257 Run-TryBot: Suzy Mueller <suzmue@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
// 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.
|
|
|
|
package lsp
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/span"
|
|
)
|
|
|
|
func (s *Server) references(ctx context.Context, params *protocol.ReferenceParams) ([]protocol.Location, error) {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
view := s.session.ViewOf(uri)
|
|
f, m, err := getGoFile(ctx, view, uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn, err := m.PointSpan(params.Position)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rng, err := spn.Range(m.Converter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Find all references to the identifier at the position.
|
|
ident, err := source.Identifier(ctx, view, f, rng.Start)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
references, err := ident.References(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Get the location of each reference to return as the result.
|
|
locations := make([]protocol.Location, 0, len(references))
|
|
for _, ref := range references {
|
|
refSpan, err := ref.Range.Span()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, refM, err := getSourceFile(ctx, view, refSpan.URI())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
loc, err := refM.Location(refSpan)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
locations = append(locations, loc)
|
|
}
|
|
return locations, nil
|
|
}
|