edit: Simplify setup process of listing mode.

This commit is contained in:
Qi Xiao 2018-02-08 18:59:28 -08:00
parent 183d0649b5
commit 977c885a90
6 changed files with 27 additions and 25 deletions

View File

@ -191,8 +191,8 @@ func (ed *editor) SetModeInsert() {
ed.SetMode(ed.insert)
}
func (ed *editor) SetModeListing(pb *eddefs.BindingMap, lp eddefs.ListingProvider) {
ed.listing.listingState = *newListing(pb, lp)
func (ed *editor) SetModeListing(b eddefs.BindingMap, p eddefs.ListingProvider) {
ed.listing.setup(b, p)
ed.SetMode(ed.listing)
}

View File

@ -117,7 +117,7 @@ func histlistStart(ed *editor) {
return
}
ed.SetModeListing(&ed.histlistBinding, newHistlist(cmds))
ed.SetModeListing(ed.histlistBinding, newHistlist(cmds))
}
func getCmds(ed *editor) ([]string, error) {

View File

@ -99,7 +99,7 @@ func lastcmdStart(ed *editor) {
ed.Notify("db error: %s", err.Error())
return
}
ed.SetModeListing(&ed.lastcmdBinding, newLastCmd(cmd))
ed.SetModeListing(ed.lastcmdBinding, newLastCmd(cmd))
}
func lastcmdAltDefault(ed *editor) {

View File

@ -20,7 +20,7 @@ type listingMode struct {
}
type listingState struct {
binding *eddefs.BindingMap
binding eddefs.BindingMap
provider eddefs.ListingProvider
selected int
filter string
@ -59,28 +59,12 @@ type placeholderer interface {
Placeholder() string
}
func newListing(pb *eddefs.BindingMap, p eddefs.ListingProvider) *listingState {
l := &listingState{pb, p, 0, "", 0, 0}
l.refresh()
for i := 0; i < p.Len(); i++ {
header, _ := p.Show(i)
width := util.Wcswidth(header)
if l.headerWidth < width {
l.headerWidth = width
}
}
return l
}
func (l *listingMode) Teardown() {
l.listingState = listingState{}
}
func (l *listingMode) Binding(k ui.Key) eval.Callable {
if l.binding == nil {
return l.commonBinding.GetOrDefault(k)
}
specificBindings := *l.binding
specificBindings := l.binding
listingBindings := l.commonBinding
// mode-specific binding -> listing binding ->
// mode-specific default -> listing default
@ -98,6 +82,24 @@ func (l *listingMode) Binding(k ui.Key) eval.Callable {
}
}
func newListing(b eddefs.BindingMap, p eddefs.ListingProvider) *listingState {
l := &listingState{}
l.setup(b, p)
return l
}
func (l *listingState) setup(b eddefs.BindingMap, p eddefs.ListingProvider) {
*l = listingState{b, p, 0, "", 0, 0}
l.refresh()
for i := 0; i < p.Len(); i++ {
header, _ := p.Show(i)
width := util.Wcswidth(header)
if l.headerWidth < width {
l.headerWidth = width
}
}
}
func (l *listingState) ModeLine() ui.Renderer {
return modeLineRenderer{l.provider.ModeTitle(l.selected), l.filter}
}

View File

@ -26,7 +26,7 @@ func (p provider) Show(i int) (string, ui.Styled) {
var (
p = provider{[]string{"foo", "bar", "foobar", "lorem", "ipsum"}, -1}
ls = newListing(nil, p)
ls = newListing(emptyBindingMap, p)
)
func TestListing(t *testing.T) {

View File

@ -52,7 +52,7 @@ func initLocation(ed *editor, ns eval.Ns) {
"pinned": eval.NewVariableFromPtr(&mode.pinned),
}
subns.AddBuiltinFns("edit:location:", map[string]interface{}{
"start": func() { locStart(ed, mode.hidden, mode.pinned, &mode.binding) },
"start": func() { locStart(ed, mode.hidden, mode.pinned, mode.binding) },
})
ns.AddNs("location", subns)
}
@ -140,7 +140,7 @@ func (loc *locationState) Accept(i int, ed eddefs.Editor) {
ed.SetModeInsert()
}
func locStart(ed *editor, hidden, pinned vector.Vector, binding *eddefs.BindingMap) {
func locStart(ed *editor, hidden, pinned vector.Vector, binding eddefs.BindingMap) {
daemon := ed.Daemon()
if daemon == nil {
ed.Notify("%v", errStoreOffline)