2020-02-20 09:17:54 -07:00
|
|
|
package mod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"go/token"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/mod/modfile"
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-02-20 09:17:54 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Hover(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle, position protocol.Position) (*protocol.Hover, error) {
|
2020-06-10 23:11:52 -06:00
|
|
|
uri := snapshot.View().ModFile()
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
// For now, we only provide hover information for the view's go.mod file.
|
2020-06-10 23:11:52 -06:00
|
|
|
if uri == "" || fh.URI() != uri {
|
2020-02-20 09:17:54 -07:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "mod.Hover")
|
2020-02-20 09:17:54 -07:00
|
|
|
defer done()
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
// Get the position of the cursor.
|
2020-07-24 11:39:58 -06:00
|
|
|
pm, err := snapshot.ParseMod(ctx, fh)
|
2020-06-10 14:13:25 -06:00
|
|
|
if err != nil {
|
2020-06-15 20:21:06 -06:00
|
|
|
return nil, fmt.Errorf("getting modfile handle: %w", err)
|
2020-06-10 14:13:25 -06:00
|
|
|
}
|
2020-07-24 11:39:58 -06:00
|
|
|
spn, err := pm.Mapper.PointSpan(position)
|
2020-02-20 09:17:54 -07:00
|
|
|
if err != nil {
|
2020-06-15 20:21:06 -06:00
|
|
|
return nil, fmt.Errorf("computing cursor position: %w", err)
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
2020-07-24 11:39:58 -06:00
|
|
|
hoverRng, err := spn.Range(pm.Mapper.Converter)
|
2020-02-20 09:17:54 -07:00
|
|
|
if err != nil {
|
2020-06-15 20:21:06 -06:00
|
|
|
return nil, fmt.Errorf("computing hover range: %w", err)
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
// Confirm that the cursor is at the position of a require statement.
|
2020-02-20 09:17:54 -07:00
|
|
|
var req *modfile.Require
|
|
|
|
var startPos, endPos int
|
2020-07-24 11:39:58 -06:00
|
|
|
for _, r := range pm.File.Require {
|
2020-02-20 09:17:54 -07:00
|
|
|
dep := []byte(r.Mod.Path)
|
|
|
|
s, e := r.Syntax.Start.Byte, r.Syntax.End.Byte
|
2020-07-24 11:39:58 -06:00
|
|
|
i := bytes.Index(pm.Mapper.Content[s:e], dep)
|
2020-02-20 09:17:54 -07:00
|
|
|
if i == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Shift the start position to the location of the
|
|
|
|
// dependency within the require statement.
|
|
|
|
startPos, endPos = s+i, s+i+len(dep)
|
|
|
|
if token.Pos(startPos) <= hoverRng.Start && hoverRng.Start <= token.Pos(endPos) {
|
|
|
|
req = r
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
|
|
|
|
// The cursor position is not on a require statement.
|
|
|
|
if req == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the `go mod why` results for the given file.
|
2020-07-24 11:39:58 -06:00
|
|
|
why, err := snapshot.ModWhy(ctx)
|
2020-06-19 17:07:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-20 09:17:54 -07:00
|
|
|
explanation, ok := why[req.Mod.Path]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
|
2020-02-20 09:17:54 -07:00
|
|
|
// Get the range to highlight for the hover.
|
2020-07-24 11:39:58 -06:00
|
|
|
line, col, err := pm.Mapper.Converter.ToPosition(startPos)
|
2020-02-20 09:17:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
start := span.NewPoint(line, col, startPos)
|
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
line, col, err = pm.Mapper.Converter.ToPosition(endPos)
|
2020-02-20 09:17:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
end := span.NewPoint(line, col, endPos)
|
|
|
|
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
spn = span.New(fh.URI(), start, end)
|
2020-07-24 11:39:58 -06:00
|
|
|
rng, err := pm.Mapper.Range(spn)
|
2020-02-20 09:17:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options := snapshot.View().Options()
|
2020-06-15 20:21:06 -06:00
|
|
|
isPrivate := snapshot.View().IsGoPrivatePath(req.Mod.Path)
|
|
|
|
explanation = formatExplanation(explanation, req, options, isPrivate)
|
2020-02-20 09:17:54 -07:00
|
|
|
return &protocol.Hover{
|
|
|
|
Contents: protocol.MarkupContent{
|
|
|
|
Kind: options.PreferredContentFormat,
|
|
|
|
Value: explanation,
|
|
|
|
},
|
|
|
|
Range: rng,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:21:06 -06:00
|
|
|
func formatExplanation(text string, req *modfile.Require, options source.Options, isPrivate bool) string {
|
2020-02-20 09:17:54 -07:00
|
|
|
text = strings.TrimSuffix(text, "\n")
|
|
|
|
splt := strings.Split(text, "\n")
|
|
|
|
length := len(splt)
|
|
|
|
|
|
|
|
var b strings.Builder
|
|
|
|
// Write the heading as an H3.
|
|
|
|
b.WriteString("##" + splt[0])
|
|
|
|
if options.PreferredContentFormat == protocol.Markdown {
|
|
|
|
b.WriteString("\n\n")
|
|
|
|
} else {
|
|
|
|
b.WriteRune('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the explanation is 2 lines, then it is of the form:
|
|
|
|
// # golang.org/x/text/encoding
|
|
|
|
// (main module does not need package golang.org/x/text/encoding)
|
|
|
|
if length == 2 {
|
|
|
|
b.WriteString(splt[1])
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:21:06 -06:00
|
|
|
imp := splt[length-1] // import path
|
|
|
|
reference := imp
|
|
|
|
// See golang/go#36998: don't link to modules matching GOPRIVATE.
|
|
|
|
if !isPrivate && options.PreferredContentFormat == protocol.Markdown {
|
|
|
|
target := imp
|
|
|
|
if strings.ToLower(options.LinkTarget) == "pkg.go.dev" {
|
|
|
|
target = strings.Replace(target, req.Mod.Path, req.Mod.String(), 1)
|
|
|
|
}
|
|
|
|
reference = fmt.Sprintf("[%s](https://%s/%s)", imp, options.LinkTarget, target)
|
2020-02-20 09:17:54 -07:00
|
|
|
}
|
2020-06-15 20:21:06 -06:00
|
|
|
b.WriteString("This module is necessary because " + reference + " is imported in")
|
2020-02-20 09:17:54 -07:00
|
|
|
|
|
|
|
// If the explanation is 3 lines, then it is of the form:
|
|
|
|
// # golang.org/x/tools
|
|
|
|
// modtest
|
|
|
|
// golang.org/x/tools/go/packages
|
|
|
|
if length == 3 {
|
|
|
|
msg := fmt.Sprintf(" `%s`.", splt[1])
|
|
|
|
b.WriteString(msg)
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the explanation is more than 3 lines, then it is of the form:
|
|
|
|
// # golang.org/x/text/language
|
|
|
|
// rsc.io/quote
|
|
|
|
// rsc.io/sampler
|
|
|
|
// golang.org/x/text/language
|
|
|
|
b.WriteString(":\n```text")
|
|
|
|
dash := ""
|
|
|
|
for _, imp := range splt[1 : length-1] {
|
|
|
|
dash += "-"
|
|
|
|
b.WriteString("\n" + dash + " " + imp)
|
|
|
|
}
|
|
|
|
b.WriteString("\n```")
|
|
|
|
return b.String()
|
|
|
|
}
|