1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2026-06-09 22:05:16 +02:00
Files
lazygit/pkg/gocui/edit.go
Stefan Haller 5748d82073 Convert keybinding fields to Keybinding
Until now every keybinding config field was a plain string. That meant a user
couldn't ask for two keys to invoke a command — the config silently accepted
only one form.

Convert every string-typed field across all 13 KeybindingXxxConfig structs to
Keybinding so the union type extends to every command. Defaults wrap their
single-key value in Keybinding{...} so the generated Config.md still renders one
scalar key per binding.

The alt fields keep their separate Binding registrations for now: this commit
does not yet introduce the merge mechanism that folds them into the main field —
that comes in a follow-up. Consumers previously calling opts.GetKeys on a string
field now call opts.GetKeys on the Keybinding, or take .String() / Keys[0] where
a single value is needed.

Adds a Keybinding.String helper for rendering, schema-generator work that
inlines the Keybinding union into each consuming property, and a unit test
covering the user-facing scalar/sequence YAML forms for quit.
2026-05-25 15:32:47 +02:00

91 lines
3.0 KiB
Go

// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocui
import "github.com/samber/lo"
// Editor interface must be satisfied by gocui editors.
type Editor interface {
Edit(v *View, key Key) bool
}
// The EditorFunc type is an adapter to allow the use of ordinary functions as
// Editors. If f is a function with the appropriate signature, EditorFunc(f)
// is an Editor object that calls f.
type EditorFunc func(v *View, key Key) bool
// Edit calls f(v, key, mod)
func (f EditorFunc) Edit(v *View, key Key) bool {
return f(v, key)
}
// DefaultEditor is the default editor.
var DefaultEditor Editor = EditorFunc(SimpleEditor)
var (
moveWordLeftKeybinding = []Key{NewKey(KeyArrowLeft, "", ModCtrl)}
moveWordRightKeybinding = []Key{NewKey(KeyArrowRight, "", ModCtrl)}
backspaceWordKeybinding = []Key{NewKey(KeyBackspace, "", ModCtrl)}
forwardDeleteWordKeybinding = []Key{NewKey(KeyDelete, "", ModCtrl)}
)
// SimpleEditor is used as the default gocui editor.
func SimpleEditor(v *View, key Key) bool {
switch {
case lo.SomeBy(backspaceWordKeybinding, func(k Key) bool { return key.Equals(k) }),
key.Equals(NewKeyStrMod("w", ModCtrl)):
v.TextArea.BackSpaceWord()
case lo.SomeBy(forwardDeleteWordKeybinding, func(k Key) bool { return key.Equals(k) }),
key.Equals(NewKeyStrMod("d", ModAlt)):
v.TextArea.ForwardDeleteWord()
case key.Equals(NewKeyName(KeyBackspace)),
key.Equals(NewKeyStrMod("h", ModCtrl)):
v.TextArea.BackSpaceChar()
case key.Equals(NewKeyStrMod("d", ModCtrl)),
key.Equals(NewKeyName(KeyDelete)):
v.TextArea.DeleteChar()
case key.Equals(NewKeyName(KeyArrowDown)):
v.TextArea.MoveCursorDown()
case key.Equals(NewKeyName(KeyArrowUp)):
v.TextArea.MoveCursorUp()
case key.Equals(NewKeyStrMod("b", ModAlt)),
lo.SomeBy(moveWordLeftKeybinding, func(k Key) bool { return key.Equals(k) }):
v.TextArea.MoveLeftWord()
case key.Equals(NewKeyName(KeyArrowLeft)),
key.Equals(NewKeyStrMod("b", ModCtrl)):
v.TextArea.MoveCursorLeft()
case key.Equals(NewKeyStrMod("f", ModAlt)),
lo.SomeBy(moveWordRightKeybinding, func(k Key) bool { return key.Equals(k) }):
v.TextArea.MoveRightWord()
case key.Equals(NewKeyName(KeyArrowRight)),
key.Equals(NewKeyStrMod("f", ModCtrl)):
v.TextArea.MoveCursorRight()
case key.Equals(NewKeyName(KeyEnter)):
v.TextArea.TypeCharacter("\n")
case key.Equals(NewKeyName(KeyInsert)):
v.TextArea.ToggleOverwrite()
case key.Equals(NewKeyStrMod("u", ModCtrl)):
v.TextArea.DeleteToStartOfLine()
case key.Equals(NewKeyStrMod("k", ModCtrl)):
v.TextArea.DeleteToEndOfLine()
case key.Equals(NewKeyStrMod("a", ModCtrl)),
key.Equals(NewKeyName(KeyHome)):
v.TextArea.GoToStartOfLine()
case key.Equals(NewKeyStrMod("e", ModCtrl)),
key.Equals(NewKeyName(KeyEnd)):
v.TextArea.GoToEndOfLine()
case key.Equals(NewKeyStrMod("y", ModCtrl)):
v.TextArea.Yank()
case key.Str() != "" && key.Mod() == 0:
v.TextArea.TypeCharacter(key.Str())
default:
return false
}
v.RenderTextArea()
return true
}