2019-04-24 09:33:45 -06:00
|
|
|
// Copyright 2018 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 lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-17 11:45:50 -06:00
|
|
|
"fmt"
|
2019-04-24 09:33:45 -06:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLinkParams) ([]protocol.DocumentLink, error) {
|
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-05-15 10:24:49 -06:00
|
|
|
view := s.session.ViewOf(uri)
|
2019-05-03 22:04:18 -06:00
|
|
|
f, m, err := getGoFile(ctx, view, uri)
|
2019-04-24 09:33:45 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-20 13:23:02 -06:00
|
|
|
file := f.GetAST(ctx)
|
|
|
|
if file == nil {
|
2019-05-17 11:45:50 -06:00
|
|
|
return nil, fmt.Errorf("no AST for %v", uri)
|
|
|
|
}
|
2019-05-20 13:23:02 -06:00
|
|
|
// Add a Godoc link for each imported package.
|
2019-04-24 09:33:45 -06:00
|
|
|
var result []protocol.DocumentLink
|
2019-05-20 13:23:02 -06:00
|
|
|
for _, imp := range file.Imports {
|
2019-06-21 15:00:02 -06:00
|
|
|
spn, err := span.NewRange(view.Session().Cache().FileSet(), imp.Pos(), imp.End()).Span()
|
2019-04-24 09:33:45 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := m.Range(spn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
target, err := strconv.Unquote(imp.Path.Value)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
target = "https://godoc.org/" + target
|
|
|
|
result = append(result, protocol.DocumentLink{
|
|
|
|
Range: rng,
|
|
|
|
Target: target,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|