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 167 168 169 170 171 172 173
|
// 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 mod
import (
"context"
"fmt"
"os"
"path/filepath"
"golang.org/x/mod/modfile"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/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: upgradeLenses,
source.CommandTidy.Name: tidyLens,
source.CommandVendor.Name: vendorLens,
}
}
func upgradeLenses(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.CodeLens, error) {
pm, err := snapshot.ParseMod(ctx, fh)
if err != nil || pm.File == nil {
return nil, err
}
if len(pm.File.Require) == 0 {
// Nothing to upgrade.
return nil, nil
}
upgradeTransitiveArgs, err := source.MarshalArgs(fh.URI(), false, []string{"-u", "all"})
if err != nil {
return nil, err
}
var requires []string
for _, req := range pm.File.Require {
requires = append(requires, req.Mod.Path)
}
upgradeDirectArgs, err := source.MarshalArgs(fh.URI(), false, requires)
if err != nil {
return nil, err
}
// Put the upgrade code lenses above the first require block or statement.
rng, err := firstRequireRange(fh, pm)
if err != nil {
return nil, err
}
return []protocol.CodeLens{
{
Range: rng,
Command: protocol.Command{
Title: "Upgrade transitive dependencies",
Command: source.CommandUpgradeDependency.ID(),
Arguments: upgradeTransitiveArgs,
},
},
{
Range: rng,
Command: protocol.Command{
Title: "Upgrade direct dependencies",
Command: source.CommandUpgradeDependency.ID(),
Arguments: upgradeDirectArgs,
},
},
}, nil
}
func tidyLens(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.CodeLens, error) {
pm, err := snapshot.ParseMod(ctx, fh)
if err != nil || pm.File == nil {
return nil, err
}
if len(pm.File.Require) == 0 {
// Nothing to vendor.
return nil, nil
}
goModArgs, err := source.MarshalArgs(fh.URI())
if err != nil {
return nil, err
}
rng, err := moduleStmtRange(fh, pm)
if err != nil {
return nil, err
}
return []protocol.CodeLens{{
Range: rng,
Command: protocol.Command{
Title: source.CommandTidy.Title,
Command: source.CommandTidy.ID(),
Arguments: goModArgs,
},
}}, nil
}
func vendorLens(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.CodeLens, error) {
pm, err := snapshot.ParseMod(ctx, fh)
if err != nil || pm.File == nil {
return nil, err
}
rng, err := moduleStmtRange(fh, pm)
if err != nil {
return nil, err
}
goModArgs, err := source.MarshalArgs(fh.URI())
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.ID(),
Arguments: goModArgs,
},
}}, nil
}
func moduleStmtRange(fh source.FileHandle, pm *source.ParsedModule) (protocol.Range, error) {
if pm.File == nil || pm.File.Module == nil || pm.File.Module.Syntax == nil {
return protocol.Range{}, fmt.Errorf("no module statement in %s", fh.URI())
}
syntax := pm.File.Module.Syntax
return lineToRange(pm.Mapper, fh.URI(), syntax.Start, syntax.End)
}
// firstRequireRange returns the range for the first "require" in the given
// go.mod file. This is either a require block or an individual require line.
func firstRequireRange(fh source.FileHandle, pm *source.ParsedModule) (protocol.Range, error) {
if len(pm.File.Require) == 0 {
return protocol.Range{}, fmt.Errorf("no requires in the file %s", fh.URI())
}
var start, end modfile.Position
for _, stmt := range pm.File.Syntax.Stmt {
if b, ok := stmt.(*modfile.LineBlock); ok && len(b.Token) == 1 && b.Token[0] == "require" {
start, end = b.Span()
break
}
}
firstRequire := pm.File.Require[0].Syntax
if start.Byte == 0 || firstRequire.Start.Byte < start.Byte {
start, end = firstRequire.Start, firstRequire.End
}
return lineToRange(pm.Mapper, fh.URI(), start, end)
}
func lineToRange(m *protocol.ColumnMapper, uri span.URI, start, end modfile.Position) (protocol.Range, error) {
line, col, err := m.Converter.ToPosition(start.Byte)
if err != nil {
return protocol.Range{}, err
}
s := span.NewPoint(line, col, start.Byte)
line, col, err = m.Converter.ToPosition(end.Byte)
if err != nil {
return protocol.Range{}, err
}
e := span.NewPoint(line, col, end.Byte)
return m.Range(span.New(uri, s, e))
}
|