Skip to content

Commit

Permalink
Merge branch 'issue/3' which fixes issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Aug 13, 2014
2 parents 2b467b1 + e906e7e commit f03dd95
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ You can use some mappings in the information window, which is opened to show the
| `q` | Quit the info window |
| `<CR>` | Move to the location of the error |
| `f` | Fix the error __automatically__ |
| `r` | Remove the error from the checked buffer |
| `r` | Remove the error without fix |
| `R` | Disable the grammar rule in the checked buffer |
| `?` | Show help of the mapping in info window |

Expand All @@ -44,6 +44,10 @@ You can use some mappings in the information window, which is opened to show the
| `<Plug>(grammarous-remove-error)` | Remove the error under the cursor |
| `<Plug>(grammarous-disable-rule)` | Disable the grammar rule under the cursor |

### `grammarous` unite.vim source

If you are [unite.vim](https://github.com/Shougo/unite.vim) user, `grammarous` unite source is available to look and search the error list incrementally.
To the candidates of the list, you can do the actions which are the same as ones in the info window. (`fixit`, `remove error` and `disable rule`)

## Fix examples

Expand All @@ -58,9 +62,9 @@ This plugin attempts to install [LanguageTool](https://www.languagetool.org/) us

## Requirements

- Java7 (jdk-1.7, jre-1.7, ...)
- [vimproc.vim](https://github.com/Shougo/vimproc.vim) (It will be optional)

- Java7 (required)
- [vimproc.vim](https://github.com/Shougo/vimproc.vim) (It will be optional but now required)
- [unite.vim](https://github.com/Shougo/unite.vim) (optional)

## Contribution

Expand Down
2 changes: 1 addition & 1 deletion autoload/grammarous.vim
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ function! grammarous#get_error_at(pos, errs)
return s:binary_search_by_pos(a:errs, a:pos, 0, len(a:errs)-1)
endfunction

function! grammarous#fixit(err, ...)
function! grammarous#fixit(err)
if empty(a:err) || !grammarous#move_to_checked_buf(a:err.fromy+1, a:err.fromx+1)
return
endif
Expand Down
2 changes: 1 addition & 1 deletion autoload/grammarous/info_win.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function! grammarous#info_win#action_help()
\ "| q | Quit the info window |",
\ "| <CR> | Move to the location of the error |",
\ "| f | Fix the error automatically |",
\ "| r | Remove the error from the checked buffer |",
\ "| r | Remove the error without fix |",
\ "| R | Disable the grammar rule in the checked buffer |",
\ ], "\n")
endfunction
Expand Down
68 changes: 60 additions & 8 deletions autoload/unite/sources/grammarous.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let s:source = {
\ 'default_kind' : 'jump_list',
\ 'default_action' : 'open',
\ 'hooks' : {},
\ 'action_table' : {},
\ 'syntax' : 'uniteSource__Grammarous',
\ }

Expand All @@ -15,33 +16,84 @@ function! unite#sources#grammarous#define()
endfunction

function! s:source.hooks.on_init(args, context)
let s:bufnr = bufnr('%')
let s:errs = get(b:, 'grammarous_result', [])
endfunction
if exists('b:unite') && has_key(b:unite, 'prev_bufnr')
let a:context.source__checked_bufnr = b:unite.prev_bufnr
else
let a:context.source__checked_bufnr = bufnr('%')
endif
let a:context.source__checked_bufnr
\ = getbufvar(
\ a:context.source__checked_bufnr,
\ 'grammarous_preview_original_bufnr',
\ a:context.source__checked_bufnr
\ )
if type(getbufvar(a:context.source__checked_bufnr, 'grammarous_result', 0)) == type(0)
let should_check_current_buf = a:context.source__checked_bufnr == bufnr('%')
if should_check_current_buf
execute 'GrammarousCheck' join(a:args, ' ')
else
let w = bufwinnr(a:context.source__checked_bufnr)
execute w . 'wincmd w'
execute 'GrammarousCheck' join(a:args, ' ')
wincmd p
endif
endif

function! s:source.hooks.on_close(args, context)
unlet! s:errs
call grammarous#info_win#close()
endfunction

function! s:source.hooks.on_syntax(args, context)
syntax match uniteSource__GrammarousKeyword "\%(Context\|Correct\):" contained containedin=uniteSource__Grammarous
syntax keyword uniteSource__GrammarousError Error contained containedin=uniteSource__Grammarous
highlight default link uniteSource__GrammarousKeyword Keyword
highlight default link uniteSource__GrammarousError ErrorMsg
for err in s:errs
for err in getbufvar(a:context.source__checked_bufnr, 'grammarous_result', [])
call matchadd('GrammarousError', grammarous#generate_highlight_pattern(err), 999)
endfor
endfunction

function! s:source.change_candidates(args, context)
return map(copy(s:errs), '{
return map(copy(getbufvar(a:context.source__checked_bufnr, 'grammarous_result', [])), '{
\ "word" : printf("Error: %s\nContext: %s\nCorrect: %s", v:val.msg, v:val.context, split(v:val.replacements, "#")[0]),
\ "action__buffer_nr" : s:bufnr,
\ "action__buffer_nr" : a:context.source__checked_bufnr,
\ "action__line" : str2nr(v:val.fromy)+1,
\ "action__col" : str2nr(v:val.fromx)+1,
\ "action__grammar_error" : v:val,
\ "is_multiline" : 1,
\}')
endfunction

function! s:prepare_bufvar(c)
let b:grammarous_preview_error = a:c.action__grammar_error
let b:grammarous_preview_original_bufnr = a:c.action__buffer_nr
endfunction

let s:source.action_table.fixit = {
\ 'description' : 'Fix the error automatically',
\ }

function! s:source.action_table.fixit.func(candidate)
call s:prepare_bufvar(a:candidate)
call grammarous#info_win#action_fixit()
endfunction

let s:source.action_table.remove_error = {
\ 'description' : 'Remove the error without fix'
\ }

function! s:source.action_table.remove_error.func(candidate)
call s:prepare_bufvar(a:candidate)
call grammarous#info_win#action_remove_error()
endfunction

let s:source.action_table.disable_rule = {
\ 'description' : 'Disable the grammar rule in the checked buffer'
\ }

function! s:source.action_table.disable_rule.func(candidate)
call s:prepare_bufvar(a:candidate)
call grammarous#info_win#action_disable_rule()
endfunction

let &cpo = s:save_cpo
unlet s:save_cpo

0 comments on commit f03dd95

Please sign in to comment.