m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-16 07:13:48 -05:00

Add sub-word actions (#3997)

Add `backward-subword`, `forward-subword`, `kill-subword`, `backward-kill-subword` actions.
This commit is contained in:
Massimo Mund
2025-09-05 12:38:22 +02:00
committed by GitHub
parent 9ed971cc90
commit ae12e94b1f
5 changed files with 221 additions and 145 deletions

View File

@@ -291,6 +291,8 @@ type Terminal struct {
gapLineLen int
wordRubout string
wordNext string
subWordRubout string
subWordNext string
cx int
cy int
offset int
@@ -512,6 +514,7 @@ const (
actBackwardDeleteChar
actBackwardDeleteCharEof
actBackwardWord
actBackwardSubWord
actCancel
actChangeBorderLabel
@@ -541,12 +544,15 @@ const (
actFatal
actForwardChar
actForwardWord
actForwardSubWord
actKillLine
actKillWord
actKillSubWord
actUnixLineDiscard
actUnixWordRubout
actYank
actBackwardKillWord
actBackwardKillSubWord
actSelectAll
actDeselectAll
actToggle
@@ -937,6 +943,8 @@ func NewTerminal(opts *Options, eventBox *util.EventBox, executor *util.Executor
}
wordRubout := "[^\\pL\\pN][\\pL\\pN]"
wordNext := "[\\pL\\pN][^\\pL\\pN]|(.$)"
subWordRubout := "[a-z][A-Z]|[^\\pL\\pN][\\pL\\pN]"
subWordNext := "[a-z][A-Z]|[\\pL\\pN][^\\pL\\pN]|(.$)"
if opts.FileWord {
sep := regexp.QuoteMeta(string(os.PathSeparator))
wordRubout = fmt.Sprintf("%s[^%s]", sep, sep)
@@ -970,6 +978,8 @@ func NewTerminal(opts *Options, eventBox *util.EventBox, executor *util.Executor
markerMultiLine: *opts.MarkerMulti,
wordRubout: wordRubout,
wordNext: wordNext,
subWordRubout: subWordRubout,
subWordNext: subWordNext,
cx: len(input),
cy: 0,
offset: 0,
@@ -6021,6 +6031,11 @@ func (t *Terminal) Loop() error {
if t.cx > 0 {
t.rubout(t.wordRubout)
}
case actBackwardKillSubWord:
beof = len(t.input) == 0
if t.cx > 0 {
t.rubout(t.subWordRubout)
}
case actYank:
suffix := copySlice(t.input[t.cx:])
t.input = append(append(t.input[:t.cx], t.yanked...), suffix...)
@@ -6132,6 +6147,10 @@ func (t *Terminal) Loop() error {
t.cx = findLastMatch(t.wordRubout, string(t.input[:t.cx])) + 1
case actForwardWord:
t.cx += findFirstMatch(t.wordNext, string(t.input[t.cx:])) + 1
case actBackwardSubWord:
t.cx = findLastMatch(t.subWordRubout, string(t.input[:t.cx])) + 1
case actForwardSubWord:
t.cx += findFirstMatch(t.subWordNext, string(t.input[t.cx:])) + 1
case actKillWord:
ncx := t.cx +
findFirstMatch(t.wordNext, string(t.input[t.cx:])) + 1
@@ -6139,6 +6158,13 @@ func (t *Terminal) Loop() error {
t.yanked = copySlice(t.input[t.cx:ncx])
t.input = append(t.input[:t.cx], t.input[ncx:]...)
}
case actKillSubWord:
ncx := t.cx +
findFirstMatch(t.subWordNext, string(t.input[t.cx:])) + 1
if ncx > t.cx {
t.yanked = copySlice(t.input[t.cx:ncx])
t.input = append(t.input[:t.cx], t.input[ncx:]...)
}
case actKillLine:
if t.cx < len(t.input) {
t.yanked = copySlice(t.input[t.cx:])