pkg/edit: Support &display to edit:complex-candidate.

The &display-suffix command is still supported but will be deprecated once #898
is fixed.
This commit is contained in:
Qi Xiao 2020-01-17 08:05:10 -05:00
parent 4ca283ecf4
commit f63eaf8e7e
5 changed files with 43 additions and 33 deletions

View File

@ -30,19 +30,23 @@ func (nq noQuoteItem) Cook(parse.PrimaryType) completion.Item {
// ComplexItem is an implementation of RawItem that offers customization options. // ComplexItem is an implementation of RawItem that offers customization options.
type ComplexItem struct { type ComplexItem struct {
Stem string // Used in the code and the menu. Stem string // Used in the code and the menu.
CodeSuffix string // Appended to the code. CodeSuffix string // Appended to the code.
DisplaySuffix string // Appended to the display. Display string // How the item is displayed. If empty, defaults to Stem.
DisplayStyle ui.Style // Use for displaying. DisplayStyle ui.Style // Use for displaying.
} }
func (c ComplexItem) String() string { return c.Stem } func (c ComplexItem) String() string { return c.Stem }
func (c ComplexItem) Cook(q parse.PrimaryType) completion.Item { func (c ComplexItem) Cook(q parse.PrimaryType) completion.Item {
quoted, _ := parse.QuoteAs(c.Stem, q) quoted, _ := parse.QuoteAs(c.Stem, q)
display := c.Display
if display == "" {
display = c.Stem
}
return completion.Item{ return completion.Item{
ToInsert: quoted + c.CodeSuffix, ToInsert: quoted + c.CodeSuffix,
ToShow: c.Stem + c.DisplaySuffix, ToShow: display,
ShowStyle: c.DisplayStyle, ShowStyle: c.DisplayStyle,
} }
} }

View File

@ -115,9 +115,9 @@ func completeGetopt(fm *eval.Frame, vArgs, vOpts, vArgHandlers interface{}) erro
c := complexItem{Stem: "-" + string(opt.Short)} c := complexItem{Stem: "-" + string(opt.Short)}
if d, ok := opts.desc[opt]; ok { if d, ok := opts.desc[opt]; ok {
if e, ok := opts.argDesc[opt]; ok { if e, ok := opts.argDesc[opt]; ok {
c.DisplaySuffix = " " + e + " (" + d + ")" c.Display = c.Stem + " " + e + " (" + d + ")"
} else { } else {
c.DisplaySuffix = " (" + d + ")" c.Display = c.Stem + " (" + d + ")"
} }
} }
out <- c out <- c
@ -126,9 +126,9 @@ func completeGetopt(fm *eval.Frame, vArgs, vOpts, vArgHandlers interface{}) erro
c := complexItem{Stem: "--" + opt.Long} c := complexItem{Stem: "--" + opt.Long}
if d, ok := opts.desc[opt]; ok { if d, ok := opts.desc[opt]; ok {
if e, ok := opts.argDesc[opt]; ok { if e, ok := opts.argDesc[opt]; ok {
c.DisplaySuffix = " " + e + " (" + d + ")" c.Display = c.Stem + " " + e + " (" + d + ")"
} else { } else {
c.DisplaySuffix = " (" + d + ")" c.Display = c.Stem + " (" + d + ")"
} }
} }
out <- c out <- c

View File

@ -31,13 +31,13 @@ func TestCompleteGetopt(t *testing.T) {
testGlobals(t, f.Evaler, map[string]interface{}{ testGlobals(t, f.Evaler, map[string]interface{}{
"arg1": vals.MakeList("first1", "first2"), "arg1": vals.MakeList("first1", "first2"),
"opts": vals.MakeList( "opts": vals.MakeList(
complexItem{Stem: "-a", DisplaySuffix: " (Show all)"}, complexItem{Stem: "-a", Display: "-a (Show all)"},
complexItem{Stem: "--all", DisplaySuffix: " (Show all)"}, complexItem{Stem: "--all", Display: "--all (Show all)"},
complexItem{Stem: "-n", DisplaySuffix: " (Set name)"}, complexItem{Stem: "-n", Display: "-n (Set name)"},
complexItem{Stem: "--name", DisplaySuffix: " (Set name)"}), complexItem{Stem: "--name", Display: "--name (Set name)"}),
"long-opts": vals.MakeList( "long-opts": vals.MakeList(
complexItem{Stem: "--all", DisplaySuffix: " (Show all)"}, complexItem{Stem: "--all", Display: "--all (Show all)"},
complexItem{Stem: "--name", DisplaySuffix: " (Set name)"}), complexItem{Stem: "--name", Display: "--name (Set name)"}),
"short-opt-arg": vals.MakeList("name1", "name2"), "short-opt-arg": vals.MakeList("name1", "name2"),
"long-opt-arg": vals.MakeList("name1", "name2"), "long-opt-arg": vals.MakeList("name1", "name2"),
"arg1-after-opt": vals.MakeList("first1", "first2"), "arg1-after-opt": vals.MakeList("first1", "first2"),

View File

@ -74,15 +74,21 @@ import (
type complexCandidateOpts struct { type complexCandidateOpts struct {
CodeSuffix string CodeSuffix string
DisplaySuffix string DisplaySuffix string
Display string
} }
func (*complexCandidateOpts) SetDefaultOptions() {} func (*complexCandidateOpts) SetDefaultOptions() {}
func complexCandidate(opts complexCandidateOpts, stem string) complexItem { func complexCandidate(opts complexCandidateOpts, stem string) complexItem {
display := opts.Display
if display == "" {
// TODO(#898): Deprecate DisplaySuffix and remove this branch.
display = stem + opts.DisplaySuffix
}
return complexItem{ return complexItem{
Stem: stem, Stem: stem,
CodeSuffix: opts.CodeSuffix, CodeSuffix: opts.CodeSuffix,
DisplaySuffix: opts.DisplaySuffix, Display: display,
} }
} }
@ -241,14 +247,14 @@ func (c complexItem) Index(k interface{}) (interface{}, bool) {
return c.Stem, true return c.Stem, true
case "code-suffix": case "code-suffix":
return c.CodeSuffix, true return c.CodeSuffix, true
case "display-suffix": case "display":
return c.DisplaySuffix, true return c.Display, true
} }
return nil, false return nil, false
} }
func (c complexItem) IterateKeys(f func(interface{}) bool) { func (c complexItem) IterateKeys(f func(interface{}) bool) {
util.Feed(f, "stem", "code-suffix", "display-suffix") util.Feed(f, "stem", "code-suffix", "display")
} }
func (c complexItem) Kind() string { return "map" } func (c complexItem) Kind() string { return "map" }
@ -256,21 +262,21 @@ func (c complexItem) Kind() string { return "map" }
func (c complexItem) Equal(a interface{}) bool { func (c complexItem) Equal(a interface{}) bool {
rhs, ok := a.(complexItem) rhs, ok := a.(complexItem)
return ok && c.Stem == rhs.Stem && return ok && c.Stem == rhs.Stem &&
c.CodeSuffix == rhs.CodeSuffix && c.DisplaySuffix == rhs.DisplaySuffix c.CodeSuffix == rhs.CodeSuffix && c.Display == rhs.Display
} }
func (c complexItem) Hash() uint32 { func (c complexItem) Hash() uint32 {
h := hash.DJBInit h := hash.DJBInit
h = hash.DJBCombine(h, hash.String(c.Stem)) h = hash.DJBCombine(h, hash.String(c.Stem))
h = hash.DJBCombine(h, hash.String(c.CodeSuffix)) h = hash.DJBCombine(h, hash.String(c.CodeSuffix))
h = hash.DJBCombine(h, hash.String(c.DisplaySuffix)) h = hash.DJBCombine(h, hash.String(c.Display))
return h return h
} }
func (c complexItem) Repr(indent int) string { func (c complexItem) Repr(indent int) string {
// TODO(xiaq): Pretty-print when indent >= 0 // TODO(xiaq): Pretty-print when indent >= 0
return fmt.Sprintf("(edit:complex-candidate %s &code-suffix=%s &display-suffix=%s)", return fmt.Sprintf("(edit:complex-candidate %s &code-suffix=%s &display=%s)",
parse.Quote(c.Stem), parse.Quote(c.CodeSuffix), parse.Quote(c.DisplaySuffix)) parse.Quote(c.Stem), parse.Quote(c.CodeSuffix), parse.Quote(c.Display))
} }
type wrappedArgGenerator func(*eval.Frame, ...string) error type wrappedArgGenerator func(*eval.Frame, ...string) error

View File

@ -64,9 +64,9 @@ func TestComplexCandidate(t *testing.T) {
defer f.Cleanup() defer f.Cleanup()
evals(f.Evaler, evals(f.Evaler,
`cand = (edit:complex-candidate a/b/c &code-suffix=' ' &display-suffix='x')`, `cand = (edit:complex-candidate a/b/c &code-suffix=' ' &display=A/B/C)`,
// Identical to $cand. // Identical to $cand.
`cand2 = (edit:complex-candidate a/b/c &code-suffix=' ' &display-suffix='x')`, `cand2 = (edit:complex-candidate a/b/c &code-suffix=' ' &display=A/B/C)`,
// Different from $cand. // Different from $cand.
`cand3 = (edit:complex-candidate a/b/c)`, `cand3 = (edit:complex-candidate a/b/c)`,
`kind = (kind-of $cand)`, `kind = (kind-of $cand)`,
@ -75,19 +75,19 @@ func TestComplexCandidate(t *testing.T) {
`eq2 = (eq $cand $cand2)`, `eq2 = (eq $cand $cand2)`,
`eq2h = [&$cand=$true][$cand2]`, `eq2h = [&$cand=$true][$cand2]`,
`eq3 = (eq $cand $cand3)`, `eq3 = (eq $cand $cand3)`,
`stem code-suffix display-suffix = $cand[stem code-suffix display-suffix]`, `stem code-suffix display = $cand[stem code-suffix display]`,
) )
testGlobals(t, f.Evaler, map[string]interface{}{ testGlobals(t, f.Evaler, map[string]interface{}{
"kind": "map", "kind": "map",
"keys": vals.MakeList("stem", "code-suffix", "display-suffix"), "keys": vals.MakeList("stem", "code-suffix", "display"),
"repr": "(edit:complex-candidate a/b/c &code-suffix=' ' &display-suffix=x)", "repr": "(edit:complex-candidate a/b/c &code-suffix=' ' &display=A/B/C)",
"eq2": true, "eq2": true,
"eq2h": true, "eq2h": true,
"eq3": false, "eq3": false,
"stem": "a/b/c", "stem": "a/b/c",
"code-suffix": " ", "code-suffix": " ",
"display-suffix": "x", "display": "A/B/C",
}) })
} }