Skip to content

Commit

Permalink
Fix completion navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 16, 2023
1 parent f7078ba commit cc24fc0
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ func (p *Prompt) feed(b []byte) (shouldExit bool, userInput *UserInput) {
p.buf.lastKeyStroke = key
// completion
completing := p.completion.Completing()
p.handleCompletionKeyBinding(key, completing)
p.handleCompletionKeyBinding(b, key, completing)

keySwitch:
switch key {
case Enter, ControlJ, ControlM:
indent, execute := p.executeOnEnterCallback(p.buf.Text(), p.renderer.indentSize)
Expand All @@ -169,40 +168,6 @@ keySwitch:
if userInput.input != "" {
p.history.Add(userInput.input)
}
case Tab:
if len(p.completion.GetSuggestions()) > 0 {
// If there are any suggestions, select the next one
p.completion.Next()
break
}

// if there are no suggestions insert indentation
newBytes := make([]byte, 0, len(b))
for _, byt := range b {
switch byt {
case '\t':
for i := 0; i < p.renderer.indentSize; i++ {
newBytes = append(newBytes, IndentUnit)
}
default:
newBytes = append(newBytes, byt)
}
}
p.buf.InsertText(string(newBytes), false, true)
case BackTab:
if len(p.completion.GetSuggestions()) > 0 {
// If there are any suggestions, select the previous one
p.completion.Previous()
break
}

text := p.buf.Document().CurrentLineBeforeCursor()
for _, char := range text {
if char != IndentUnit {
break keySwitch
}
}
p.buf.DeleteBeforeCursor(istrings.RuneNumber(p.renderer.indentSize))
case ControlC:
p.renderer.BreakLine(p.buf, p.lexer)
p.buf = NewBuffer()
Expand Down Expand Up @@ -258,7 +223,8 @@ keySwitch:
return
}

func (p *Prompt) handleCompletionKeyBinding(key Key, completing bool) {
func (p *Prompt) handleCompletionKeyBinding(b []byte, key Key, completing bool) {
keySwitch:
switch key {
case Down:
if completing || p.completionOnDown {
Expand All @@ -270,6 +236,40 @@ func (p *Prompt) handleCompletionKeyBinding(key Key, completing bool) {
if completing {
p.completion.Previous()
}
case Tab:
if len(p.completion.GetSuggestions()) > 0 {
// If there are any suggestions, select the next one
p.completion.Next()
break
}

// if there are no suggestions insert indentation
newBytes := make([]byte, 0, len(b))
for _, byt := range b {
switch byt {
case '\t':
for i := 0; i < p.renderer.indentSize; i++ {
newBytes = append(newBytes, IndentUnit)
}
default:
newBytes = append(newBytes, byt)
}
}
p.buf.InsertText(string(newBytes), false, true)
case BackTab:
if len(p.completion.GetSuggestions()) > 0 {
// If there are any suggestions, select the previous one
p.completion.Previous()
break
}

text := p.buf.Document().CurrentLineBeforeCursor()
for _, char := range text {
if char != IndentUnit {
break keySwitch
}
}
p.buf.DeleteBeforeCursor(istrings.RuneNumber(p.renderer.indentSize))
default:
if s, ok := p.completion.GetSelectedSuggestion(); ok {
w := p.buf.Document().GetWordBeforeCursorUntilSeparator(p.completion.wordSeparator)
Expand Down

0 comments on commit cc24fc0

Please sign in to comment.