github.com/v2fly/tools@v0.100.0/internal/lsp/highlight.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package lsp
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/v2fly/tools/internal/event"
    11  	"github.com/v2fly/tools/internal/lsp/debug/tag"
    12  	"github.com/v2fly/tools/internal/lsp/protocol"
    13  	"github.com/v2fly/tools/internal/lsp/source"
    14  )
    15  
    16  func (s *Server) documentHighlight(ctx context.Context, params *protocol.DocumentHighlightParams) ([]protocol.DocumentHighlight, error) {
    17  	snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
    18  	defer release()
    19  	if !ok {
    20  		return nil, err
    21  	}
    22  	rngs, err := source.Highlight(ctx, snapshot, fh, params.Position)
    23  	if err != nil {
    24  		event.Error(ctx, "no highlight", err, tag.URI.Of(params.TextDocument.URI))
    25  	}
    26  	return toProtocolHighlight(rngs), nil
    27  }
    28  
    29  func toProtocolHighlight(rngs []protocol.Range) []protocol.DocumentHighlight {
    30  	result := make([]protocol.DocumentHighlight, 0, len(rngs))
    31  	kind := protocol.Text
    32  	for _, rng := range rngs {
    33  		result = append(result, protocol.DocumentHighlight{
    34  			Kind:  kind,
    35  			Range: rng,
    36  		})
    37  	}
    38  	return result
    39  }