Debian Codegraph @

gopls/internal/golang/movetype.go

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package golang
6
7 import (
8 "context"
9 "fmt"
10 "go/ast"
11 "go/token"
12
13 "golang.org/x/tools/go/ast/inspector"
14 "golang.org/x/tools/gopls/internal/cache"
15 "golang.org/x/tools/gopls/internal/file"
16 "golang.org/x/tools/gopls/internal/protocol"
17 "golang.org/x/tools/internal/moreiters"
18 )
19
20 // MoveType moves the selected type declaration into a new package and updates all references.
21 func MoveType(ctx context.Context, fh file.Handle, snapshot *cache.Snapshot, loc protocol.Location, newPkgDir string) ([]protocol.DocumentChange, error) {
22 return nil, fmt.Errorf("MoveType: not yet supported")
23 }
24
25 // selectionContainsType returns the Cursor, GenDecl and TypeSpec of the type
26 // declaration that encloses cursor if one exists. Otherwise it returns false.
27 func selectionContainsType(cursor inspector.Cursor) (inspector.Cursor, *ast.GenDecl, *ast.TypeSpec, string, bool) {
28 declCur, ok := moreiters.First(cursor.Enclosing((*ast.GenDecl)(nil)))
29 if !ok {
30 return inspector.Cursor{}, &ast.GenDecl{}, &ast.TypeSpec{}, "", false
31 }
32
33 // Verify that we have a type declaration (e.g. not an import declaration).
34 declNode := declCur.Node().(*ast.GenDecl)
35 if declNode.Tok != token.TYPE {
36 return inspector.Cursor{}, &ast.GenDecl{}, &ast.TypeSpec{}, "", false
37 }
38
39 typSpec, ok := declNode.Specs[0].(*ast.TypeSpec)
40 if !ok {
41 return inspector.Cursor{}, &ast.GenDecl{}, &ast.TypeSpec{}, "", false
42 }
43
44 return declCur, declNode, declNode.Specs[0].(*ast.TypeSpec), typSpec.Name.Name, true
45 }