Skip to content

Commit

Permalink
Merge pull request #27 from kamykn/fix/toggle_func_bug
Browse files Browse the repository at this point in the history
Fix toggle func bug #21
  • Loading branch information
kamykn authored Nov 23, 2019
2 parents e62b1d4 + f3e6b9e commit ce888f1
Show file tree
Hide file tree
Showing 18 changed files with 467 additions and 30 deletions.
20 changes: 19 additions & 1 deletion autoload/spelunker.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set cpo&vim

function! spelunker#check_displayed_words()
if s:is_runnable() == 0
call s:clear_matches()
return 0
endif

Expand All @@ -23,6 +24,7 @@ endfunction

function! spelunker#check()
if s:is_runnable() == 0
call s:clear_matches()
return 0
endif

Expand Down Expand Up @@ -190,9 +192,25 @@ function! spelunker#toggle()
return 1
endfunction

" bufferごとのspelunkerの機能のon/off
function! spelunker#toggle_buffer()
call spelunker#toggle#toggle_buffer()
return 1
endfunction

function s:clear_matches()
if spelunker#toggle#is_enabled_buffer() == 0
call spelunker#matches#clear_current_buffer_matches()
endif

if spelunker#toggle#is_enabled_global() == 0
call spelunker#matches#clear_matches()
endif
endfunction

" 実行可能な条件のチェック
function s:is_runnable()
if g:enable_spelunker_vim == 0
if spelunker#toggle#is_enabled() == 0
return 0
endif

Expand Down
42 changes: 37 additions & 5 deletions autoload/spelunker/matches.vim
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,29 @@ function spelunker#matches#get_match_pattern(word)
return l:pattern
endfunction

function! spelunker#matches#delete_matches(word_list_for_delete, match_id_dict)
function! spelunker#matches#delete_matches(word_list_for_delete, match_id_dict, window_id)
let l:match_id_dict = a:match_id_dict

for l:word in a:word_list_for_delete
let l:delete_match_id = get(l:match_id_dict, l:word, 0)
if l:delete_match_id > 0
let l:is_ok = 1
try
call matchdelete(l:delete_match_id)
" recommend version is => 8.1.1739
" https://github.com/vim/vim/issues/4720
let l:is_ok = matchdelete(l:delete_match_id, a:window_id)
if l:is_ok == -1 && a:window_id == win_getid()
" 第2引数がある場合に上手く削除できない不具合があった時期があったため
let l:is_ok = matchdelete(l:delete_match_id)
endif
catch
" エラー読み捨て
finally
let l:del_index = index(values(l:match_id_dict), l:delete_match_id)
if l:del_index != 1
call remove(l:match_id_dict, keys(l:match_id_dict)[l:del_index])
if l:is_ok == 0
let l:del_index = index(values(l:match_id_dict), l:delete_match_id)
if l:del_index != 1
call remove(l:match_id_dict, keys(l:match_id_dict)[l:del_index])
endif
endif
endtry
endif
Expand All @@ -79,5 +88,28 @@ function! spelunker#matches#delete_matches(word_list_for_delete, match_id_dict)
return l:match_id_dict
endfunction

function! spelunker#matches#clear_matches()
" matchからの削除処理を利用してハイライト削除
if exists('b:match_id_dict')
for l:window_id in keys(b:match_id_dict)
let b:match_id_dict[l:window_id] =
\ spelunker#matches#delete_matches(keys(b:match_id_dict[l:window_id]), b:match_id_dict[l:window_id], l:window_id)
endfor
endif
endfunction

function! spelunker#matches#clear_current_buffer_matches()
" matchからの削除処理を利用してハイライト削除
if exists('b:match_id_dict')
let l:window_id = win_getid()

if exists('b:match_id_dict[l:window_id]')
let b:match_id_dict[l:window_id] =
\ spelunker#matches#delete_matches(keys(b:match_id_dict[l:window_id]), b:match_id_dict[l:window_id], l:window_id)
endif
endif
endfunction


let &cpo = s:save_cpo
unlet s:save_cpo
34 changes: 32 additions & 2 deletions autoload/spelunker/test/test_match.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ function! spelunker#test#test_match#test()
call s:test_get_match_pattern()
let l:match_id_list = s:test_add_matches()
call s:test_delete_matches(l:match_id_list)

call s:test_clear_matches()
call s:test_clear_buffer_matches()
endfunction

function! s:test_get_match_pattern()
Expand Down Expand Up @@ -56,15 +59,42 @@ function! s:test_add_matches()
endfunction

function! s:test_delete_matches(match_id_list)
let l:win_id = win_getid()
call spelunker#test#open_unit_test_buffer('match', 'add_matches.txt')
let l:match_id_list_after_delete = spelunker#matches#delete_matches(a:match_id_list[0], a:match_id_list[1])
let l:match_id_list_after_delete = spelunker#matches#delete_matches(a:match_id_list[0], a:match_id_list[1], l:win_id)
" {'orange': 5, 'peach': 8, 'apple': 4, 'grape': 9}
call assert_equal(['orange', 'peach', 'apple', 'grape'], keys(l:match_id_list_after_delete))

let l:all_ids = keys(l:match_id_list_after_delete)
let l:match_id_list_after_delete = spelunker#matches#delete_matches(l:all_ids, l:match_id_list_after_delete)
let l:match_id_list_after_delete = spelunker#matches#delete_matches(l:all_ids, l:match_id_list_after_delete, l:win_id)
call assert_equal({}, l:match_id_list_after_delete)
endfunction

function! s:test_clear_matches()
call spelunker#test#open_unit_test_buffer('match', 'clear_matches.txt')

let l:win_id = win_getid()
let b:match_id_dict = {}
let [l:word_list_for_delete_match, b:match_id_dict[l:win_id]]
\ = spelunker#matches#add_matches(['appl', 'orangg', 'banna'], {})

call assert_notequal({}, b:match_id_dict[l:win_id])
call spelunker#matches#clear_matches()
call assert_equal({l:win_id: {}}, b:match_id_dict)
endfunction

function! s:test_clear_buffer_matches()
call spelunker#test#open_unit_test_buffer('match', 'clear_matches.txt')

let l:win_id = win_getid()
let b:match_id_dict = {}
let [l:word_list_for_delete_match, b:match_id_dict[l:win_id]]
\ = spelunker#matches#add_matches(['appl', 'orangg', 'banna'], {})

call assert_notequal({}, b:match_id_dict[l:win_id])
call spelunker#matches#clear_current_buffer_matches()
call assert_equal({l:win_id: {}}, b:match_id_dict)
endfunction

let &cpo = s:save_cpo
unlet s:save_cpo
Loading

0 comments on commit ce888f1

Please sign in to comment.