2020-02-06 12:50:26 -07:00
|
|
|
package mod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-02-26 12:11:30 -07:00
|
|
|
"strings"
|
2020-02-06 12:50:26 -07:00
|
|
|
|
2020-02-26 12:11:30 -07:00
|
|
|
"golang.org/x/mod/modfile"
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-03-10 21:09:39 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2020-02-06 12:50:26 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
2020-05-06 20:54:50 -06:00
|
|
|
// CodeLens computes code lens for a go.mod file.
|
2020-02-06 12:50:26 -07:00
|
|
|
func CodeLens(ctx context.Context, snapshot source.Snapshot, uri span.URI) ([]protocol.CodeLens, error) {
|
2020-05-06 20:54:50 -06:00
|
|
|
if !snapshot.View().Options().EnabledCodeLens[source.CommandUpgradeDependency] {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-02-06 12:50:26 -07:00
|
|
|
realURI, _ := snapshot.View().ModFiles()
|
|
|
|
if realURI == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// Only get code lens on the go.mod for the view.
|
|
|
|
if uri != realURI {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "mod.CodeLens", tag.URI.Of(realURI))
|
2020-02-06 12:50:26 -07:00
|
|
|
defer done()
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
fh, err := snapshot.GetFile(realURI)
|
2020-02-06 12:50:26 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
f, m, upgrades, err := snapshot.ModHandle(ctx, fh).Upgrades(ctx)
|
2020-02-14 09:52:17 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 12:50:26 -07:00
|
|
|
var codelens []protocol.CodeLens
|
2020-02-26 12:11:30 -07:00
|
|
|
var allUpgrades []string
|
2020-02-06 12:50:26 -07:00
|
|
|
for _, req := range f.Require {
|
|
|
|
dep := req.Mod.Path
|
|
|
|
latest, ok := upgrades[dep]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Get the range of the require directive.
|
2020-02-26 12:11:30 -07:00
|
|
|
rng, err := positionsToRange(uri, m, req.Syntax.Start, req.Syntax.End)
|
2020-02-06 12:50:26 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-26 12:11:30 -07:00
|
|
|
codelens = append(codelens, protocol.CodeLens{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
|
|
|
Title: fmt.Sprintf("Upgrade dependency to %s", latest),
|
2020-05-06 20:54:50 -06:00
|
|
|
Command: source.CommandUpgradeDependency,
|
2020-02-26 12:11:30 -07:00
|
|
|
Arguments: []interface{}{uri, dep},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
allUpgrades = append(allUpgrades, dep)
|
|
|
|
}
|
|
|
|
// If there is at least 1 upgrade, add an "Upgrade all dependencies" to the module statement.
|
|
|
|
if module := f.Module; len(allUpgrades) > 0 && module != nil && module.Syntax != nil {
|
|
|
|
// Get the range of the module directive.
|
|
|
|
rng, err := positionsToRange(uri, m, module.Syntax.Start, module.Syntax.End)
|
2020-02-06 12:50:26 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
codelens = append(codelens, protocol.CodeLens{
|
|
|
|
Range: rng,
|
|
|
|
Command: protocol.Command{
|
2020-02-26 12:11:30 -07:00
|
|
|
Title: "Upgrade all dependencies",
|
2020-05-06 20:54:50 -06:00
|
|
|
Command: source.CommandUpgradeDependency,
|
2020-02-26 12:11:30 -07:00
|
|
|
Arguments: []interface{}{uri, strings.Join(append([]string{"-u"}, allUpgrades...), " ")},
|
2020-02-06 12:50:26 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return codelens, err
|
|
|
|
}
|
2020-02-26 12:11:30 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|