'reload' action should be allowed even where there's no match

If the command template doesn't have any placeholder expressions.

    : | fzf --bind 'space:reload:seq 10'
This commit is contained in:
Junegunn Choi 2019-12-06 22:34:30 +09:00
parent 5e42b1c9f8
commit 1e6ac5590e
2 changed files with 16 additions and 7 deletions

View File

@ -1237,7 +1237,7 @@ func parsePlaceholder(match string) (bool, string, placeholderFlags) {
return false, matchWithoutFlags, flags return false, matchWithoutFlags, flags
} }
func hasPreviewFlags(template string) (plus bool, query bool) { func hasPreviewFlags(template string) (slot bool, plus bool, query bool) {
for _, match := range placeholder.FindAllString(template, -1) { for _, match := range placeholder.FindAllString(template, -1) {
_, _, flags := parsePlaceholder(match) _, _, flags := parsePlaceholder(match)
if flags.plus { if flags.plus {
@ -1246,6 +1246,7 @@ func hasPreviewFlags(template string) (plus bool, query bool) {
if flags.query { if flags.query {
query = true query = true
} }
slot = true
} }
return return
} }
@ -1409,7 +1410,7 @@ func (t *Terminal) currentItem() *Item {
func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item) { func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item) {
current := t.currentItem() current := t.currentItem()
plus, query := hasPreviewFlags(template) _, plus, query := hasPreviewFlags(template)
if !(query && len(t.input) > 0 || (forcePlus || plus) && len(t.selected) > 0) { if !(query && len(t.input) > 0 || (forcePlus || plus) && len(t.selected) > 0) {
return current != nil, []*Item{current, current} return current != nil, []*Item{current, current}
} }
@ -2045,11 +2046,12 @@ func (t *Terminal) Loop() {
t.failed = nil t.failed = nil
valid, list := t.buildPlusList(a.a, false) valid, list := t.buildPlusList(a.a, false)
// If the command template has {q}, we run the command even when the
// query string is empty.
if !valid { if !valid {
_, query := hasPreviewFlags(a.a) // We run the command even when there's no match
valid = query // 1. If the template doesn't have any slots
// 2. If the template has {q}
slot, _, query := hasPreviewFlags(a.a)
valid = !slot || query
} }
if valid { if valid {
command := replacePlaceholder(a.a, command := replacePlaceholder(a.a,
@ -2095,7 +2097,7 @@ func (t *Terminal) Loop() {
if queryChanged { if queryChanged {
if t.isPreviewEnabled() { if t.isPreviewEnabled() {
_, q := hasPreviewFlags(t.preview.command) _, _, q := hasPreviewFlags(t.preview.command)
if q { if q {
t.version++ t.version++
} }

View File

@ -1640,6 +1640,13 @@ class TestGoFZF < TestBase
tmux.until { |lines| lines.item_count == 553 && lines.match_count == 1 } tmux.until { |lines| lines.item_count == 553 && lines.match_count == 1 }
tmux.until { |lines| !lines[-2].include?('(1/2)') } tmux.until { |lines| !lines[-2].include?('(1/2)') }
end end
def test_reload_even_when_theres_no_match
tmux.send_keys %(: | #{FZF} --bind 'space:reload(seq 10)'), :Enter
tmux.until { |lines| lines.item_count.zero? }
tmux.send_keys :Space
tmux.until { |lines| lines.item_count == 10 }
end
end end
module TestShell module TestShell