Skip to content

Commit

Permalink
Merge pull request #26 from kamykn/revert-23-fix/toggle_issue_21
Browse files Browse the repository at this point in the history
Revert "Add buffer toggle function #21"
  • Loading branch information
kamykn authored Nov 20, 2019
2 parents 2834bf0 + 8e9f64b commit e62b1d4
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 250 deletions.
8 changes: 1 addition & 7 deletions autoload/spelunker.vim
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,9 @@ 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 spelunker#toggle#is_enabled() == 0
if g:enable_spelunker_vim == 0
return 0
endif

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

function! spelunker#matches#delete_matches(word_list_for_delete, match_id_dict, window_id)
function! spelunker#matches#delete_matches(word_list_for_delete, match_id_dict)
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
" 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
call matchdelete(l:delete_match_id)
catch
" エラー読み捨て
finally
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
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
endtry
endif
Expand All @@ -88,28 +79,5 @@ 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: 2 additions & 32 deletions autoload/spelunker/test/test_match.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ 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 @@ -59,42 +56,15 @@ 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], l:win_id)
let l:match_id_list_after_delete = spelunker#matches#delete_matches(a:match_id_list[0], a:match_id_list[1])
" {'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, l:win_id)
let l:match_id_list_after_delete = spelunker#matches#delete_matches(l:all_ids, l:match_id_list_after_delete)
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: 10 additions & 104 deletions autoload/spelunker/test/test_toggle.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,23 @@ let s:save_cpo = &cpo
set cpo&vim

function! spelunker#test#test_toggle#test()
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()
call s:test_toggle()
endfunction

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

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

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

" highlightがなくなっていることを確認
call s:toggle(a:toggle_mode)
call spelunker#toggle#toggle()
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 s:toggle(a:toggle_mode)
call spelunker#toggle#toggle()
let l:result = getmatches()
call assert_equal([], l:result)

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

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

" spelunker#check_displayed_words spelunker#check "{{{
let g:spelunker_check_type = g:spelunker_check_type_buf_lead_write
Expand All @@ -89,15 +77,13 @@ function! s:test_toggle(toggle_mode)
" 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 @@ -107,17 +93,14 @@ function! s:test_toggle(toggle_mode)
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 @@ -128,12 +111,11 @@ function! s:test_toggle(toggle_mode)
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 @@ -144,7 +126,6 @@ function! s:test_toggle(toggle_mode)

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 @@ -157,80 +138,5 @@ function! s:test_toggle(toggle_mode)
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 e62b1d4

Please sign in to comment.