Skip to content

Commit

Permalink
Merge pull request #23 from kamykn/fix/toggle_issue_21
Browse files Browse the repository at this point in the history
Add buffer toggle function #21
  • Loading branch information
kamykn authored Nov 20, 2019
2 parents 7a67826 + eaf4f8e commit e314a87
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 27 deletions.
8 changes: 7 additions & 1 deletion autoload/spelunker.vim
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,15 @@ function! spelunker#toggle()
return 1
endfunction

" bufferごとのspelunkerの機能のon/off
function! spelunker#toggle_buffer()
call spelunker#toggle#toggle_buffer()
return 1
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
114 changes: 104 additions & 10 deletions autoload/spelunker/test/test_toggle.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,35 @@ let s:save_cpo = &cpo
set cpo&vim

function! spelunker#test#test_toggle#test()
call s:test_toggle()
call s:test_toggle(1) " test global toggle
call s:test_toggle(2) " test local toggle
call s:test_is_enabled()
call s:test_is_enabled_global()
call s:test_is_enabled_buffer()

call s:force_enable()
endfunction

function! s:test_toggle()
" toggle_mode
" 1: global mode
" 2: buffer mode
function! s:test_toggle(toggle_mode)

" [case10-0] =====================================
call spelunker#test#open_unit_test_buffer('toggle', 'toggle1.txt')
call spelunker#test#init()
call spelunker#toggle#toggle()

call s:force_enable()
call s:toggle(a:toggle_mode)

" highlightがなくなっていることを確認
call spelunker#toggle#toggle()
call s:toggle(a:toggle_mode)
let l:result = getmatches()
call assert_equal(1, len(l:result))
call assert_equal('SpelunkerSpellBad', l:result[0]['group'])
call assert_equal('\v[A-Za-z]@<!appl[a-z]@!\C', l:result[0]['pattern'])
call assert_equal(0, l:result[0]['priority'])
call spelunker#toggle#toggle()
call s:toggle(a:toggle_mode)
let l:result = getmatches()
call assert_equal([], l:result)

Expand All @@ -51,7 +63,7 @@ function! s:test_toggle()
call assert_equal(0, spelunker#execute_with_target_word(''))

" [case10-1] =====================================
call spelunker#toggle#toggle()
call s:toggle(a:toggle_mode)

" spelunker#check_displayed_words spelunker#check "{{{
let g:spelunker_check_type = g:spelunker_check_type_buf_lead_write
Expand All @@ -77,13 +89,15 @@ function! s:test_toggle()
" register word dict test
call spelunker#test#open_unit_test_buffer('toggle', 'toggle2.txt')
call spelunker#test#init()

call cursor(1,1)
let l:line = spelunker#spellbad#get_spell_bad_list(1, -1)
call assert_equal(['addgoodword'], l:line)

call assert_equal(1, spelunker#execute_with_target_word('spellgood!'))
let l:line = spelunker#spellbad#get_spell_bad_list(1, -1)
call assert_equal([], l:line)
call execute('spellundo! addgoodword')

call spelunker#test#reload_buffer()
call cursor(2,1)
Expand All @@ -93,14 +107,17 @@ function! s:test_toggle()
call assert_equal(1, spelunker#execute_with_target_word('spellwrong!'))
let l:line = spelunker#spellbad#get_spell_bad_list(2, -1)
call assert_equal(['wrong'], l:line)
call execute('spellundo! wrong')
" }}}

" [case11-0] =====================================
call spelunker#toggle#toggle()

" spelunker#correct
call spelunker#test#open_unit_test_buffer('toggle', 'toggle3.txt')
call spelunker#test#init()

call s:force_enable()
call s:toggle(a:toggle_mode)

call cursor(1, 2)
call assert_equal(0, spelunker#correct())
call assert_equal(0, spelunker#correct_all())
Expand All @@ -111,11 +128,12 @@ function! s:test_toggle()
call assert_equal('aple', expand("<cword>"))

" [case11-1] =====================================
call spelunker#toggle#toggle()

" spelunker#correct
call spelunker#test#reload_buffer()
call spelunker#test#init()

call s:force_enable()

call cursor(1, 2)
" call assert_equal(0, spelunker#correct())
" call assert_equal(0, spelunker#correct_all())
Expand All @@ -126,6 +144,7 @@ function! s:test_toggle()

call spelunker#test#reload_buffer()
call spelunker#test#init()

call cursor(1, 2)
call assert_equal(1, spelunker#correct_all_feeling_lucky())
call assert_equal('apple', expand("<cword>"))
Expand All @@ -138,5 +157,80 @@ function! s:test_toggle()
call spelunker#test#reload_buffer()
endfunction

" toggle_mode
" 1: global mode
" 2: buffer mode
function! s:toggle(toggle_mode)
if a:toggle_mode == 1
silent! call spelunker#toggle#toggle()
elseif a:toggle_mode == 2
silent! call spelunker#toggle#toggle_buffer()
endif
endfunction

function! s:test_is_enabled()
call spelunker#test#open_unit_test_buffer('toggle', 'toggle1.txt')
call spelunker#test#init()

call assert_equal(1, spelunker#toggle#is_enabled())

" disabled (global)
call spelunker#toggle#toggle()
call assert_equal(0, spelunker#toggle#is_enabled())

" enabled (global)
call spelunker#toggle#toggle()
call assert_equal(1, spelunker#toggle#is_enabled())

" disabled (global)
call spelunker#toggle#toggle_buffer()
call assert_equal(0, spelunker#toggle#is_enabled())

" enabled (global)
call spelunker#toggle#toggle_buffer()
call assert_equal(1, spelunker#toggle#is_enabled())

" enabled with global toggle
call spelunker#toggle#toggle() " disabled (global)
call spelunker#toggle#toggle_buffer() " disabled (buffer)
call spelunker#toggle#toggle() " disabled (global)
call assert_equal(1, spelunker#toggle#is_enabled())
endfunction

function! s:test_is_enabled_global()
call spelunker#test#open_unit_test_buffer('toggle', 'toggle1.txt')
call spelunker#test#init()

call assert_equal(1, spelunker#toggle#is_enabled_global())

" disabled (global)
call spelunker#toggle#toggle()
call assert_equal(0, spelunker#toggle#is_enabled_global())

" enabled (global)
call spelunker#toggle#toggle()
call assert_equal(1, spelunker#toggle#is_enabled_global())
endfunction

function! s:test_is_enabled_buffer()
call spelunker#test#open_unit_test_buffer('toggle', 'toggle1.txt')
call spelunker#test#init()

call assert_equal(1, spelunker#toggle#is_enabled_buffer())

" disabled (global)
call spelunker#toggle#toggle_buffer()
call assert_equal(0, spelunker#toggle#is_enabled_buffer())

" enabled (global)
call spelunker#toggle#toggle_buffer()
call assert_equal(1, spelunker#toggle#is_enabled_buffer())
endfunction

function! s:force_enable()
let g:enable_spelunker_vim = 1
let b:enable_spelunker_vim = 1
endfunction

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

0 comments on commit e314a87

Please sign in to comment.