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

@ -32,7 +32,7 @@ func (nq noQuoteItem) Cook(parse.PrimaryType) completion.Item {
type ComplexItem struct {
Stem string // Used in the code and the menu.
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.
}
@ -40,9 +40,13 @@ func (c ComplexItem) String() string { return c.Stem }
func (c ComplexItem) Cook(q parse.PrimaryType) completion.Item {
quoted, _ := parse.QuoteAs(c.Stem, q)
display := c.Display
if display == "" {
display = c.Stem
}
return completion.Item{
ToInsert: quoted + c.CodeSuffix,
ToShow: c.Stem + c.DisplaySuffix,
ToShow: display,
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)}
if d, ok := opts.desc[opt]; ok {
if e, ok := opts.argDesc[opt]; ok {
c.DisplaySuffix = " " + e + " (" + d + ")"
c.Display = c.Stem + " " + e + " (" + d + ")"
} else {
c.DisplaySuffix = " (" + d + ")"
c.Display = c.Stem + " (" + d + ")"
}
}
out <- c
@ -126,9 +126,9 @@ func completeGetopt(fm *eval.Frame, vArgs, vOpts, vArgHandlers interface{}) erro
c := complexItem{Stem: "--" + opt.Long}
if d, ok := opts.desc[opt]; ok {
if e, ok := opts.argDesc[opt]; ok {
c.DisplaySuffix = " " + e + " (" + d + ")"
c.Display = c.Stem + " " + e + " (" + d + ")"
} else {
c.DisplaySuffix = " (" + d + ")"
c.Display = c.Stem + " (" + d + ")"
}
}
out <- c

View File

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

View File

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

View File

@ -64,9 +64,9 @@ func TestComplexCandidate(t *testing.T) {
defer f.Cleanup()
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.
`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.
`cand3 = (edit:complex-candidate a/b/c)`,
`kind = (kind-of $cand)`,
@ -75,19 +75,19 @@ func TestComplexCandidate(t *testing.T) {
`eq2 = (eq $cand $cand2)`,
`eq2h = [&$cand=$true][$cand2]`,
`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{}{
"kind": "map",
"keys": vals.MakeList("stem", "code-suffix", "display-suffix"),
"repr": "(edit:complex-candidate a/b/c &code-suffix=' ' &display-suffix=x)",
"keys": vals.MakeList("stem", "code-suffix", "display"),
"repr": "(edit:complex-candidate a/b/c &code-suffix=' ' &display=A/B/C)",
"eq2": true,
"eq2h": true,
"eq3": false,
"stem": "a/b/c",
"code-suffix": " ",
"display-suffix": "x",
"display": "A/B/C",
})
}