| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mod |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
|
| 13 |
"golang.org/x/mod/modfile" |
| 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/gopls/internal/protocol/command" |
| 18 |
"golang.org/x/tools/gopls/internal/settings" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
func CodeLensSources() map[settings.CodeLensSource]cache.CodeLensSourceFunc { |
| 23 |
return map[settings.CodeLensSource]cache.CodeLensSourceFunc{ |
| 24 |
settings.CodeLensUpgradeDependency: upgradeLenses, |
| 25 |
settings.CodeLensTidy: tidyLens, |
| 26 |
settings.CodeLensVendor: vendorLens, |
| 27 |
settings.CodeLensVulncheck: vulncheckLenses, |
| 28 |
settings.CodeLensRunGovulncheck: runGovulncheckLenses, |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func upgradeLenses(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.CodeLens, error) { |
| 33 |
pm, err := snapshot.ParseMod(ctx, fh) |
| 34 |
if err != nil || pm.File == nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
uri := fh.URI() |
| 38 |
reset := command.NewResetGoModDiagnosticsCommand("Reset go.mod diagnostics", command.ResetGoModDiagnosticsArgs{URIArg: command.URIArg{URI: uri}}) |
| 39 |
|
| 40 |
modrng, err := moduleStmtRange(fh, pm) |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
lenses := []protocol.CodeLens{{Range: modrng, Command: reset}} |
| 45 |
if len(pm.File.Require) == 0 { |
| 46 |
|
| 47 |
return lenses, nil |
| 48 |
} |
| 49 |
var requires []string |
| 50 |
for _, req := range pm.File.Require { |
| 51 |
requires = append(requires, req.Mod.Path) |
| 52 |
} |
| 53 |
checkUpgrade := command.NewCheckUpgradesCommand("Check for upgrades", command.CheckUpgradesArgs{ |
| 54 |
URI: uri, |
| 55 |
Modules: requires, |
| 56 |
}) |
| 57 |
upgradeTransitive := command.NewUpgradeDependencyCommand("Upgrade transitive dependencies", command.DependencyArgs{ |
| 58 |
URI: uri, |
| 59 |
AddRequire: false, |
| 60 |
GoCmdArgs: []string{"-d", "-u", "-t", "./..."}, |
| 61 |
}) |
| 62 |
upgradeDirect := command.NewUpgradeDependencyCommand("Upgrade direct dependencies", command.DependencyArgs{ |
| 63 |
URI: uri, |
| 64 |
AddRequire: false, |
| 65 |
GoCmdArgs: append([]string{"-d"}, requires...), |
| 66 |
}) |
| 67 |
|
| 68 |
|
| 69 |
rng, err := firstRequireRange(fh, pm) |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
|
| 74 |
return append(lenses, []protocol.CodeLens{ |
| 75 |
{Range: rng, Command: checkUpgrade}, |
| 76 |
{Range: rng, Command: upgradeTransitive}, |
| 77 |
{Range: rng, Command: upgradeDirect}, |
| 78 |
}...), nil |
| 79 |
} |
| 80 |
|
| 81 |
func tidyLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.CodeLens, error) { |
| 82 |
pm, err := snapshot.ParseMod(ctx, fh) |
| 83 |
if err != nil || pm.File == nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
uri := fh.URI() |
| 87 |
cmd := command.NewTidyCommand("Run go mod tidy", command.URIArgs{URIs: []protocol.DocumentURI{uri}}) |
| 88 |
rng, err := moduleStmtRange(fh, pm) |
| 89 |
if err != nil { |
| 90 |
return nil, err |
| 91 |
} |
| 92 |
return []protocol.CodeLens{{ |
| 93 |
Range: rng, |
| 94 |
Command: cmd, |
| 95 |
}}, nil |
| 96 |
} |
| 97 |
|
| 98 |
func vendorLens(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.CodeLens, error) { |
| 99 |
pm, err := snapshot.ParseMod(ctx, fh) |
| 100 |
if err != nil || pm.File == nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
if len(pm.File.Require) == 0 { |
| 104 |
|
| 105 |
return nil, nil |
| 106 |
} |
| 107 |
rng, err := moduleStmtRange(fh, pm) |
| 108 |
if err != nil { |
| 109 |
return nil, err |
| 110 |
} |
| 111 |
title := "Create vendor directory" |
| 112 |
uri := fh.URI() |
| 113 |
cmd := command.NewVendorCommand(title, command.URIArg{URI: uri}) |
| 114 |
|
| 115 |
|
| 116 |
vendorDir := filepath.Join(fh.URI().DirPath(), "vendor") |
| 117 |
if info, _ := os.Stat(vendorDir); info != nil && info.IsDir() { |
| 118 |
title = "Sync vendor directory" |
| 119 |
} |
| 120 |
return []protocol.CodeLens{{Range: rng, Command: cmd}}, nil |
| 121 |
} |
| 122 |
|
| 123 |
func moduleStmtRange(fh file.Handle, pm *cache.ParsedModule) (protocol.Range, error) { |
| 124 |
if pm.File == nil || pm.File.Module == nil || pm.File.Module.Syntax == nil { |
| 125 |
return protocol.Range{}, fmt.Errorf("no module statement in %s", fh.URI()) |
| 126 |
} |
| 127 |
syntax := pm.File.Module.Syntax |
| 128 |
return pm.Mapper.OffsetRange(syntax.Start.Byte, syntax.End.Byte) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
func firstRequireRange(fh file.Handle, pm *cache.ParsedModule) (protocol.Range, error) { |
| 134 |
if len(pm.File.Require) == 0 { |
| 135 |
return protocol.Range{}, fmt.Errorf("no requires in the file %s", fh.URI()) |
| 136 |
} |
| 137 |
var start, end modfile.Position |
| 138 |
for _, stmt := range pm.File.Syntax.Stmt { |
| 139 |
if b, ok := stmt.(*modfile.LineBlock); ok && len(b.Token) == 1 && b.Token[0] == "require" { |
| 140 |
start, end = b.Span() |
| 141 |
break |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
firstRequire := pm.File.Require[0].Syntax |
| 146 |
if start.Byte == 0 || firstRequire.Start.Byte < start.Byte { |
| 147 |
start, end = firstRequire.Start, firstRequire.End |
| 148 |
} |
| 149 |
return pm.Mapper.OffsetRange(start.Byte, end.Byte) |
| 150 |
} |
| 151 |
|
| 152 |
func vulncheckLenses(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.CodeLens, error) { |
| 153 |
pm, err := snapshot.ParseMod(ctx, fh) |
| 154 |
if err != nil || pm.File == nil { |
| 155 |
return nil, err |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
uri := fh.URI() |
| 161 |
rng, err := moduleStmtRange(fh, pm) |
| 162 |
if err != nil { |
| 163 |
return nil, err |
| 164 |
} |
| 165 |
|
| 166 |
vulncheck := command.NewVulncheckCommand("Run govulncheck", command.VulncheckArgs{ |
| 167 |
URI: uri, |
| 168 |
Pattern: "./...", |
| 169 |
}) |
| 170 |
return []protocol.CodeLens{ |
| 171 |
{Range: rng, Command: vulncheck}, |
| 172 |
}, nil |
| 173 |
} |
| 174 |
|
| 175 |
func runGovulncheckLenses(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]protocol.CodeLens, error) { |
| 176 |
|
| 177 |
if snapshot.Options().UserOptions.UIOptions.Codelenses[settings.CodeLensVulncheck] { |
| 178 |
return nil, nil |
| 179 |
} |
| 180 |
|
| 181 |
pm, err := snapshot.ParseMod(ctx, fh) |
| 182 |
if err != nil || pm.File == nil { |
| 183 |
return nil, err |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
uri := fh.URI() |
| 189 |
rng, err := moduleStmtRange(fh, pm) |
| 190 |
if err != nil { |
| 191 |
return nil, err |
| 192 |
} |
| 193 |
|
| 194 |
vulncheck := command.NewRunGovulncheckCommand("Run govulncheck", command.VulncheckArgs{ |
| 195 |
URI: uri, |
| 196 |
Pattern: "./...", |
| 197 |
}) |
| 198 |
return []protocol.CodeLens{ |
| 199 |
{Range: rng, Command: vulncheck}, |
| 200 |
}, nil |
| 201 |
} |