Don't select completions until tab has been pressed

Before, pressing <Enter> when completions were visible would execute the
selected completion. As soon as completions were provided, the first
item would be selected. This could cause issues e.g. when changing
folders:

  :cf <Enter>

Previously, this would have selected the first folder in the list. Now,
since <Tab>, <C-n>, etc have not been pressed to select the first
completion, the command above simply executes `:cf `.

To accomplish this, a "no-op completion" has been added at index -1.
This commit is contained in:
Ben Burwell 2020-01-04 12:54:48 -05:00 committed by Drew DeVault
parent b2dc624dbf
commit 07a9b9204e
1 changed files with 8 additions and 6 deletions

View File

@ -276,7 +276,7 @@ func (ti *TextInput) updateCompletions() {
func (ti *TextInput) showCompletions() { func (ti *TextInput) showCompletions() {
ti.completions = ti.tabcomplete(ti.StringLeft()) ti.completions = ti.tabcomplete(ti.StringLeft())
ti.completeIndex = 0 ti.completeIndex = -1
ti.Invalidate() ti.Invalidate()
} }
@ -410,7 +410,7 @@ func (c *completions) next() {
idx := c.idx idx := c.idx
idx++ idx++
if idx > len(c.options)-1 { if idx > len(c.options)-1 {
idx = 0 idx = -1
} }
c.onSelect(idx) c.onSelect(idx)
} }
@ -418,7 +418,7 @@ func (c *completions) next() {
func (c *completions) prev() { func (c *completions) prev() {
idx := c.idx idx := c.idx
idx-- idx--
if idx < 0 { if idx < -1 {
idx = len(c.options) - 1 idx = len(c.options) - 1
} }
c.onSelect(idx) c.onSelect(idx)
@ -429,7 +429,7 @@ func (c *completions) Event(e tcell.Event) bool {
case *tcell.EventKey: case *tcell.EventKey:
switch e.Key() { switch e.Key() {
case tcell.KeyTab: case tcell.KeyTab:
if len(c.options) == 1 { if len(c.options) == 1 && c.idx >= 0 {
c.onExec() c.onExec()
} else { } else {
stem := findStem(c.options) stem := findStem(c.options)
@ -447,8 +447,10 @@ func (c *completions) Event(e tcell.Event) bool {
c.prev() c.prev()
return true return true
case tcell.KeyEnter: case tcell.KeyEnter:
c.onExec() if c.idx >= 0 {
return true c.onExec()
return true
}
} }
} }
return false return false