newedit: Reorganize lastcmd so that it behaves more like a mode.

This commit is contained in:
Qi Xiao 2019-03-10 19:09:56 +00:00
parent c6d7be11ed
commit 6af426499c
3 changed files with 29 additions and 16 deletions

View File

@ -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)
}
}

View File

@ -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"),

View File

@ -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("<edit:lastcmd>:", "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
}