newedit: Hook up with its Store dependency.

This commit is contained in:
Qi Xiao 2019-03-18 22:41:38 +00:00
parent fd81492236
commit 71d050b31c
7 changed files with 64 additions and 22 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/elves/elvish/newedit/core"
"github.com/elves/elvish/newedit/highlight"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/store/storedefs"
)
// Editor is the interface line editor for Elvish.
@ -22,7 +23,7 @@ type Editor struct {
}
// NewEditor creates a new editor from input and output terminal files.
func NewEditor(in, out *os.File, ev *eval.Evaler) *Editor {
func NewEditor(in, out *os.File, ev *eval.Evaler, st storedefs.Store) *Editor {
ed := core.NewEditor(core.NewTTY(in, out), core.NewSignalSource())
ed.Highlighter = highlight.NewHighlighter(
@ -56,7 +57,7 @@ func NewEditor(in, out *os.File, ev *eval.Evaler) *Editor {
// Listing modes.
lsMode, lsBinding, lsNs := initListing(ed)
ns.AddNs("listing", lsNs)
lastcmdNs := initLastcmd(ed, ev, lsMode, lsBinding)
lastcmdNs := initLastcmd(ed, ev, st, lsMode, lsBinding)
ns.AddNs("lastcmd", lastcmdNs)
// Evaluate default bindings.

View File

@ -1,25 +1,14 @@
package newedit
import (
"os"
"testing"
"github.com/elves/elvish/eval"
)
var devNull *os.File
func init() {
f, err := os.Open(os.DevNull)
if err != nil {
panic(err)
}
devNull = f
}
func TestNs(t *testing.T) {
ev := eval.NewEvaler()
ed := NewEditor(devNull, devNull, ev)
ed := NewEditor(devNull, devNull, ev, testStore)
ev.Global.AddNs("edit", ed.Ns())
ev.EvalSourceInTTY(eval.NewScriptSource("[t]", "[t]", "edit:max-height = 20"))

View File

@ -4,10 +4,12 @@ import (
"github.com/elves/elvish/eval"
"github.com/elves/elvish/newedit/lastcmd"
"github.com/elves/elvish/newedit/listing"
"github.com/elves/elvish/parse/parseutil"
"github.com/elves/elvish/store/storedefs"
)
// Initializes states for the lastcmd mode and its API.
func initLastcmd(ed editor, ev *eval.Evaler, lsMode *listing.Mode, lsBinding *BindingMap) eval.Ns {
func initLastcmd(ed editor, ev *eval.Evaler, st storedefs.Store, lsMode *listing.Mode, lsBinding *BindingMap) eval.Ns {
binding := EmptyBindingMap
mode := lastcmd.Mode{
Mode: lsMode,
@ -15,9 +17,17 @@ func initLastcmd(ed editor, ev *eval.Evaler, lsMode *listing.Mode, lsBinding *Bi
}
ns := eval.Ns{}.
AddBuiltinFn("<edit:lastcmd>:", "start", func() {
// TODO: Actually get the last line instead of using a fake one.
mode.Start("echo hello world", []string{"echo", "hello", "world"})
ed.State().SetMode(mode)
startLastcmd(ed, st, &mode)
})
return ns
}
func startLastcmd(ed editor, st storedefs.Store, mode *lastcmd.Mode) {
_, cmd, err := st.PrevCmd(-1, "")
if err != nil {
ed.Notify("db error: " + err.Error())
return
}
mode.Start(cmd, parseutil.Wordify(cmd))
ed.State().SetMode(mode)
}

View File

@ -16,7 +16,7 @@ func TestInitLastCmd_Start(t *testing.T) {
lsMode := listing.Mode{}
lsBinding := EmptyBindingMap
ns := initLastcmd(ed, ev, &lsMode, &lsBinding)
ns := initLastcmd(ed, ev, testStore, &lsMode, &lsBinding)
// Call <edit:listing>:start.
fm := eval.NewTopFrame(ev, eval.NewInternalSource("[test]"), nil)

View File

@ -2,7 +2,6 @@ package newedit
import (
"fmt"
"os"
"reflect"
"testing"
@ -15,7 +14,7 @@ import (
func TestMakePrompt_ElvishVariableLinksToPromptConfig(t *testing.T) {
ev := eval.NewEvaler()
// NewEditor calls makePrompt
ed := NewEditor(os.Stdin, os.Stdout, ev)
ed := NewEditor(devNull, devNull, ev, testStore)
ev.Global.AddNs("ed", ed.Ns())
ev.EvalSourceInTTY(eval.NewScriptSource(
"[t]", "[t]", "ed:prompt = { put 'CUSTOM PROMPT' }"))

43
newedit/testmain_test.go Normal file
View File

@ -0,0 +1,43 @@
package newedit
import (
"os"
"testing"
"github.com/elves/elvish/store"
"github.com/elves/elvish/store/storedefs"
)
var (
devNull *os.File
testStore storedefs.Store
)
// TestMain sets up the test fixture.
func TestMain(m *testing.M) {
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
// Set up devNull. This can be used as I/O for the editor when we do not
// wish to test the IO.
f, err := os.Open(os.DevNull)
if err != nil {
panic(err)
}
defer f.Close()
devNull = f
// Set up testStore.
st, cleanup := store.MustGetTempStore()
defer cleanup()
testStore = st
// Add some data to testStore.
_, err = testStore.AddCmd("echo hello world")
if err != nil {
panic(err)
}
return m.Run()
}

View File

@ -25,7 +25,7 @@ func interact(ev *eval.Evaler, dataDir string, norc, newEdit bool) {
var ed editor
if sys.IsATTY(os.Stdin) {
if newEdit {
newed := newedit.NewEditor(os.Stdin, os.Stderr, ev)
newed := newedit.NewEditor(os.Stdin, os.Stderr, ev, ev.DaemonClient)
ev.Global.AddNs("edit", newed.Ns())
ed = newed
} else {