github.com/april1989/origin-go-tools@v0.0.32/internal/lsp/code_lens.go (about)

     1  // Copyright 2020 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  	"fmt"
    10  
    11  	"github.com/april1989/origin-go-tools/internal/event"
    12  	"github.com/april1989/origin-go-tools/internal/lsp/mod"
    13  	"github.com/april1989/origin-go-tools/internal/lsp/protocol"
    14  	"github.com/april1989/origin-go-tools/internal/lsp/source"
    15  )
    16  
    17  func (s *Server) codeLens(ctx context.Context, params *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
    18  	snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind)
    19  	defer release()
    20  	if !ok {
    21  		return nil, err
    22  	}
    23  	var lensFuncs map[string]source.LensFunc
    24  	switch fh.Kind() {
    25  	case source.Mod:
    26  		lensFuncs = mod.LensFuncs()
    27  	case source.Go:
    28  		lensFuncs = source.LensFuncs()
    29  	default:
    30  		// Unsupported file kind for a code lens.
    31  		return nil, nil
    32  	}
    33  	var result []protocol.CodeLens
    34  	for lens, lf := range lensFuncs {
    35  		if !snapshot.View().Options().EnabledCodeLens[lens] {
    36  			continue
    37  		}
    38  		added, err := lf(ctx, snapshot, fh)
    39  		// Code lens is called on every keystroke, so we should just operate in
    40  		// a best-effort mode, ignoring errors.
    41  		if err != nil {
    42  			event.Error(ctx, fmt.Sprintf("code lens %s failed", lens), err)
    43  			continue
    44  		}
    45  		result = append(result, added...)
    46  	}
    47  	return result, nil
    48  }