Skip to content

Commit

Permalink
gopls/internal/golang: move from lsp/source
Browse files Browse the repository at this point in the history
Change-Id: If6d3ba6327cdb9ecd4d693a9d6eff39f74bc9e25
Reviewed-on: https://go-review.googlesource.com/c/tools/+/557740
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
adonovan committed Jan 24, 2024
1 parent 129144e commit 6823da4
Show file tree
Hide file tree
Showing 104 changed files with 215 additions and 227 deletions.
2 changes: 1 addition & 1 deletion gopls/doc/analyzers.md
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ This analyzer will suggest a fix to declare this method:

(At least, it appears to behave that way, but technically it
doesn't use the SuggestedFix mechanism and the stub is created by
logic in gopls's source.stub function.)
logic in gopls's golang.stub function.)

[Full documentation](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/stubmethods)

Expand Down
2 changes: 1 addition & 1 deletion gopls/doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Args:
// the analysis.Diagnostic with a SuggestedFix containing no edits.
//
// For fixes suggested by code actions, this is a string agreed
// upon by the code action and source.ApplyFix.
// upon by the code action and golang.ApplyFix.
"Fix": string,
// The file URI for the document to fix.
"URI": string,
Expand Down
2 changes: 1 addition & 1 deletion gopls/doc/design/implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ provided as a debugging aid (but see
[lsp]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/lsp
[lsprpc]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/lsprpc
[memoize]: https://github.com/golang/tools/tree/master/internal/memoize
[metadata]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/cache/metadata
[metadata]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/lsp/cache/metadata
[methodsets]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/lsp/cache/methodsets
[mod]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/mod
[parsego]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/lsp/cache/parsego
Expand Down
8 changes: 4 additions & 4 deletions gopls/doc/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
"github.com/jba/printsrc"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/gopls/internal/golang"
"golang.org/x/tools/gopls/internal/lsp/command"
"golang.org/x/tools/gopls/internal/lsp/command/commandmeta"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/gopls/internal/mod"
"golang.org/x/tools/gopls/internal/settings"
"golang.org/x/tools/gopls/internal/util/safetoken"
Expand Down Expand Up @@ -124,7 +124,7 @@ func loadAPI() (*settings.APIJSON, error) {
for _, c := range api.Commands {
c.Command = command.ID(c.Command)
}
api.Hints = loadHints(source.AllInlayHints)
api.Hints = loadHints(golang.AllInlayHints)
for _, category := range []reflect.Value{
reflect.ValueOf(defaults.UserOptions),
} {
Expand Down Expand Up @@ -481,7 +481,7 @@ func structDoc(fields []*commandmeta.Field, level int) string {

func loadLenses(commands []*settings.CommandJSON) []*settings.LensJSON {
all := map[command.Command]struct{}{}
for k := range source.LensFuncs() {
for k := range golang.LensFuncs() {
all[k] = struct{}{}
}
for k := range mod.LensFuncs() {
Expand Down Expand Up @@ -524,7 +524,7 @@ func loadAnalyzers(m map[string]*settings.Analyzer) []*settings.AnalyzerJSON {
return json
}

func loadHints(m map[string]*source.Hint) []*settings.HintJSON {
func loadHints(m map[string]*golang.Hint) []*settings.HintJSON {
var sorted []string
for _, h := range m {
sorted = append(sorted, h.Name)
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/analysis/embeddirective/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

// Package embeddirective defines an Analyzer that validates //go:embed directives.
// The analyzer defers fixes to its parent source.Analyzer.
// The analyzer defers fixes to its parent golang.Analyzer.
//
// # Analyzer embed
//
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/analysis/stubmethods/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
//
// (At least, it appears to behave that way, but technically it
// doesn't use the SuggestedFix mechanism and the stub is created by
// logic in gopls's source.stub function.)
// logic in gopls's golang.stub function.)
package stubmethods
2 changes: 1 addition & 1 deletion gopls/internal/analysis/unusedparams/unusedparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func run(pass *analysis.Pass) (any, error) {
}
// This diagnostic carries both an edit-based fix to
// rename the unused parameter, and a command-based fix
// to remove it (see source.RemoveUnusedParameter).
// to remove it (see golang.RemoveUnusedParameter).
pass.Report(analysis.Diagnostic{
Pos: start,
End: end,
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/cmd/codelens.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *codelens) Run(ctx context.Context, args ...string) error {
// Override the default setting for codelenses[Test], which is
// off by default because VS Code has a superior client-side
// implementation. But this client is not VS Code.
// See source.LensFuncs().
// See golang.LensFuncs().
origOptions := r.app.options
r.app.options = func(opts *settings.Options) {
origOptions(opts)
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func SetBudget(new int64) (old int64) {
// The current design instead exploits a trick from the cache
// implementation used by the go command: writes of small files are in
// practice atomic (all or nothing) on all platforms.
// (See GOROOT/src/cmd/go/internal/cache/cache.go.)
// (See GOROOT/src/cmd/go/internal/lsp/cache/cache.go.)
//
// Russ Cox notes: "all file systems use an rwlock around every file
// system block, including data blocks, so any writes or reads within
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package source
package golang

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package source
package golang

import (
"context"
Expand All @@ -25,7 +25,7 @@ import (

// PrepareCallHierarchy returns an array of CallHierarchyItem for a file and the position within the file.
func PrepareCallHierarchy(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp protocol.Position) ([]protocol.CallHierarchyItem, error) {
ctx, done := event.Start(ctx, "source.PrepareCallHierarchy")
ctx, done := event.Start(ctx, "golang.PrepareCallHierarchy")
defer done()

pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI())
Expand Down Expand Up @@ -66,7 +66,7 @@ func PrepareCallHierarchy(ctx context.Context, snapshot *cache.Snapshot, fh file

// IncomingCalls returns an array of CallHierarchyIncomingCall for a file and the position within the file.
func IncomingCalls(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pos protocol.Position) ([]protocol.CallHierarchyIncomingCall, error) {
ctx, done := event.Start(ctx, "source.IncomingCalls")
ctx, done := event.Start(ctx, "golang.IncomingCalls")
defer done()

refs, err := references(ctx, snapshot, fh, pos, false)
Expand Down Expand Up @@ -180,7 +180,7 @@ outer:

// OutgoingCalls returns an array of CallHierarchyOutgoingCall for a file and the position within the file.
func OutgoingCalls(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp protocol.Position) ([]protocol.CallHierarchyOutgoingCall, error) {
ctx, done := event.Start(ctx, "source.OutgoingCalls")
ctx, done := event.Start(ctx, "golang.OutgoingCalls")
defer done()

pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package source
package golang

import (
"go/ast"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package source
package golang

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package source
package golang

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package source
package golang

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//go:build !go1.19
// +build !go1.19

package source
package golang

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//go:build !go1.19
// +build !go1.19

package source
package golang

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//go:build go1.19
// +build go1.19

package source
package golang

// Starting with go1.19, the formatting of comments has changed, and there
// is a new package (go/doc/comment) for processing them.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/gopls/internal/file"
"golang.org/x/tools/gopls/internal/golang"
"golang.org/x/tools/gopls/internal/golang/completion/snippet"
"golang.org/x/tools/gopls/internal/lsp/cache"
"golang.org/x/tools/gopls/internal/lsp/cache/metadata"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/gopls/internal/lsp/source/completion/snippet"
"golang.org/x/tools/gopls/internal/settings"
goplsastutil "golang.org/x/tools/gopls/internal/util/astutil"
"golang.org/x/tools/gopls/internal/util/safetoken"
Expand Down Expand Up @@ -173,7 +173,7 @@ type completer struct {
snapshot *cache.Snapshot
pkg *cache.Package
qf types.Qualifier // for qualifying typed expressions
mq source.MetadataQualifier // for syntactic qualifying
mq golang.MetadataQualifier // for syntactic qualifying
opts *completionOptions

// completionContext contains information about the trigger for this
Expand Down Expand Up @@ -453,7 +453,7 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p

startTime := time.Now()

pkg, pgf, err := source.NarrowestPackageForFile(ctx, snapshot, fh.URI())
pkg, pgf, err := golang.NarrowestPackageForFile(ctx, snapshot, fh.URI())
if err != nil || pgf.File.Package == token.NoPos {
// If we can't parse this file or find position for the package
// keyword, it may be missing a package declaration. Try offering
Expand Down Expand Up @@ -522,15 +522,15 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
}

// Collect all surrounding scopes, innermost first.
scopes := source.CollectScopes(pkg.GetTypesInfo(), path, pos)
scopes := golang.CollectScopes(pkg.GetTypesInfo(), path, pos)
scopes = append(scopes, pkg.GetTypes().Scope(), types.Universe)

opts := snapshot.Options()
c := &completer{
pkg: pkg,
snapshot: snapshot,
qf: typesutil.FileQualifier(pgf.File, pkg.GetTypes(), pkg.GetTypesInfo()),
mq: source.MetadataQualifierForFile(snapshot, pgf.File, pkg.Metadata()),
mq: golang.MetadataQualifierForFile(snapshot, pgf.File, pkg.Metadata()),
completionContext: completionContext{
triggerCharacter: protoContext.TriggerCharacter,
triggerKind: protoContext.TriggerKind,
Expand Down Expand Up @@ -1158,7 +1158,7 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
imp := pkgName.Imported()

// Known direct dependency? Expand using type information.
if _, ok := c.pkg.Metadata().DepsByPkgPath[source.PackagePath(imp.Path())]; ok {
if _, ok := c.pkg.Metadata().DepsByPkgPath[golang.PackagePath(imp.Path())]; ok {
c.packageMembers(imp, stdScore, nil, c.deepState.enqueue)
return nil
}
Expand Down Expand Up @@ -1210,7 +1210,7 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
if err != nil {
return err
}
known := make(map[source.PackagePath]*metadata.Package)
known := make(map[golang.PackagePath]*metadata.Package)
for _, mp := range all {
if mp.Name == "main" {
continue // not importable
Expand Down Expand Up @@ -1373,7 +1373,7 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
// Extract the package-level candidates using a quick parse.
var g errgroup.Group
for _, path := range paths {
mp := known[source.PackagePath(path)]
mp := known[golang.PackagePath(path)]
for _, uri := range mp.CompiledGoFiles {
uri := uri
g.Go(func() error {
Expand All @@ -1396,7 +1396,7 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
mu.Lock()
defer mu.Unlock()
// TODO(adonovan): what if the actual package has a vendor/ prefix?
if _, ok := known[source.PackagePath(pkgExport.Fix.StmtInfo.ImportPath)]; ok {
if _, ok := known[golang.PackagePath(pkgExport.Fix.StmtInfo.ImportPath)]; ok {
return // We got this one above.
}

Expand Down Expand Up @@ -1585,7 +1585,7 @@ func (c *completer) lexical(ctx context.Context) error {
}

if c.inference.objType != nil {
if named, _ := source.Deref(c.inference.objType).(*types.Named); named != nil {
if named, _ := golang.Deref(c.inference.objType).(*types.Named); named != nil {
// If we expected a named type, check the type's package for
// completion items. This is useful when the current file hasn't
// imported the type's package yet.
Expand All @@ -1596,7 +1596,7 @@ func (c *completer) lexical(ctx context.Context) error {
// Make sure the package name isn't already in use by another
// object, and that this file doesn't import the package yet.
// TODO(adonovan): what if pkg.Path has vendor/ prefix?
if _, ok := seen[pkg.Name()]; !ok && pkg != c.pkg.GetTypes() && !alreadyImports(c.file, source.ImportPath(pkg.Path())) {
if _, ok := seen[pkg.Name()]; !ok && pkg != c.pkg.GetTypes() && !alreadyImports(c.file, golang.ImportPath(pkg.Path())) {
seen[pkg.Name()] = struct{}{}
obj := types.NewPkgName(0, nil, pkg.Name(), pkg)
imp := &importInfo{
Expand Down Expand Up @@ -1651,7 +1651,7 @@ func (c *completer) injectType(ctx context.Context, t types.Type) {
return
}

t = source.Deref(t)
t = golang.Deref(t)

// If we have an expected type and it is _not_ a named type, handle
// it specially. Non-named types like "[]int" will never be
Expand Down Expand Up @@ -1699,7 +1699,7 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
if err != nil {
return err
}
pkgNameByPath := make(map[source.PackagePath]string)
pkgNameByPath := make(map[golang.PackagePath]string)
var paths []string // actually PackagePaths
for _, mp := range all {
if mp.ForTest != "" {
Expand Down Expand Up @@ -1737,7 +1737,7 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
})

for _, path := range paths {
name := pkgNameByPath[source.PackagePath(path)]
name := pkgNameByPath[golang.PackagePath(path)]
if _, ok := seen[name]; ok {
continue
}
Expand Down Expand Up @@ -1802,7 +1802,7 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
}

// alreadyImports reports whether f has an import with the specified path.
func alreadyImports(f *ast.File, path source.ImportPath) bool {
func alreadyImports(f *ast.File, path golang.ImportPath) bool {
for _, s := range f.Imports {
if metadata.UnquoteImportPath(s) == path {
return true
Expand Down Expand Up @@ -1898,7 +1898,7 @@ func enclosingCompositeLiteral(path []ast.Node, pos token.Pos, info *types.Info)

clInfo := compLitInfo{
cl: n,
clType: source.Deref(tv.Type).Underlying(),
clType: golang.Deref(tv.Type).Underlying(),
}

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
"unicode"
"unicode/utf8"

"golang.org/x/tools/gopls/internal/golang"
"golang.org/x/tools/gopls/internal/golang/completion/snippet"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/gopls/internal/lsp/source/completion/snippet"
)

// some function definitions in test files can be completed
// So far, TestFoo(t *testing.T), TestMain(m *testing.M)
// BenchmarkFoo(b *testing.B), FuzzFoo(f *testing.F)

// path[0] is known to be *ast.Ident
func definition(path []ast.Node, obj types.Object, pgf *source.ParsedGoFile) ([]CompletionItem, *Selection) {
func definition(path []ast.Node, obj types.Object, pgf *golang.ParsedGoFile) ([]CompletionItem, *Selection) {
if _, ok := obj.(*types.Func); !ok {
return nil, nil // not a function at all
}
Expand Down
Loading

0 comments on commit 6823da4

Please sign in to comment.