diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-10-17 13:00:44 +1100 |
---|---|---|
committer | github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> | 2021-10-17 04:17:59 +0000 |
commit | 345c90ac05bffd6e4163be9dabc0386cd8918057 (patch) | |
tree | ed29b41dfe6354adba1f81c11d676519ebdf2daa /pkg/gui/editors.go | |
parent | 7564e506b525e0831a1fb88dfc70e9d27564d8a1 (diff) |
fix editor
Diffstat (limited to 'pkg/gui/editors.go')
-rw-r--r-- | pkg/gui/editors.go | 87 |
1 files changed, 38 insertions, 49 deletions
diff --git a/pkg/gui/editors.go b/pkg/gui/editors.go index 7d3054cd0..6cc6409cb 100644 --- a/pkg/gui/editors.go +++ b/pkg/gui/editors.go @@ -6,88 +6,77 @@ import ( "github.com/jesseduffield/gocui" ) -// we've just copy+pasted the editor from gocui to here so that we can also re- -// render the commit message length on each keypress -func (gui *Gui) commitMessageEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool { +func (gui *Gui) handleEditorKeypress(textArea *gocui.TextArea, key gocui.Key, ch rune, mod gocui.Modifier, allowMultiline bool) bool { newlineKey, ok := gui.getKey(gui.Config.GetUserConfig().Keybinding.Universal.AppendNewline).(gocui.Key) if !ok { newlineKey = gocui.KeyAltEnter } - matched := true switch { case key == gocui.KeyBackspace || key == gocui.KeyBackspace2: - v.EditDelete(true) + textArea.BackSpaceChar() case key == gocui.KeyCtrlD || key == gocui.KeyDelete: - v.EditDelete(false) + textArea.DeleteChar() case key == gocui.KeyArrowDown: - v.MoveCursor(0, 1, false) + textArea.MoveCursorDown() case key == gocui.KeyArrowUp: - v.MoveCursor(0, -1, false) + textArea.MoveCursorUp() case key == gocui.KeyArrowLeft: - v.MoveCursor(-1, 0, false) + textArea.MoveCursorLeft() case key == gocui.KeyArrowRight: - v.MoveCursor(1, 0, false) + textArea.MoveCursorRight() case key == newlineKey: - v.EditNewLine() + if allowMultiline { + textArea.TypeRune('\n') + } else { + return false + } case key == gocui.KeySpace: - v.EditWrite(' ') + textArea.TypeRune(' ') case key == gocui.KeyInsert: - v.Overwrite = !v.Overwrite + textArea.ToggleOverwrite() case key == gocui.KeyCtrlU: - v.EditDeleteToStartOfLine() + textArea.DeleteToStartOfLine() case key == gocui.KeyCtrlA: - v.EditGotoToStartOfLine() + textArea.GoToStartOfLine() case key == gocui.KeyCtrlE: - v.EditGotoToEndOfLine() + textArea.GoToEndOfLine() // TODO: see if we need all three of these conditions: maybe the final one is sufficient case ch != 0 && mod == 0 && unicode.IsPrint(ch): - v.EditWrite(ch) + textArea.TypeRune(ch) default: - matched = false + return false } + return true +} + +// we've just copy+pasted the editor from gocui to here so that we can also re- +// render the commit message length on each keypress +func (gui *Gui) commitMessageEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool { + matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, true) + + // This function is called again on refresh as part of the general resize popup call, + // but we need to call it here so that when we go to render the text area it's not + // considered out of bounds to add a newline, meaning we can avoid unnecessary scrolling. + err := gui.resizePopupPanel(v, v.TextArea.GetContent()) + if err != nil { + gui.Log.Error(err) + } + v.RenderTextArea() gui.RenderCommitLength() return matched } func (gui *Gui) defaultEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool { - matched := true - switch { - case key == gocui.KeyBackspace || key == gocui.KeyBackspace2: - v.EditDelete(true) - case key == gocui.KeyCtrlD || key == gocui.KeyDelete: - v.EditDelete(false) - case key == gocui.KeyArrowDown: - v.MoveCursor(0, 1, false) - case key == gocui.KeyArrowUp: - v.MoveCursor(0, -1, false) - case key == gocui.KeyArrowLeft: - v.MoveCursor(-1, 0, false) - case key == gocui.KeyArrowRight: - v.MoveCursor(1, 0, false) - case key == gocui.KeySpace: - v.EditWrite(' ') - case key == gocui.KeyInsert: - v.Overwrite = !v.Overwrite - case key == gocui.KeyCtrlU: - v.EditDeleteToStartOfLine() - case key == gocui.KeyCtrlA: - v.EditGotoToStartOfLine() - case key == gocui.KeyCtrlE: - v.EditGotoToEndOfLine() + matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, false) - // TODO: see if we need all three of these conditions: maybe the final one is sufficient - case ch != 0 && mod == 0 && unicode.IsPrint(ch): - v.EditWrite(ch) - default: - matched = false - } + v.RenderTextArea() if gui.findSuggestions != nil { - input := v.Buffer() + input := v.TextArea.GetContent() suggestions := gui.findSuggestions(input) gui.setSuggestions(suggestions) } |