Skip to content

Commit

Permalink
small followup for #74
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 15, 2022
1 parent c853455 commit 82f8531
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
17 changes: 11 additions & 6 deletions autoload/clever_f.vim
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,21 @@ function! clever_f#_mark_direct(forward, count) abort

let char_count = {}
let matches = []
if a:forward
let line = split(line[c - 1 : ], '\zs') " adding a limit like c+4*termcol*termlines just after `:` is a free gift in a very long line, assuming all char are less than 4 bytes.
if a:forward
" adding a limit like c+4*termcol*termlines just after `:` is a free gift in a very long line, assuming all
" chars are less than 4 bytes. (#74)
let line = split(line[c - 1 : ], '\zs')
let i = c - 1 + len(line[0])
let line = line[1:] " skip char under cursor
else
let line = reverse(split(line[0 : c - 2], '\zs')) " split() is slow on very long lines, new option to use `max(0,c-4*termcol*termlines-1000)` instead of `0` would help on a very long line, with the drawback of seeking at an arbitrarily byte possibly in the middle of a multibyte char (which would be off screen)
" split() is slow on very long lines, new option to use `max(0,c-4*termcol*termlines-1000)` instead of `0` would
" help on a very long line, with the drawback of seeking at an arbitrarily byte possibly in the middle of a
" multibyte char (which would be off screen). (#74)
let line = reverse(split(line[0 : c - 2], '\zs'))
let i = c - 1
endif
for ch in line
if !a:forward
if !a:forward
let i -= len(ch)
endif
let ch_lower = tolower(ch)
Expand All @@ -180,10 +185,10 @@ function! clever_f#_mark_direct(forward, count) abort
\ (g:clever_f_smart_case && char_count[ch_lower] == a:count)
" NOTE: should not use `matchaddpos(group, [...position])`,
" because the maximum number of position is 8
let m = matchaddpos('CleverFDirect', [[l, i + 1]])
let m = matchaddpos('CleverFDirect', [[l, i + 1]]) " `+ 1` since column is 1-based
call add(matches, m)
endif
if a:forward
if a:forward
let i += len(ch)
endif
endfor
Expand Down
17 changes: 15 additions & 2 deletions test/test.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ function! WaitUntil(ms, pred) abort
return 0
endfunction

function! s:find_cols(heystack, needles) abort
let offsets = []
let current = 0
for c in split(a:heystack, '\zs')
if index(a:needles, c) >= 0
" Columns start from 1, not 0
let offsets += [current + 1]
endif
let current += len(c)
endfor
return offsets
endfunction

Describe default config
It should load plugin
Assert g:loaded_clever_f
Expand Down Expand Up @@ -1283,7 +1296,7 @@ Describe clever_f#_mark_direct()
" #ムかわいいよzビムx
let s = 'ビ####い#_#ム_'
call clever_f#_mark_direct(1, 1)
let cols = filter(range(1,len(s)),'s[v:val-1]=="_"||s[v:val-1]=="#"[0]')
let cols = s:find_cols(s, ['_', ''])
Assert Equals(sort(map(getmatches(), 'v:val.pos1[1]'), 'n'), cols)
End

Expand All @@ -1292,7 +1305,7 @@ Describe clever_f#_mark_direct()
" ビムかわいいよzビ#x
let s = 'ビ###い##_#ムx'
call clever_f#_mark_direct(0, 1)
let cols = filter(range(1,len(s)),'s[v:val-1]=="_"||s[v:val-1]=="#"[0]')
let cols = s:find_cols(s, ['_', ''])
Assert Equals(sort(map(getmatches(), 'v:val.pos1[1]'), 'n'), cols)
End

Expand Down

0 comments on commit 82f8531

Please sign in to comment.