github.com/jmigpin/editor@v1.6.0/core/internalcmds/lsprotoreferences.go (about)

     1  package internalcmds
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"path/filepath"
     8  
     9  	"github.com/jmigpin/editor/core"
    10  	"github.com/jmigpin/editor/core/lsproto"
    11  	"github.com/jmigpin/editor/util/iout/iorw"
    12  )
    13  
    14  func LSProtoReferences(args0 *core.InternalCmdArgs) error {
    15  	ed := args0.Ed
    16  	erow := args0.ERow
    17  
    18  	if !erow.Info.IsFileButNotDir() {
    19  		return fmt.Errorf("not a file")
    20  	}
    21  
    22  	// create new erow to run on
    23  	dir := filepath.Dir(erow.Info.Name())
    24  	info := erow.Ed.ReadERowInfo(dir)
    25  	erow2 := core.NewBasicERow(info, erow.Row.PosBelow())
    26  	iorw.Append(erow2.Row.Toolbar.RW(), []byte(" | Stop"))
    27  	erow2.Flash()
    28  
    29  	// NOTE: args0.Ctx will end at func exit
    30  
    31  	erow2.Exec.RunAsync(func(ctx context.Context, rw io.ReadWriter) error {
    32  		// NOTE: not running in UI goroutine here
    33  
    34  		ta := erow.Row.TextArea
    35  		locs, err := ed.LSProtoMan.TextDocumentReferences(ctx, erow.Info.Name(), ta.RW(), ta.CursorIndex())
    36  		if err != nil {
    37  			return err
    38  		}
    39  
    40  		// print locations
    41  		str, err := lsproto.LocationsToString(locs, erow2.Info.Dir())
    42  		if err != nil {
    43  			return err
    44  		}
    45  		fmt.Fprintf(rw, "lsproto references:")
    46  		if len(locs) == 0 {
    47  			fmt.Fprintf(rw, " no results\n")
    48  			return nil
    49  		}
    50  		fmt.Fprintf(rw, "\n%v", str)
    51  		return nil
    52  	})
    53  
    54  	return nil
    55  }