2019-03-25 18:56:05 -06:00
|
|
|
// Copyright 2019 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 (
|
2019-04-17 13:37:20 -06:00
|
|
|
"context"
|
|
|
|
|
2019-03-25 18:56:05 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-04-17 13:37:20 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-09-05 16:54:05 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-03-25 18:56:05 -06:00
|
|
|
)
|
|
|
|
|
2019-09-07 15:01:26 -06:00
|
|
|
func (s *Server) documentHighlight(ctx context.Context, params *protocol.DocumentHighlightParams) ([]protocol.DocumentHighlight, error) {
|
2020-02-12 14:36:46 -07:00
|
|
|
uri := params.TextDocument.URI.SpanURI()
|
2019-11-15 10:43:45 -07:00
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-20 14:38:43 -07:00
|
|
|
snapshot := view.Snapshot()
|
2020-01-10 15:37:29 -07:00
|
|
|
fh, err := snapshot.GetFile(uri)
|
2019-11-20 14:38:43 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-10 09:51:34 -07:00
|
|
|
var rngs []protocol.Range
|
2019-12-17 16:57:54 -07:00
|
|
|
switch fh.Identity().Kind {
|
2019-12-10 09:51:34 -07:00
|
|
|
case source.Go:
|
2019-12-17 16:57:54 -07:00
|
|
|
rngs, err = source.Highlight(ctx, snapshot, fh, params.Position)
|
2019-12-10 09:51:34 -07:00
|
|
|
case source.Mod:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-08-16 11:49:17 -06:00
|
|
|
if err != nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
log.Error(ctx, "no highlight", err, telemetry.URI.Of(uri))
|
2019-08-16 11:49:17 -06:00
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
return toProtocolHighlight(rngs), nil
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
|
|
|
|
2019-09-05 16:54:05 -06:00
|
|
|
func toProtocolHighlight(rngs []protocol.Range) []protocol.DocumentHighlight {
|
|
|
|
result := make([]protocol.DocumentHighlight, 0, len(rngs))
|
2019-03-25 18:56:05 -06:00
|
|
|
kind := protocol.Text
|
2019-09-05 16:54:05 -06:00
|
|
|
for _, rng := range rngs {
|
|
|
|
result = append(result, protocol.DocumentHighlight{
|
2019-11-17 12:29:15 -07:00
|
|
|
Kind: kind,
|
2019-09-05 16:54:05 -06:00
|
|
|
Range: rng,
|
|
|
|
})
|
2019-03-25 18:56:05 -06:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|