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

Improve pyrex cython linter. #1675

Merged
merged 3 commits into from Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 37 additions & 4 deletions ale_linters/pyrex/cython.vim
@@ -1,10 +1,43 @@
" Author: w0rp <devw0rp@gmail.com>
" Author: w0rp <devw0rp@gmail.com>,
" Nicolas Pauss <https://github.com/nicopauss>
" Description: cython syntax checking for cython files.

call ale#Set('pyrex_cython_executable', 'cython')
call ale#Set('pyrex_cython_options', '--warning-extra')

function! ale_linters#pyrex#cython#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'pyrex_cython_executable')
endfunction

function! ale_linters#pyrex#cython#GetCommand(buffer) abort
let l:local_dir = ale#Escape(fnamemodify(bufname(a:buffer), ':p:h'))

return ale#Escape(ale_linters#pyrex#cython#GetExecutable(a:buffer))
\ . ' --working ' . l:local_dir . ' --include-dir ' . l:local_dir
\ . ' ' . ale#Var(a:buffer, 'pyrex_cython_options')
\ . ' --output-file ' . g:ale#util#nul_file . ' %t'
endfunction

function! ale_linters#pyrex#cython#Handle(buffer, lines) abort
let l:pattern = '\v^(\w+: )?[^:]+:(\d+):?(\d+)?:? ?(.+)$'
let l:output = []

for l:match in ale#util#GetMatches(a:lines, l:pattern)
call add(l:output, {
\ 'lnum': l:match[2] + 0,
\ 'col': l:match[3] + 0,
\ 'text': l:match[4],
\ 'type': l:match[1][0] is# 'w' ? 'W' : 'E',
\})
endfor

return l:output
endfunction

call ale#linter#Define('pyrex', {
\ 'name': 'cython',
\ 'output_stream': 'stderr',
\ 'executable': 'cython',
\ 'command': 'cython --warning-extra -o ' . g:ale#util#nul_file . ' %t',
\ 'callback': 'ale#handlers#unix#HandleAsError',
\ 'executable_callback': 'ale_linters#pyrex#cython#GetExecutable',
\ 'command_callback': 'ale_linters#pyrex#cython#GetCommand',
\ 'callback': 'ale_linters#pyrex#cython#Handle',
\})
25 changes: 25 additions & 0 deletions doc/ale-pyrex.txt
@@ -0,0 +1,25 @@
===============================================================================
ALE Pyrex (Cython) Integration *ale-pyrex-options*


===============================================================================
cython *ale-pyrex-cython*

g:ale_pyrex_cython_executable *g:ale_pyrex_cython_executable*
*b:ale_pyrex_cython_executable*
Type: |String|
Default: `'cython'`

This variable can be changed to use a different executable for cython.


g:ale_pyrex_cython_options *g:ale_pyrex_cython_options*
*b:ale_pyrex_cython_options*
Type: |String|
Default: `'--warning-extra --warning-errors'`

This variable can be changed to modify flags given to cython.


===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
2 changes: 2 additions & 0 deletions doc/ale.txt
Expand Up @@ -185,6 +185,8 @@ CONTENTS *ale-contents*
puglint.............................|ale-pug-puglint|
puppet................................|ale-puppet-options|
puppetlint..........................|ale-puppet-puppetlint|
pyrex (cython)........................|ale-pyrex-options|
cython..............................|ale-pyrex-cython|
python................................|ale-python-options|
autopep8............................|ale-python-autopep8|
black...............................|ale-python-black|
Expand Down
50 changes: 50 additions & 0 deletions test/command_callback/test_pyrex_cython_command_callback.vader
@@ -0,0 +1,50 @@
Before:
Save g:ale_pyrex_cython_executable
Save g:ale_pyrex_cython_options

unlet! g:ale_pyrex_cython_executable
unlet! b:ale_pyrex_cython_executable
unlet! g:ale_pyrex_cython_options
unlet! b:ale_pyrex_cython_options

runtime ale_linters/pyrex/cython.vim

call ale#test#SetDirectory('/testplugin/test/command_callback')

After:
Restore
unlet! b:ale_pyrex_cython_options
unlet! b:ale_pyrex_cython_executable
call ale#linter#Reset()
call ale#test#RestoreDirectory()

Execute(The default cython command should be correct):
AssertEqual
\ ale#Escape('cython')
\ . ' --working ' . ale#Escape(g:dir)
\ . ' --include-dir ' . ale#Escape(g:dir)
\ . ' --warning-extra'
\ . ' --output-file ' . g:ale#util#nul_file . ' %t',
\ ale_linters#pyrex#cython#GetCommand(bufnr(''))

Execute(The cython executable should be configurable):
let b:ale_pyrex_cython_executable = 'cython_foobar'

AssertEqual
\ ale#Escape('cython_foobar')
\ . ' --working ' . ale#Escape(g:dir)
\ . ' --include-dir ' . ale#Escape(g:dir)
\ . ' --warning-extra'
\ . ' --output-file ' . g:ale#util#nul_file . ' %t',
\ ale_linters#pyrex#cython#GetCommand(bufnr(''))

Execute(Additional cython options should be configurable):
let b:ale_pyrex_cython_options = '--foobar'

AssertEqual
\ ale#Escape('cython')
\ . ' --working ' . ale#Escape(g:dir)
\ . ' --include-dir ' . ale#Escape(g:dir)
\ . ' --foobar'
\ . ' --output-file ' . g:ale#util#nul_file . ' %t',
\ ale_linters#pyrex#cython#GetCommand(bufnr(''))
26 changes: 26 additions & 0 deletions test/handler/test_pyrex_cython_handler.vader
@@ -0,0 +1,26 @@
Before:
runtime ale_linters/pyrex/cython.vim

After:
call ale#linter#Reset()

Execute(The cython handler should handle warnings and errors):
AssertEqual
\ [
\ {
\ 'lnum': 42,
\ 'col': 7,
\ 'text': 'some warning',
\ 'type': 'W',
\ },
\ {
\ 'lnum': 777,
\ 'col': 21,
\ 'text': 'some error',
\ 'type': 'E',
\ },
\ ],
\ ale_linters#pyrex#cython#Handle(347, [
\ 'warning: file:42:7: some warning',
\ 'file:777:21: some error',
\ ])