src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/edit/complete/raw_item.go (about)

     1  package complete
     2  
     3  import (
     4  	"src.elv.sh/pkg/cli/modes"
     5  	"src.elv.sh/pkg/parse"
     6  	"src.elv.sh/pkg/ui"
     7  )
     8  
     9  // PlainItem is a simple implementation of RawItem.
    10  type PlainItem string
    11  
    12  func (p PlainItem) String() string { return string(p) }
    13  
    14  func (p PlainItem) Cook(q parse.PrimaryType) modes.CompletionItem {
    15  	s := string(p)
    16  	quoted, _ := parse.QuoteAs(s, q)
    17  	return modes.CompletionItem{ToInsert: quoted, ToShow: ui.T(s)}
    18  }
    19  
    20  // noQuoteItem is a RawItem implementation that does not quote when cooked. This
    21  // type is not exposed, since argument generators never need this.
    22  type noQuoteItem string
    23  
    24  func (nq noQuoteItem) String() string { return string(nq) }
    25  
    26  func (nq noQuoteItem) Cook(parse.PrimaryType) modes.CompletionItem {
    27  	s := string(nq)
    28  	return modes.CompletionItem{ToInsert: s, ToShow: ui.T(s)}
    29  }
    30  
    31  // ComplexItem is an implementation of RawItem that offers customization options.
    32  type ComplexItem struct {
    33  	Stem       string  // Used in the code and the menu.
    34  	CodeSuffix string  // Appended to the code.
    35  	Display    ui.Text // How the item is displayed. If empty, defaults to ui.T(Stem).
    36  }
    37  
    38  func (c ComplexItem) String() string { return c.Stem }
    39  
    40  func (c ComplexItem) Cook(q parse.PrimaryType) modes.CompletionItem {
    41  	quoted, _ := parse.QuoteAs(c.Stem, q)
    42  	display := c.Display
    43  	if display == nil {
    44  		display = ui.T(c.Stem)
    45  	}
    46  	return modes.CompletionItem{
    47  		ToInsert: quoted + c.CodeSuffix,
    48  		ToShow:   display,
    49  	}
    50  }