-
Notifications
You must be signed in to change notification settings - Fork 1
/
code_lens.go
166 lines (159 loc) · 4.5 KB
/
code_lens.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package mod
import (
"context"
"fmt"
"os"
"path/filepath"
"golang.org/x/mod/modfile"
"github.com/april1989/origin-go-tools/internal/lsp/protocol"
"github.com/april1989/origin-go-tools/internal/lsp/source"
"github.com/april1989/origin-go-tools/internal/span"
)
// LensFuncs returns the supported lensFuncs for go.mod files.
func LensFuncs() map[string]source.LensFunc {
return map[string]source.LensFunc{
source.CommandUpgradeDependency.Name: upgradeLens,
source.CommandTidy.Name: tidyLens,
source.CommandVendor.Name: vendorLens,
}
}
func upgradeLens(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.CodeLens, error) {
pm, err := snapshot.ParseMod(ctx, fh)
if err != nil {
return nil, err
}
module := pm.File.Module
if module == nil || module.Syntax == nil {
return nil, nil
}
upgrades, err := snapshot.ModUpgrade(ctx, fh)
if err != nil {
return nil, err
}
var (
codelenses []protocol.CodeLens
allUpgrades []string
)
for _, req := range pm.File.Require {
dep := req.Mod.Path
latest, ok := upgrades[dep]
if !ok {
continue
}
// Get the range of the require directive.
rng, err := positionsToRange(fh.URI(), pm.Mapper, req.Syntax.Start, req.Syntax.End)
if err != nil {
return nil, err
}
upgradeDepArgs, err := source.MarshalArgs(fh.URI(), []string{dep})
if err != nil {
return nil, err
}
codelenses = append(codelenses, protocol.CodeLens{
Range: rng,
Command: protocol.Command{
Title: fmt.Sprintf("Upgrade dependency to %s", latest),
Command: source.CommandUpgradeDependency.Name,
Arguments: upgradeDepArgs,
},
})
allUpgrades = append(allUpgrades, dep)
}
// If there is at least 1 upgrade, add "Upgrade all dependencies" to
// the module statement.
if len(allUpgrades) > 0 {
upgradeDepArgs, err := source.MarshalArgs(fh.URI(), append([]string{"-u"}, allUpgrades...))
if err != nil {
return nil, err
}
// Get the range of the module directive.
moduleRng, err := positionsToRange(pm.Mapper.URI, pm.Mapper, module.Syntax.Start, module.Syntax.End)
if err != nil {
return nil, err
}
codelenses = append(codelenses, protocol.CodeLens{
Range: moduleRng,
Command: protocol.Command{
Title: "Upgrade all dependencies",
Command: source.CommandUpgradeDependency.Name,
Arguments: upgradeDepArgs,
},
})
}
return codelenses, err
}
func tidyLens(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.CodeLens, error) {
goModArgs, err := source.MarshalArgs(fh.URI())
if err != nil {
return nil, err
}
tidied, err := snapshot.ModTidy(ctx, fh)
if err != nil {
return nil, err
}
if len(tidied.Errors) == 0 {
return nil, nil
}
pm, err := snapshot.ParseMod(ctx, fh)
if err != nil {
return nil, err
}
rng, err := positionsToRange(pm.Mapper.URI, pm.Mapper, pm.File.Module.Syntax.Start, pm.File.Module.Syntax.End)
if err != nil {
return nil, err
}
return []protocol.CodeLens{{
Range: rng,
Command: protocol.Command{
Title: "Tidy module",
Command: source.CommandTidy.Name,
Arguments: goModArgs,
},
}}, err
}
func vendorLens(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.CodeLens, error) {
goModArgs, err := source.MarshalArgs(fh.URI())
if err != nil {
return nil, err
}
pm, err := snapshot.ParseMod(ctx, fh)
if err != nil {
return nil, err
}
rng, err := positionsToRange(pm.Mapper.URI, pm.Mapper, pm.File.Module.Syntax.Start, pm.File.Module.Syntax.End)
if err != nil {
return nil, err
}
// Change the message depending on whether or not the module already has a
// vendor directory.
title := "Create vendor directory"
vendorDir := filepath.Join(filepath.Dir(fh.URI().Filename()), "vendor")
if info, _ := os.Stat(vendorDir); info != nil && info.IsDir() {
title = "Sync vendor directory"
}
return []protocol.CodeLens{{
Range: rng,
Command: protocol.Command{
Title: title,
Command: source.CommandVendor.Name,
Arguments: goModArgs,
},
}}, nil
}
func positionsToRange(uri span.URI, m *protocol.ColumnMapper, s, e modfile.Position) (protocol.Range, error) {
line, col, err := m.Converter.ToPosition(s.Byte)
if err != nil {
return protocol.Range{}, err
}
start := span.NewPoint(line, col, s.Byte)
line, col, err = m.Converter.ToPosition(e.Byte)
if err != nil {
return protocol.Range{}, err
}
end := span.NewPoint(line, col, e.Byte)
rng, err := m.Range(span.New(uri, start, end))
if err != nil {
return protocol.Range{}, err
}
return rng, err
}