Skip to content

Commit 2b2d12e

Browse files
committed
label-mode: direction-aware <Tab> and <BS>
1 parent 18b1faf commit 2b2d12e

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

autoload/sneak/label.vim

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,22 @@ endf
5656

5757
func! sneak#label#to(s, v, label) abort
5858
let seq = ""
59+
" TODO: handle g:sneak#absolute_dir=1
60+
let forward = !a:s._reverse
5961
while 1
60-
let choice = s:do_label(a:s, a:v, a:s._reverse, a:label)
62+
let choice = s:do_label(a:s, a:v, a:s._reverse, forward, a:label)
6163
let seq .= choice
6264
if choice =~# "^\<S-Tab>\\|\<BS>$"
63-
call a:s.init(a:s._input, a:s._repeatmotion, 1)
65+
call a:s.init(a:s._input, a:s._repeatmotion, forward ? 1 : 0)
6466
elseif choice ==# "\<Tab>"
65-
call a:s.init(a:s._input, a:s._repeatmotion, 0)
67+
call a:s.init(a:s._input, a:s._repeatmotion, forward ? 0 : 1)
6668
else
6769
return seq
6870
endif
6971
endwhile
7072
endf
7173

72-
func! s:do_label(s, v, reverse, label) abort "{{{
74+
func! s:do_label(s, v, reverse, forward, label) abort "{{{
7375
let w = winsaveview()
7476
call s:before()
7577
let search_pattern = (a:s.prefix).(a:s.search).(a:s.get_onscreen_searchpattern(w))
@@ -108,8 +110,15 @@ func! s:do_label(s, v, reverse, label) abort "{{{
108110
\ : mappedto =~# '<Plug>Sneak\(_;\|Next\)'
109111

110112
if choice =~# "\\v^\<Tab>|\<S-Tab>|\<BS>$" " Decorate next N matches.
111-
if (!a:reverse && choice ==# "\<Tab>") || (a:reverse && choice =~# "^\<S-Tab>\\|\<BS>$")
112-
call cursor(overflow[0], overflow[1])
113+
" TODO: handle g:sneak#absolute_dir=1
114+
if a:forward
115+
if (!a:reverse && choice ==# "\<Tab>") || (a:reverse && choice =~# "^\<S-Tab>\\|\<BS>$")
116+
call cursor(overflow[0], overflow[1])
117+
endif
118+
else
119+
if (a:reverse && choice ==# "\<Tab>") || (!a:reverse && choice =~# "^\<S-Tab>\\|\<BS>$")
120+
call cursor(overflow[0], overflow[1])
121+
endif
113122
endif " ...else we just switched directions, do not overflow.
114123
elseif (strlen(g:sneak_opt.label_esc) && choice ==# g:sneak_opt.label_esc)
115124
\ || -1 != index(["\<Esc>", "\<C-c>"], choice)

tests/test.vader

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,9 @@ Execute (cleanup):
733733
silent! xunmap t
734734
silent! xunmap T
735735

736+
############################################################
737+
# Label-mode <Tab>/<BS> when running out of labels
738+
736739
Given:
737740
A11111111111111111111111111111111111111111111111111111K
738741
B22222222222222222222222222222222222222222222222222222L
@@ -772,8 +775,8 @@ Expect:
772775
I11111111111111111111111111111111111111111111111111111S
773776
J22222222222222222222222222222222222222222222222222222T
774777

775-
Do (label/backwards/<BS>):
776-
4j9lS22\<BS>\<BS>fr-
778+
Do (label/backwards/delete):
779+
4j9lS22\<Tab>\<Tab>fr-
777780
Expect:
778781
A11111111111111111111111111111111111111111111111111111K
779782
B2222-222222222222222222222222222222222222222222222222L
@@ -786,6 +789,35 @@ Expect:
786789
I11111111111111111111111111111111111111111111111111111S
787790
J22222222222222222222222222222222222222222222222222222T
788791

792+
Do (label/backwards/change-direction/delete):
793+
4j9lS22\<Tab>\<Tab>\<BS>\<BS>\<BS>\<BS>Rr-
794+
Expect:
795+
A11111111111111111111111111111111111111111111111111111K
796+
B22222222222222222222222222222222222222222222222222222L
797+
C11111111111111111111111111111111111111111111111111111M
798+
D22222222222222222222222222222222222222222222222222222N
799+
E11111111111111111111111111111111111111111111111111111O
800+
F22222222222222222222222222222222222222222222222222222P
801+
G11111111111111111111111111111111111111111111111111111Q
802+
H22222222222222222222222222-22222222222222222222222222R
803+
I11111111111111111111111111111111111111111111111111111S
804+
J22222222222222222222222222222222222222222222222222222T
805+
806+
Do (label/backwards/change-direction/delete/repeat):
807+
4j9ldZ22\<Tab>\<BS>\<BS>N
808+
:\<C-U>doautocmd CursorMoved\<CR>
809+
.
810+
Expect:
811+
A11111111111111111111111111111111111111111111111111111K
812+
B22222222222222222222222222222222222222222222222222222L
813+
C11111111111111111111111111111111111111111111111111111M
814+
D22222222222222222222222222222222222222222222222222222N
815+
E11111111222222222222222222222222222222222222222222222R
816+
I11111111111111111111111111111111111111111111111111111S
817+
J22222222222222222222222222222222222222222222222222222T
818+
819+
# TODO: g:sneak#absolute_dir=1 tests
820+
789821
Execute (cleanup):
790822
let g:sneak#label = 0
791823
call SneakReset()

0 commit comments

Comments
 (0)