From f63eaf8e7e9003117d30d7d80ff0b5b99cb14de0 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Fri, 17 Jan 2020 08:05:10 -0500 Subject: [PATCH] pkg/edit: Support &display to edit:complex-candidate. The &display-suffix command is still supported but will be deprecated once #898 is fixed. --- pkg/edit/complete/raw_item.go | 14 +++++++++----- pkg/edit/complete_getopt.go | 8 ++++---- pkg/edit/complete_getopt_test.go | 12 ++++++------ pkg/edit/completion.go | 26 ++++++++++++++++---------- pkg/edit/completion_test.go | 16 ++++++++-------- 5 files changed, 43 insertions(+), 33 deletions(-) diff --git a/pkg/edit/complete/raw_item.go b/pkg/edit/complete/raw_item.go index fa2724b8..a6f513dc 100644 --- a/pkg/edit/complete/raw_item.go +++ b/pkg/edit/complete/raw_item.go @@ -30,19 +30,23 @@ func (nq noQuoteItem) Cook(parse.PrimaryType) completion.Item { // ComplexItem is an implementation of RawItem that offers customization options. type ComplexItem struct { - Stem string // Used in the code and the menu. - CodeSuffix string // Appended to the code. - DisplaySuffix string // Appended to the display. - DisplayStyle ui.Style // Use for displaying. + Stem string // Used in the code and the menu. + CodeSuffix string // Appended to the code. + Display string // How the item is displayed. If empty, defaults to Stem. + DisplayStyle ui.Style // Use for displaying. } 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, } } diff --git a/pkg/edit/complete_getopt.go b/pkg/edit/complete_getopt.go index 70b98fdc..a32f7703 100644 --- a/pkg/edit/complete_getopt.go +++ b/pkg/edit/complete_getopt.go @@ -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 diff --git a/pkg/edit/complete_getopt_test.go b/pkg/edit/complete_getopt_test.go index 181e9b11..dae09292 100644 --- a/pkg/edit/complete_getopt_test.go +++ b/pkg/edit/complete_getopt_test.go @@ -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"), diff --git a/pkg/edit/completion.go b/pkg/edit/completion.go index 3a9c5df7..e6d7d99c 100644 --- a/pkg/edit/completion.go +++ b/pkg/edit/completion.go @@ -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, + Stem: stem, + CodeSuffix: opts.CodeSuffix, + 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 diff --git a/pkg/edit/completion_test.go b/pkg/edit/completion_test.go index cfa533ee..818dcfe0 100644 --- a/pkg/edit/completion_test.go +++ b/pkg/edit/completion_test.go @@ -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", + "stem": "a/b/c", + "code-suffix": " ", + "display": "A/B/C", }) }