Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to disable word checking. #36

Merged
merged 13 commits into from
Apr 19, 2020
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,18 @@ Zt

<img src="https://user-images.githubusercontent.com/7608231/65375352-6a396c80-dccf-11e9-9295-1dd061140a78.gif" width=540>

### 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.
Expand Down
88 changes: 88 additions & 0 deletions autoload/spelunker/get_buffer.vim
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion autoload/spelunker/jump.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions autoload/spelunker/spellbad.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion autoload/spelunker/test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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!'
Expand Down
104 changes: 104 additions & 0 deletions autoload/spelunker/test/test_get_buffer.vim
Original file line number Diff line number Diff line change
@@ -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
43 changes: 24 additions & 19 deletions autoload/spelunker/test/test_spellbad.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,70 @@ 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
" # #6 https://github.com/kamykn/spelunker.vim/pull/6
" # 過去に[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)

" 設定戻す
Expand Down
Loading