diff --git a/newedit/lastcmd/lastcmd.go b/newedit/lastcmd/lastcmd.go index e835731a..c7d9e246 100644 --- a/newedit/lastcmd/lastcmd.go +++ b/newedit/lastcmd/lastcmd.go @@ -9,7 +9,24 @@ import ( "github.com/elves/elvish/styled" ) -func StartConfig(line string, words []string) listing.StartConfig { +// Mode represents the lastcmd mode. It implements the types.Mode interface by +// embedding a *listing.Mode. +type Mode struct { + *listing.Mode +} + +// Start starts the lastcmd mode. +func (m *Mode) Start(line string, words []string) { + m.Mode.Start(listing.StartConfig{ + Name: "LASTCMD", + ItemsGetter: itemsGetter(line, words), + // TODO: Uncomment + // AutoAccept: true, + // StartFiltering: true, + }) +} + +func itemsGetter(line string, words []string) func(string) listing.Items { // Build the list of all entries from the line and words. Entries have // positive and negative indicies, except for the first entry, which // represents the entire line and has no indicies. @@ -23,14 +40,8 @@ func StartConfig(line string, words []string) listing.StartConfig { } } - return listing.StartConfig{ - Name: "LASTCMD", - ItemsGetter: func(p string) listing.Items { - return filter(entries, p) - }, - // TODO: Uncomment - // AutoAccept: true, - // StartFiltering: true, + return func(p string) listing.Items { + return filter(entries, p) } } diff --git a/newedit/lastcmd/lastcmd_test.go b/newedit/lastcmd/lastcmd_test.go index 8245595c..86351830 100644 --- a/newedit/lastcmd/lastcmd_test.go +++ b/newedit/lastcmd/lastcmd_test.go @@ -11,9 +11,9 @@ import ( var Args = tt.Args func TestLastCmdItemsGetter_ShowAll(t *testing.T) { - cfg := StartConfig("put hello elvish", []string{"put", "hello", "elvish"}) + g := itemsGetter("put hello elvish", []string{"put", "hello", "elvish"}) - tt.Test(t, tt.Fn("cfg.ItemsGetter", cfg.ItemsGetter), tt.Table{ + tt.Test(t, tt.Fn("itemsGetter", g), tt.Table{ // Empty filter; show everything Args("").Rets(listing.MatchItems( styled.Unstyled(" put hello elvish"), @@ -31,10 +31,10 @@ func TestLastCmdItemsGetter_ShowAll(t *testing.T) { } func TestLastCmdItemsGetter_PrefixMatchIndex(t *testing.T) { - cfg := StartConfig("put 1 2 3 4 5 6 7 8 9 10 11 12", []string{ + g := itemsGetter("put 1 2 3 4 5 6 7 8 9 10 11 12", []string{ "put", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}) - tt.Test(t, tt.Fn("cfg.ItemsGetter", cfg.ItemsGetter), tt.Table{ + tt.Test(t, tt.Fn("itemsGetter", g), tt.Table{ Args("1").Rets(listing.MatchItems( styled.Unstyled(" 1 1"), styled.Unstyled(" 10 10"), diff --git a/newedit/lastcmd_api.go b/newedit/lastcmd_api.go index 54127a45..c4955137 100644 --- a/newedit/lastcmd_api.go +++ b/newedit/lastcmd_api.go @@ -8,10 +8,12 @@ import ( // Initializes states for the lastcmd mode and its API. func initLastcmd(ed editor, ev *eval.Evaler, lsMode *listing.Mode) eval.Ns { + m := lastcmd.Mode{Mode: lsMode} ns := eval.Ns{}. - AddBuiltinFn("edit:listing", "start", func() { - lsMode.Start(lastcmd.StartConfig("echo hello world", []string{"echo", "hello", "world"})) - ed.State().SetMode(lsMode) + AddBuiltinFn(":", "start", func() { + // TODO: Actually get the last line instead of using a fake one. + m.Start("echo hello world", []string{"echo", "hello", "world"}) + ed.State().SetMode(m) }) return ns }