From bd9a93c09d55e62e494c9d66ff781a548422be5f Mon Sep 17 00:00:00 2001 From: kamykn Date: Fri, 22 Nov 2019 00:39:33 +0900 Subject: [PATCH] Revert "Revert "Add buffer toggle function #21"" This reverts commit 8e9f64bc71be3317706dac62a6aade09ab223cb8. --- autoload/spelunker.vim | 8 +- autoload/spelunker/matches.vim | 42 +++++++-- autoload/spelunker/test/test_match.vim | 34 ++++++- autoload/spelunker/test/test_toggle.vim | 114 +++++++++++++++++++++--- autoload/spelunker/toggle.vim | 68 ++++++++++++-- autoload/spelunker/words.vim | 2 +- plugin/spelunker.vim | 6 ++ test/unit_test/match/clear_matches.txt | 3 + 8 files changed, 250 insertions(+), 27 deletions(-) create mode 100644 test/unit_test/match/clear_matches.txt diff --git a/autoload/spelunker.vim b/autoload/spelunker.vim index 64bbadd..1e7c903 100644 --- a/autoload/spelunker.vim +++ b/autoload/spelunker.vim @@ -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 diff --git a/autoload/spelunker/matches.vim b/autoload/spelunker/matches.vim index f7c1560..6937dad 100644 --- a/autoload/spelunker/matches.vim +++ b/autoload/spelunker/matches.vim @@ -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 @@ -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 diff --git a/autoload/spelunker/test/test_match.vim b/autoload/spelunker/test/test_match.vim index a2818bc..0af8752 100644 --- a/autoload/spelunker/test/test_match.vim +++ b/autoload/spelunker/test/test_match.vim @@ -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() @@ -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 diff --git a/autoload/spelunker/test/test_toggle.vim b/autoload/spelunker/test/test_toggle.vim index 06c03bc..377cf25 100644 --- a/autoload/spelunker/test/test_toggle.vim +++ b/autoload/spelunker/test/test_toggle.vim @@ -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]@")) " [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()) @@ -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("")) @@ -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 diff --git a/autoload/spelunker/toggle.vim b/autoload/spelunker/toggle.vim index b3985bb..3b14225 100644 --- a/autoload/spelunker/toggle.vim +++ b/autoload/spelunker/toggle.vim @@ -11,15 +11,43 @@ set cpo&vim function! spelunker#toggle#toggle() let g:enable_spelunker_vim = g:enable_spelunker_vim == 1 ? 0 : 1 - if g:enable_spelunker_vim == 0 - " 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]) - endfor - endif + " onにするときは今開いているbufferも連動させる + if g:enable_spelunker_vim == 1 + let b:enable_spelunker_vim = 1 + endif + + call spelunker#toggle#init_buffer(1, g:enable_spelunker_vim) + + if g:enable_spelunker_vim == 1 + echom 'Spelunker.vim is now enabled.' else + echom 'Spelunker.vim has been disabled.' + endif +endfunction + +function! spelunker#toggle#toggle_buffer() + if !exists('b:enable_spelunker_vim') + let b:enable_spelunker_vim = 1 + endif + + let b:enable_spelunker_vim = b:enable_spelunker_vim == 1 ? 0 : 1 + call spelunker#toggle#init_buffer(2, b:enable_spelunker_vim) + + if b:enable_spelunker_vim == 1 + echom 'Spelunker.vim is now enabled in a buffer.' + else + echom 'Spelunker.vim has been disabled in a buffer.' + endif +endfunction + +function! spelunker#toggle#init_buffer(mode, is_enabled) + if a:is_enabled == 0 + if a:mode == 1 " for global + call spelunker#matches#clear_matches() + elseif a:mode == 2 " for buffer + call spelunker#matches#clear_current_buffer_matches() + endif + elseif a:is_enabled == 1 if g:spelunker_check_type == g:spelunker_check_type_buf_lead_write call spelunker#check() elseif g:spelunker_check_type == g:spelunker_check_type_cursor_hold @@ -28,5 +56,29 @@ function! spelunker#toggle#toggle() endif endfunction +function! spelunker#toggle#is_enabled() + if spelunker#toggle#is_enabled_buffer() == 0 || spelunker#toggle#is_enabled_global() == 0 + return 0 + endif + + return 1 +endfunction + +function! spelunker#toggle#is_enabled_global() + if g:enable_spelunker_vim == 0 + return 0 + endif + return 1 +endfunction + + +function! spelunker#toggle#is_enabled_buffer() + if exists('b:enable_spelunker_vim') && b:enable_spelunker_vim == 0 + return 0 + endif + + return 1 +endfunction + let &cpo = s:save_cpo unlet s:save_cpo diff --git a/autoload/spelunker/words.vim b/autoload/spelunker/words.vim index 2e486e1..ef7f355 100644 --- a/autoload/spelunker/words.vim +++ b/autoload/spelunker/words.vim @@ -206,7 +206,7 @@ function! spelunker#words#highlight(spell_bad_list) endif let b:match_id_dict[l:window_id] = - \ spelunker#matches#delete_matches(l:word_list_for_delete_match, b:match_id_dict[l:window_id]) + \ spelunker#matches#delete_matches(l:word_list_for_delete_match, b:match_id_dict[l:window_id], l:window_id) endfunction let &cpo = s:save_cpo diff --git a/plugin/spelunker.vim b/plugin/spelunker.vim index 0c49398..44642b3 100644 --- a/plugin/spelunker.vim +++ b/plugin/spelunker.vim @@ -242,6 +242,12 @@ if !hasmapto('(spelunker-toggle)') silent! nmap ZT (spelunker-toggle) endif +" [toggle in the buffer feature]=============================================================== +nnoremap (spelunker-toggle-buffer) :call spelunker#toggle_buffer() +if !hasmapto('(spelunker-toggle-buffer)') + silent! nmap Zt (spelunker-toggle-buffer) +endif + " [augroup] ================================================================== if g:spelunker_disable_auto_group == 0 augroup spelunker diff --git a/test/unit_test/match/clear_matches.txt b/test/unit_test/match/clear_matches.txt new file mode 100644 index 0000000..8b9580f --- /dev/null +++ b/test/unit_test/match/clear_matches.txt @@ -0,0 +1,3 @@ +appl +orangg +banna