github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/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/powerman/golang-tools/internal/event"
    11  	"github.com/powerman/golang-tools/internal/lsp/debug/tag"
    12  	"github.com/powerman/golang-tools/internal/lsp/protocol"
    13  	"github.com/powerman/golang-tools/internal/lsp/source"
    14  	"github.com/powerman/golang-tools/internal/lsp/template"
    15  )
    16  
    17  func (s *Server) documentHighlight(ctx context.Context, params *protocol.DocumentHighlightParams) ([]protocol.DocumentHighlight, error) {
    18  	snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
    19  	defer release()
    20  	if !ok {
    21  		return nil, err
    22  	}
    23  
    24  	if snapshot.View().FileKind(fh) == source.Tmpl {
    25  		return template.Highlight(ctx, snapshot, fh, params.Position)
    26  	}
    27  
    28  	rngs, err := source.Highlight(ctx, snapshot, fh, params.Position)
    29  	if err != nil {
    30  		event.Error(ctx, "no highlight", err, tag.URI.Of(params.TextDocument.URI))
    31  	}
    32  	return toProtocolHighlight(rngs), nil
    33  }
    34  
    35  func toProtocolHighlight(rngs []protocol.Range) []protocol.DocumentHighlight {
    36  	result := make([]protocol.DocumentHighlight, 0, len(rngs))
    37  	kind := protocol.Text
    38  	for _, rng := range rngs {
    39  		result = append(result, protocol.DocumentHighlight{
    40  			Kind:  kind,
    41  			Range: rng,
    42  		})
    43  	}
    44  	return result
    45  }