-
Notifications
You must be signed in to change notification settings - Fork 21
/
add.go
89 lines (76 loc) · 1.97 KB
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2020 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 main
import (
"fmt"
"go/ast"
"go/token"
"strings"
"rsc.io/rf/refactor"
)
func cmdAdd(snap *refactor.Snapshot, args string) error {
return cmdAddSub(snap, "add", args)
}
func cmdSub(snap *refactor.Snapshot, args string) error {
return cmdAddSub(snap, "sub", args)
}
func cmdAddSub(snap *refactor.Snapshot, cmd, args string) error {
item, expr, text := snap.EvalNext(args)
if expr == "" {
return newErrUsage("%s address text...", cmd)
}
if item == nil {
// Error already reported.
return nil
}
var pos, end token.Pos
switch item.Kind {
default:
return fmt.Errorf("TODO: %s after %s", cmd, item.Kind)
case refactor.ItemNotFound:
return newErrPrecondition("%s not found", item.Name)
case refactor.ItemConst, refactor.ItemFunc, refactor.ItemType, refactor.ItemVar, refactor.ItemField:
stack := snap.SyntaxAt(item.Obj.Pos())
if len(stack) == 0 {
panic("LOST " + item.Name)
}
after := stack[1]
switch after.(type) {
case *ast.ValueSpec, *ast.TypeSpec:
decl := stack[2].(*ast.GenDecl)
if decl.Lparen == token.NoPos {
after = decl
}
}
pos, end = nodeRange(snap, after)
case refactor.ItemFile:
_, srcFile := snap.FileByName(item.Name)
pos, end = snap.FileRange(srcFile.Package)
case refactor.ItemDir:
var dstPkg *refactor.Package
for _, pkg := range snap.Packages() {
if pkg.PkgPath == item.Name {
dstPkg = pkg
break
}
}
if dstPkg == nil {
return fmt.Errorf("no such directory %s", item.Name)
}
pos, end = snap.FileRange(dstPkg.Files[0].Syntax.Pos())
case refactor.ItemPos:
pos, end = item.Pos, item.End
}
var old string
if cmd == "sub" {
old = string(snap.Text(pos, end))
snap.DeleteAt(pos, end)
}
if cmd == "add" || strings.HasSuffix(old, "\n") {
// TODO: Is final \n a good idea?
text += "\n"
}
snap.InsertAt(end, text)
return nil
}