From 9820899b9e09dba27a1fc0867fde3012769a01e7 Mon Sep 17 00:00:00 2001 From: w0rp Date: Fri, 20 Jan 2017 17:30:23 +0000 Subject: [PATCH] Improve mypy handling a little bit more --- ale_linters/python/mypy.vim | 51 +++++++++++++++++-- autoload/ale/util.vim | 14 +++++ test/test_find_nearest_directory.vader | 15 ++++++ test/test_mypy_handler.vader | 22 ++++++++ test/test_resolve_local_path.vader | 2 +- .../empty-file | 0 6 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 test/test_find_nearest_directory.vader create mode 100644 test/test_mypy_handler.vader create mode 100644 test/top/ale-special-directory-name-dont-use-this-please/empty-file diff --git a/ale_linters/python/mypy.vim b/ale_linters/python/mypy.vim index a7461443..934961b1 100644 --- a/ale_linters/python/mypy.vim +++ b/ale_linters/python/mypy.vim @@ -1,17 +1,62 @@ -" Author: Keith Smiley +" Author: Keith Smiley , w0rp " Description: mypy support for optional python typechecking let g:ale_python_mypy_options = get(g:, 'ale_python_mypy_options', '') function! g:ale_linters#python#mypy#GetCommand(buffer) abort - return g:ale#util#stdin_wrapper + let l:automatic_stubs_dir = ale#util#FindNearestDirectory(a:buffer, 'stubs') + " TODO: Add Windows support + let l:automatic_stubs_command = (has('unix') && !empty(l:automatic_stubs_dir)) + \ ? 'MYPYPATH=' . l:automatic_stubs_dir . ' ' + \ : '' + + return l:automatic_stubs_command + \ . g:ale#util#stdin_wrapper \ . ' .py mypy --show-column-numbers ' \ . g:ale_python_mypy_options endfunction +function! g:ale_linters#python#mypy#Handle(buffer, lines) abort + " Look for lines like the following: + " + " file.py:4: error: No library stub file for module 'django.db' + " + " Lines like these should be ignored below: + " + " file.py:4: note: (Stub files are from https://github.com/python/typeshed) + let l:pattern = '^.\+:\(\d\+\):\?\(\d\+\)\?: \([^:]\+\): \(.\+\)$' + let l:output = [] + + for l:line in a:lines + let l:match = matchlist(l:line, l:pattern) + + if len(l:match) == 0 + continue + endif + + if l:match[4] =~# 'Stub files are from' + " The lines telling us where to get stub files from make it so + " we can't read the actual errors, so exclude them. + continue + endif + + call add(l:output, { + \ 'bufnr': a:buffer, + \ 'lnum': l:match[1] + 0, + \ 'vcol': 0, + \ 'col': l:match[2] + 0, + \ 'text': l:match[4], + \ 'type': l:match[3] =~# 'error' ? 'E' : 'W', + \ 'nr': -1, + \}) + endfor + + return l:output +endfunction + call g:ale#linter#Define('python', { \ 'name': 'mypy', \ 'executable': 'mypy', \ 'command_callback': 'ale_linters#python#mypy#GetCommand', -\ 'callback': 'ale#handlers#HandleGCCFormat', +\ 'callback': 'ale_linters#python#mypy#Handle', \}) diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim index d40f7bf6..27517ef7 100644 --- a/autoload/ale/util.vim +++ b/autoload/ale/util.vim @@ -44,6 +44,20 @@ function! ale#util#FindNearestFile(buffer, filename) abort return '' endfunction +" Given a buffer and a directory name, find the nearest directory by searching upwards +" through the paths relative to the given buffer. +function! ale#util#FindNearestDirectory(buffer, directory_name) abort + let l:buffer_filename = fnamemodify(bufname(a:buffer), ':p') + + let l:relative_path = finddir(a:directory_name, l:buffer_filename . ';') + + if !empty(l:relative_path) + return fnamemodify(l:relative_path, ':p') + endif + + return '' +endfunction + " Given a buffer, a string to search for, an a global fallback for when " the search fails, look for a file in parent paths, and if that fails, " use the global fallback path instead. diff --git a/test/test_find_nearest_directory.vader b/test/test_find_nearest_directory.vader new file mode 100644 index 00000000..450826ed --- /dev/null +++ b/test/test_find_nearest_directory.vader @@ -0,0 +1,15 @@ +Execute(Open a file some directory down): + silent! cd /testplugin/test + :e! top/middle/bottom/dummy.txt + +Then(We should be able to find the right directory): + AssertEqual + \ expand('%:p:h:h:h:h') . '/top/ale-special-directory-name-dont-use-this-please/', + \ ale#util#FindNearestDirectory(bufnr('%'), 'ale-special-directory-name-dont-use-this-please') + +Execute(Do nothing): + +Then(We shouldn't find anything for files which don't match): + AssertEqual + \ '', + \ ale#util#FindNearestDirectory(bufnr('%'), 'ale-this-should-never-match-anything') diff --git a/test/test_mypy_handler.vader b/test/test_mypy_handler.vader new file mode 100644 index 00000000..5fe17f75 --- /dev/null +++ b/test/test_mypy_handler.vader @@ -0,0 +1,22 @@ +Execute(The mypy handler should parse lines correctly): + runtime ale_linters/python/mypy.vim + + AssertEqual + \ [ + \ { + \ 'bufnr': 347, + \ 'lnum': 4, + \ 'vcol': 0, + \ 'col': 0, + \ 'text': "No library stub file for module 'django.db'", + \ 'type': 'E', + \ 'nr': -1, + \ }, + \ ], + \ ale_linters#python#mypy#Handle(347, [ + \ "file.py:4: error: No library stub file for module 'django.db'", + \ 'file.py:4: note: (Stub files are from https://github.com/python/typeshed)', + \ ]) + +After: + call ale#linter#Reset() diff --git a/test/test_resolve_local_path.vader b/test/test_resolve_local_path.vader index bdbac01b..e269221e 100644 --- a/test/test_resolve_local_path.vader +++ b/test/test_resolve_local_path.vader @@ -2,7 +2,7 @@ Execute(Open a file some directory down): silent! cd /testplugin/test :e! top/middle/bottom/dummy.txt -Then(We should be able to to find the local version of a file): +Then(We should be able to find the local version of a file): AssertEqual \ expand('%:p:h:h:h:h') . '/top/example.ini', \ ale#util#ResolveLocalPath(bufnr('%'), 'example.ini', '/global/config.ini') diff --git a/test/top/ale-special-directory-name-dont-use-this-please/empty-file b/test/top/ale-special-directory-name-dont-use-this-please/empty-file new file mode 100644 index 00000000..e69de29b