diff --git a/README.md b/README.md index fcc3032..71b1f78 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,18 @@ Zt -### 3.6 CtrlP Extention +### 3.6 Option to disable word checking. +#### Disable URI checking. +``` +let g:spelunker_disable_uri_checking = 1 +``` + +#### Disable checking words in backtick/backquote. +``` +let g:spelunker_disable_uri_checking = 1 +``` + +### 3.7 CtrlP Extention #### CtrlPSpell [ctrlp](https://github.com/ctrlpvim/ctrlp.vim) is fuzzy finder. diff --git a/autoload/spelunker/get_buffer.vim b/autoload/spelunker/get_buffer.vim new file mode 100644 index 0000000..6d46f4a --- /dev/null +++ b/autoload/spelunker/get_buffer.vim @@ -0,0 +1,88 @@ +scriptencoding utf-8 + +let s:save_cpo = &cpo +set cpo&vim + +function! spelunker#get_buffer#all() + let l:window_text_list = getline(1, '$') + + let l:newline_character = s:get_newline_character() + let l:window_text = join(l:window_text_list, l:newline_character) + + let l:window_text = spelunker#get_buffer#filter_uri(l:window_text) + let l:window_text = spelunker#get_buffer#filter_backquoted_words(l:window_text, l:newline_character) + + return split(l:window_text, l:newline_character) +endfunction + +function! spelunker#get_buffer#displayed() + " filter済みのbufferを変数で保持する + " 変数と実際のバッファーは行数が一致するようにする + " filter済みのbufferで処理をしながら、foldの判定を実際のbufferで判定する + let l:filtered_buffer = spelunker#get_buffer#all() + + let l:current_line = line('w0') + let l:end_line = line('w$') + + let l:window_text_list = [] + + while 1 + if foldclosed(l:current_line) > 0 + let l:current_line = foldclosedend(l:current_line) + 1 + endif + + if l:current_line > l:end_line + break + endif + + " 配列なので現在行-1のindexで取得 + let l:line = get(l:filtered_buffer, l:current_line - 1, '') + if l:line != '' + call add(l:window_text_list, l:line) + endif + + let l:current_line = l:current_line + 1 + endwhile + + return l:window_text_list +endfunction + +function! s:get_newline_character() + return has('win32') || has('win64') ? "\r": "\n" +endfunc + +function! spelunker#get_buffer#filter_uri(text) + if g:spelunker_disable_uri_checking == 0 + return a:text + endif + + " FYI: https://vi.stackexchange.com/questions/3990/ignore-urls-and-email-addresses-in-spell-file/24534#24534 + return substitute(a:text, '\w\+:\/\/[^[:space:]]\+', '', 'g') +endfunction + +function! spelunker#get_buffer#filter_backquoted_words(text, newline_character) + " for shell command + " ex) `ls -la` + if g:spelunker_disable_backquoted_checking == 0 + return a:text + endif + + " [バッククオート内の文字列削除] + " substituteを2回実行する + " 関数のatomは後方参照出来ないので注意 + " 1回目: 改行を含む`以外の文字のマッチ + " 2回目: マッチした文字列の改行以外を全部消す + " + " [考慮点] + " 1: 改行を考慮 + " ex) ``` + " `aaa + " bbb` + " ``` + " 2: 末尾のバッククオートを考慮 + " ex) ``` + " aaaa` + " bbb`ccc + " ``` + return substitute(a:text, '`\([^`]*[' . a:newline_character . ']*\)\+`', '\=substitute(submatch(0), "[^' . a:newline_character . ']", "", "g")', 'g') +endfunction diff --git a/autoload/spelunker/jump.vim b/autoload/spelunker/jump.vim index f91d2fd..0bac534 100644 --- a/autoload/spelunker/jump.vim +++ b/autoload/spelunker/jump.vim @@ -17,11 +17,13 @@ function! spelunker#jump#jump_matched(is_search_next) let l:end_line = s:get_end_of_line(a:is_search_next) let l:is_enable_wrapscan = s:is_enable_wrapscan() + let l:filtered_buffer = spelunker#get_buffer#all() + " 表示範囲だけhighlightしている場合もあるので、1行ずつチェックしていく while 1 let l:matched_pos = -1 - let l:spell_bad_list = spelunker#spellbad#get_spell_bad_list(l:current_line, -1) + let l:spell_bad_list = spelunker#spellbad#get_spell_bad_list([get(l:filtered_buffer, l:current_line - 1, '')]) if len(l:spell_bad_list) > 0 for word in l:spell_bad_list diff --git a/autoload/spelunker/spellbad.vim b/autoload/spelunker/spellbad.vim index 8d1b04e..6cb9919 100644 --- a/autoload/spelunker/spellbad.vim +++ b/autoload/spelunker/spellbad.vim @@ -9,21 +9,13 @@ let s:save_cpo = &cpo set cpo&vim " end_lineの指定がなければ1行のみ -function! spelunker#spellbad#get_spell_bad_list(start_line, end_line) +function! spelunker#spellbad#get_spell_bad_list(window_text_list) call spelunker#white_list#init_white_list() - let l:window_text_list = [] - if a:end_line >= 0 - let l:window_text_list = getline(a:start_line, a:end_line) - else - " 1行のみ - call add(l:window_text_list, getline(a:start_line)) - endif - " spellgood で対象から外れる場合もあるので、全部チェックする必要があり " NOTE: spellgood系操作でmatch_id_dictから消してあげたらチェック不要になる。 " ただし、match_id_dictをglobalにする必要あり - let l:word_list = s:get_word_list(l:window_text_list) + let l:word_list = s:get_word_list(a:window_text_list) let l:current_spell_setting = spelunker#get_current_spell_setting() setlocal spell diff --git a/autoload/spelunker/test.vim b/autoload/spelunker/test.vim index bf595ab..9f48d5f 100644 --- a/autoload/spelunker/test.vim +++ b/autoload/spelunker/test.vim @@ -24,6 +24,7 @@ function! spelunker#test#check(no_exit) call spelunker#test#test_toggle#test() call spelunker#test#test_words#test() call spelunker#test#test_correct#test() + call spelunker#test#test_get_buffer#test() catch echomsg 'error occurred:' . v:exception @@ -34,7 +35,7 @@ function! spelunker#test#check(no_exit) endtry if len(v:errors) >= 1 - echo v:errors + echomsg v:errors " error exit if a:no_exit != 1 execute 'cquit!' diff --git a/autoload/spelunker/test/test_get_buffer.vim b/autoload/spelunker/test/test_get_buffer.vim new file mode 100644 index 0000000..ce73ae7 --- /dev/null +++ b/autoload/spelunker/test/test_get_buffer.vim @@ -0,0 +1,104 @@ +scriptencoding utf-8 + +let s:save_cpo = &cpo +set cpo&vim + +function! spelunker#test#test_get_buffer#test() + " init + let g:spelunker_disable_backquoted_checking = 1 + let g:spelunker_disable_uri_checking = 1 + + call s:test_all() + call s:test_displayed() + call s:test_disable_url_checking() + call s:test_disable_backquoted_checking() + call s:test_folded() +endfunction + +function! s:test_all() + call spelunker#test#open_unit_test_buffer('words', 'highlight.txt') + call assert_equal(['aple banan lemn', 'apple_banana_lemon', '', 'AppleBananaLemon'], spelunker#get_buffer#all()) +endfunction + +function! s:test_displayed() + call spelunker#test#open_unit_test_buffer('words', 'check.txt') + call assert_equal(['appl banan'], spelunker#get_buffer#displayed()) +endfunction + +function! s:test_disable_url_checking() + call spelunker#test#open_unit_test_buffer('get_buffer', 'disable_url.txt') + call assert_equal( + \ ['abc def', '', 'ghi', '', 'jkl'], + \ spelunker#get_buffer#all() + \ ) + + let g:spelunker_disable_uri_checking = 0 + call assert_equal( + \ ['http://github.com', '', 'abc http://github.com def', '', 'ghi', 'http://github.com', 'jkl'], + \ spelunker#get_buffer#all() + \ ) + + let g:spelunker_disable_uri_checking = 1 +endfunction + +function! s:test_disable_backquoted_checking() + call spelunker#test#open_unit_test_buffer('get_buffer', 'disable_backquote.txt') + call assert_equal( + \ ['abc', '', ' def', '', 'ghi ', ' jkl', '', 'mno', '', 'pqr', '', '', '', '', '', '', ''], + \ spelunker#get_buffer#all() + \ ) + + let g:spelunker_disable_backquoted_checking = 0 + call assert_equal( + \ [ + \ 'abc', '', '`aaa` def', '', 'ghi `fff', 'fff` jkl', '', 'mno`', 'aaa', '`pqr', '', '`ccc', + \ 'ddd', 'eee`', '', '`c', 'd', 'e`' + \ ], + \ spelunker#get_buffer#all() + \ ) + let g:spelunker_disable_backquoted_checking = 1 +endfunction + +function! s:test_folded() + call spelunker#test#open_unit_test_buffer('get_buffer', 'folded.txt') + + set foldmethod=marker + set foldcolumn=1 + set foldlevel=0 + + call assert_equal( + \ [ + \ '" vim: foldmethod=marker', '" vim: foldcolumn=3', '" vim: foldlevel=0', '', '', '', '', '', '', + \ '', '', '', '', 'aaaaaaa', '', '', '', '', 'grape', '', 'pineappple', '', '', '', '', '', '' + \ ], + \ spelunker#get_buffer#all() + \ ) + + call assert_equal( + \ ['" vim: foldmethod=marker', '" vim: foldcolumn=3', '" vim: foldlevel=0', 'grape', 'pineappple'], + \ spelunker#get_buffer#displayed() + \ ) + + let g:spelunker_disable_backquoted_checking = 0 + call assert_equal( + \ [ + \ '" vim: foldmethod=marker', '" vim: foldcolumn=3', '" vim: foldlevel=0', '', '', '', '`ccccccccc', + \ '', 'orange peach meron', '" {{{', "\tbanana", "\tbannana", "\t aaa", "\t`aaaaaaa`", + \ '" }}}', '', 'ccc`', '', 'grape', '', 'pineappple', '', '`bbbbbbb`', '', '`a', 'b', 'c', 'd`' + \ ], + \ spelunker#get_buffer#all() + \ ) + + call assert_equal( + \ [ + \ '" vim: foldmethod=marker', '" vim: foldcolumn=3', '" vim: foldlevel=0', '`ccccccccc', + \ 'orange peach meron', 'ccc`', 'grape', 'pineappple', '`bbbbbbb`', '`a', 'b', 'c', 'd`' + \ ], + \ spelunker#get_buffer#displayed() + \ ) + + let g:spelunker_disable_backquoted_checking = 1 +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo diff --git a/autoload/spelunker/test/test_spellbad.vim b/autoload/spelunker/test/test_spellbad.vim index 85453c4..4a7c74f 100644 --- a/autoload/spelunker/test/test_spellbad.vim +++ b/autoload/spelunker/test/test_spellbad.vim @@ -22,37 +22,37 @@ endfunction function! s:test_get_spell_bad_list() " 通常の引っかかるケース call spelunker#test#open_unit_test_buffer('spellbad', 'get_spell_bad_list1.txt') - let l:result = spelunker#spellbad#get_spell_bad_list(1, -1) - call assert_equal(['appl', 'Banan', 'Orag'], l:result) - - let l:result = spelunker#spellbad#get_spell_bad_list(2, -1) - call assert_equal(['appl', 'banan', 'orag'], l:result) - - " First Upper Case and lower case - let l:result = spelunker#spellbad#get_spell_bad_list(1, 2) + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal(['appl', 'Banan', 'Orag', 'banan', 'orag'], l:result) " Upper Case call spelunker#test#open_unit_test_buffer('spellbad', 'get_spell_bad_list2.txt') - let l:result = spelunker#spellbad#get_spell_bad_list(1, 10) + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal(['HTMLF', 'FFFCC'], l:result) " control character - call spelunker#test#open_unit_test_buffer('spellbad', 'get_spell_bad_list3.txt') - let l:result = spelunker#spellbad#get_spell_bad_list(2, -1) + call spelunker#test#open_unit_test_buffer('spellbad', 'get_spell_bad_list3_1.txt') + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal([], l:result) - let l:result = spelunker#spellbad#get_spell_bad_list(5, -1) + call spelunker#test#open_unit_test_buffer('spellbad', 'get_spell_bad_list3_2.txt') + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal(['Banan', 'Oage', 'Pach'], l:result) " char count call spelunker#test#open_unit_test_buffer('spellbad', 'get_spell_bad_list4.txt') - let l:result = spelunker#spellbad#get_spell_bad_list(1, -1) + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal(['purp', 'purpl'], l:result) " 先頭大文字のケースしかない単語 call spelunker#test#open_unit_test_buffer('spellbad', 'get_spell_bad_list5.txt') - let l:result = spelunker#spellbad#get_spell_bad_list(1, -1) + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal([], l:result) " Edge cases @@ -60,27 +60,32 @@ function! s:test_get_spell_bad_list() " # 過去に[A-Z\s]が事故ってたため " # (引っかからないケース) call spelunker#test#open_unit_test_buffer('spellbad', 'get_spell_bad_list6.txt') - let l:result = spelunker#spellbad#get_spell_bad_list(1, -1) + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal([], l:result) " set spelllang " # spelllangによる動作の違いのチェック call spelunker#test#open_unit_test_buffer('spellbad', 'get_spell_bad_list7.txt') - let l:result = spelunker#spellbad#get_spell_bad_list(1, 10) + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal([], l:result) " en_usなどの国別の設定のケース setlocal spelllang=en_us - let l:result = spelunker#spellbad#get_spell_bad_list(1, 10) + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal(['colour'], l:result) let g:spelunker_highlight_type = g:spelunker_highlight_spell_bad - let l:result = spelunker#spellbad#get_spell_bad_list(1, 10) + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal([], l:result) let g:spelunker_highlight_type = g:spelunker_highlight_all - let l:result = spelunker#spellbad#get_spell_bad_list(1, 10) + let l:window_text_list = spelunker#get_buffer#all() + let l:result = spelunker#spellbad#get_spell_bad_list(l:window_text_list) call assert_equal(['colour'], l:result) " 設定戻す diff --git a/autoload/spelunker/test/test_toggle.vim b/autoload/spelunker/test/test_toggle.vim index 3a4ac03..7b5d5d8 100644 --- a/autoload/spelunker/test/test_toggle.vim +++ b/autoload/spelunker/test/test_toggle.vim @@ -94,24 +94,25 @@ function! s:test_toggle() " register word dict test call spelunker#test#open_unit_test_buffer('toggle', 'toggle2_1.txt') + let l:filtered_buffer = spelunker#get_buffer#all() call spelunker#test#init() call cursor(1,1) - let l:line = spelunker#spellbad#get_spell_bad_list(1, -1) + let l:line = spelunker#spellbad#get_spell_bad_list([get(l:filtered_buffer, 0, '')]) 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) + let l:line = spelunker#spellbad#get_spell_bad_list([get(l:filtered_buffer, 0, '')]) call assert_equal([], l:line) call execute('spellundo! addgoodword') call spelunker#test#reload_buffer() call cursor(2,1) - let l:line = spelunker#spellbad#get_spell_bad_list(2, -1) + let l:line = spelunker#spellbad#get_spell_bad_list([get(l:filtered_buffer, 1, '')]) call assert_equal([], l:line) call assert_equal(1, spelunker#execute_with_target_word('spellwrong!')) - let l:line = spelunker#spellbad#get_spell_bad_list(2, -1) + let l:line = spelunker#spellbad#get_spell_bad_list([get(l:filtered_buffer, 1, '')]) call assert_equal(['wrong'], l:line) call execute('spellundo! wrong') " }}} @@ -243,24 +244,25 @@ function! s:test_toggle_buffer() " register word dict test call spelunker#test#open_unit_test_buffer('toggle', 'toggle2_2.txt') + let l:filtered_buffer = spelunker#get_buffer#all() call spelunker#test#init() call cursor(1,1) - let l:line = spelunker#spellbad#get_spell_bad_list(1, -1) + let l:line = spelunker#spellbad#get_spell_bad_list([get(l:filtered_buffer, 0, '')]) call assert_equal(['addcorrectedword'], l:line) call assert_equal(1, spelunker#execute_with_target_word('spellgood!')) - let l:line = spelunker#spellbad#get_spell_bad_list(1, -1) + let l:line = spelunker#spellbad#get_spell_bad_list([get(l:filtered_buffer, 0, '')]) call assert_equal([], l:line) call execute('spellundo! addcorrectedword') call spelunker#test#reload_buffer() call cursor(2,1) - let l:line = spelunker#spellbad#get_spell_bad_list(2, -1) + let l:line = spelunker#spellbad#get_spell_bad_list([get(l:filtered_buffer, 1, '')]) call assert_equal([], l:line) call assert_equal(1, spelunker#execute_with_target_word('spellwrong!')) - let l:line = spelunker#spellbad#get_spell_bad_list(2, -1) + let l:line = spelunker#spellbad#get_spell_bad_list([get(l:filtered_buffer, 1, '')]) call assert_equal(['misspelt'], l:line) call execute('spellundo! misspelt') " }}} diff --git a/autoload/spelunker/words.vim b/autoload/spelunker/words.vim index ef7f355..2ccc69b 100644 --- a/autoload/spelunker/words.vim +++ b/autoload/spelunker/words.vim @@ -139,7 +139,9 @@ endfunction function! spelunker#words#check() call spelunker#cases#reset_case_counter() - let l:spell_bad_list = spelunker#spellbad#get_spell_bad_list(1, '$') + let l:window_text_list = spelunker#get_buffer#all() + let l:spell_bad_list = spelunker#spellbad#get_spell_bad_list(l:window_text_list) + call spelunker#words#highlight(l:spell_bad_list) endfunction @@ -147,25 +149,8 @@ endfunction function! spelunker#words#check_display_area() call spelunker#cases#reset_case_counter() - let l:spell_bad_list = [] - let l:current_line = line("w0") - let l:end_line = line("w$") - - while 1 - if foldclosed(l:current_line) > 0 - let l:current_line = foldclosedend(l:current_line) + 1 - endif - - if l:current_line > l:end_line - break - endif - - " 折りたたみが無い行のみチェック - let l:tmp_spell_bad_list = spelunker#spellbad#get_spell_bad_list(l:current_line, -1) - let l:spell_bad_list = l:spell_bad_list + l:tmp_spell_bad_list - - let l:current_line = l:current_line + 1 - endwhile + let l:window_text_list = spelunker#get_buffer#displayed() + let l:spell_bad_list = spelunker#spellbad#get_spell_bad_list(l:window_text_list) " unique let l:spell_bad_list = filter(copy(l:spell_bad_list), 'index(l:spell_bad_list, v:val, v:key+1)==-1') diff --git a/plugin/spelunker.vim b/plugin/spelunker.vim index 44642b3..2aa48aa 100644 --- a/plugin/spelunker.vim +++ b/plugin/spelunker.vim @@ -42,6 +42,15 @@ if !exists('g:spelunker_disable_auto_group') let g:spelunker_disable_auto_group = 0 endif +if !exists('g:spelunker_disable_backquoted_checking') + let g:spelunker_disable_backquoted_checking = 0 +endif + +if !exists('g:spelunker_disable_uri_checking') + let g:spelunker_disable_uri_checking = 0 +endif + + let g:spelunker_check_type_buf_lead_write = 1 let g:spelunker_check_type_cursor_hold = 2 diff --git a/test/unit_test/get_buffer/disable_backquote.txt b/test/unit_test/get_buffer/disable_backquote.txt new file mode 100644 index 0000000..075ba17 --- /dev/null +++ b/test/unit_test/get_buffer/disable_backquote.txt @@ -0,0 +1,18 @@ +abc + +`aaa` def + +ghi `fff +fff` jkl + +mno` +aaa +`pqr + +`ccc +ddd +eee` + +`c +d +e` diff --git a/test/unit_test/get_buffer/disable_url.txt b/test/unit_test/get_buffer/disable_url.txt new file mode 100644 index 0000000..30ccef2 --- /dev/null +++ b/test/unit_test/get_buffer/disable_url.txt @@ -0,0 +1,7 @@ +http://github.com + +abc http://github.com def + +ghi +http://github.com +jkl diff --git a/test/unit_test/get_buffer/folded.txt b/test/unit_test/get_buffer/folded.txt new file mode 100644 index 0000000..e174f29 --- /dev/null +++ b/test/unit_test/get_buffer/folded.txt @@ -0,0 +1,28 @@ +" vim: foldmethod=marker +" vim: foldcolumn=3 +" vim: foldlevel=0 + +http://sample2.com\n + +`ccccccccc + +orange peach meron +" {{{ + banana + bannana + http://sample1.com aaa + `aaaaaaa` +" }}} + +ccc` + +grape + +pineappple + +`bbbbbbb` + +`a +b +c +d` diff --git a/test/unit_test/spellbad/get_spell_bad_list3.txt b/test/unit_test/spellbad/get_spell_bad_list3.txt index 6545bd2..d74766d 100644 --- a/test/unit_test/spellbad/get_spell_bad_list3.txt +++ b/test/unit_test/spellbad/get_spell_bad_list3.txt @@ -1,5 +1,3 @@ # 制御文字ケース(検知しない) apple\nBanana\rOrange\tPeach -# 制御文字ケース(検知する) -apple\nBanan\rOage\tPach diff --git a/test/unit_test/spellbad/get_spell_bad_list3_1.txt b/test/unit_test/spellbad/get_spell_bad_list3_1.txt new file mode 100644 index 0000000..3c73997 --- /dev/null +++ b/test/unit_test/spellbad/get_spell_bad_list3_1.txt @@ -0,0 +1,2 @@ +# 制御文字ケース(検知しない) +apple\nBanana\rOrange\tPeach diff --git a/test/unit_test/spellbad/get_spell_bad_list3_2.txt b/test/unit_test/spellbad/get_spell_bad_list3_2.txt new file mode 100644 index 0000000..8f18a57 --- /dev/null +++ b/test/unit_test/spellbad/get_spell_bad_list3_2.txt @@ -0,0 +1,3 @@ +# 制御文字ケース(検知する) +apple\nBanan\rOage\tPach +