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"
|
|
|
|
"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)
|
|
|
|
view := s.findView(ctx, 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
|
|
|
|
}
|
|
|
|
// find the import block
|
|
|
|
ast := f.GetAST(ctx)
|
|
|
|
var result []protocol.DocumentLink
|
|
|
|
for _, imp := range ast.Imports {
|
|
|
|
spn, err := span.NewRange(f.GetFileSet(ctx), imp.Pos(), imp.End()).Span()
|
|
|
|
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
|
|
|
|
}
|