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"
|
|
|
|
|
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"
|
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-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-13 11:46:49 -07:00
|
|
|
snapshot, fh, ok, err := s.beginFileRequest(params.TextDocument.URI, source.Go)
|
|
|
|
if !ok {
|
2019-11-20 14:38:43 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-13 11:46:49 -07:00
|
|
|
rngs, err := source.Highlight(ctx, snapshot, fh, params.Position)
|
2019-08-16 11:49:17 -06:00
|
|
|
if err != nil {
|
2020-03-10 21:09:39 -06:00
|
|
|
event.Error(ctx, "no highlight", err, tag.URI.Of(params.TextDocument.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
|
|
|
|
}
|