golang.org/x/tools/gopls@v0.15.3/internal/server/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 server
     6  
     7  import (
     8  	"context"
     9  
    10  	"golang.org/x/tools/gopls/internal/file"
    11  	"golang.org/x/tools/gopls/internal/golang"
    12  	"golang.org/x/tools/gopls/internal/protocol"
    13  	"golang.org/x/tools/gopls/internal/template"
    14  	"golang.org/x/tools/internal/event"
    15  	"golang.org/x/tools/internal/event/tag"
    16  )
    17  
    18  func (s *server) DocumentHighlight(ctx context.Context, params *protocol.DocumentHighlightParams) ([]protocol.DocumentHighlight, error) {
    19  	ctx, done := event.Start(ctx, "lsp.Server.documentHighlight", tag.URI.Of(params.TextDocument.URI))
    20  	defer done()
    21  
    22  	fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	defer release()
    27  
    28  	switch snapshot.FileKind(fh) {
    29  	case file.Tmpl:
    30  		return template.Highlight(ctx, snapshot, fh, params.Position)
    31  	case file.Go:
    32  		rngs, err := golang.Highlight(ctx, snapshot, fh, params.Position)
    33  		if err != nil {
    34  			event.Error(ctx, "no highlight", err)
    35  		}
    36  		return toProtocolHighlight(rngs), nil
    37  	}
    38  	return nil, nil // empty result
    39  }
    40  
    41  func toProtocolHighlight(rngs []protocol.Range) []protocol.DocumentHighlight {
    42  	result := make([]protocol.DocumentHighlight, 0, len(rngs))
    43  	kind := protocol.Text
    44  	for _, rng := range rngs {
    45  		result = append(result, protocol.DocumentHighlight{
    46  			Kind:  kind,
    47  			Range: rng,
    48  		})
    49  	}
    50  	return result
    51  }