From 801c12a8813deb6ab20107fe8de979b86297b536 Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Wed, 23 Aug 2017 09:03:13 +0200 Subject: [PATCH 1/7] Add mix linter for elixir --- README.md | 2 +- ale_linters/elixir/mix.vim | 42 ++++++++++++++++++++++++++++++++++++++ doc/ale.txt | 2 +- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 ale_linters/elixir/mix.vim diff --git a/README.md b/README.md index f9233db3..d2769083 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ formatting. | Dafny | [dafny](https://rise4fun.com/Dafny) !! | | Dart | [dartanalyzer](https://github.com/dart-lang/sdk/tree/master/pkg/analyzer_cli) !!, [language_server](https://github.com/natebosch/dart_language_server) | | Dockerfile | [hadolint](https://github.com/hadolint/hadolint) | -| Elixir | [credo](https://github.com/rrrene/credo), [dialyxir](https://github.com/jeremyjh/dialyxir), [dogma](https://github.com/lpil/dogma) !!| +| Elixir | [credo](https://github.com/rrrene/credo), [dialyxir](https://github.com/jeremyjh/dialyxir), [dogma](https://github.com/lpil/dogma), [mix](https://hexdocs.pm/mix/Mix.html) !!| | Elm | [elm-format](https://github.com/avh4/elm-format), [elm-make](https://github.com/elm-lang/elm-make) | | Erb | [erb](https://apidock.com/ruby/ERB), [erubi](https://github.com/jeremyevans/erubi), [erubis](https://github.com/kwatch/erubis) | | Erlang | [erlc](http://erlang.org/doc/man/erlc.html), [SyntaxErl](https://github.com/ten0s/syntaxerl) | diff --git a/ale_linters/elixir/mix.vim b/ale_linters/elixir/mix.vim new file mode 100644 index 00000000..41cd18a0 --- /dev/null +++ b/ale_linters/elixir/mix.vim @@ -0,0 +1,42 @@ +" Author: evnu - https://github.com/evnu +" Author: colbydehart - https://github.com/colbydehart + +function! ale_linters#elixir#mix#Handle(buffer, lines) abort + " Matches patterns like the following: + " + " Error format + " ** (CompileError) apps/sim/lib/sim/server.ex:87: undefined function update_in/4 + " + " TODO: Warning format + " warning: variable "foobar" does not exist and is being expanded to "foobar()", please use parentheses to remove the ambiguity or change the variable name + + let l:pattern = '\v\(([^\)]+Error)\) ([^:]+):([^:]+): (.+)$' + let l:output = [] + + for l:match in ale#util#GetMatches(a:lines, l:pattern) + let l:type = 'E' + let l:text = l:match[4] + + call add(l:output, { + \ 'bufnr': a:buffer, + \ 'lnum': l:match[3] + 0, + \ 'col': 0, + \ 'type': l:type, + \ 'text': l:text, + \}) + endfor + + return l:output +endfunction + +function! ale_linters#elixir#mix#Command(buffer) abort + let l:project_dir = fnamemodify(ale#path#FindNearestFile(a:buffer, 'mix.exs'), ':h') + return 'cd ' . l:project_dir . ' && mix compile %s' +endfunction + +call ale#linter#Define('elixir', { +\ 'name': 'mix', +\ 'executable': 'mix', +\ 'command_callback': 'ale_linters#elixir#mix#Command', +\ 'callback': 'ale_linters#elixir#mix#Handle', +\}) diff --git a/doc/ale.txt b/doc/ale.txt index 0bf1c849..24a41106 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -332,7 +332,7 @@ Notes: * Dafny: `dafny`!! * Dart: `dartanalyzer`!!, `language_server` * Dockerfile: `hadolint` -* Elixir: `credo`, `dialyxir`, `dogma`!! +* Elixir: `credo`, `dialyxir`, `dogma`, `mix`!! * Elm: `elm-format, elm-make` * Erb: `erb`, `erubi`, `erubis` * Erlang: `erlc`, `SyntaxErl` From d760558007631294673171451f72fc4c2f5a89a7 Mon Sep 17 00:00:00 2001 From: Colby Dehart Date: Fri, 1 Jun 2018 11:56:47 -0400 Subject: [PATCH 2/7] added mix build path env var to the mix compile --- ale_linters/elixir/mix.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ale_linters/elixir/mix.vim b/ale_linters/elixir/mix.vim index 41cd18a0..d31dbb28 100644 --- a/ale_linters/elixir/mix.vim +++ b/ale_linters/elixir/mix.vim @@ -31,7 +31,7 @@ endfunction function! ale_linters#elixir#mix#Command(buffer) abort let l:project_dir = fnamemodify(ale#path#FindNearestFile(a:buffer, 'mix.exs'), ':h') - return 'cd ' . l:project_dir . ' && mix compile %s' + return 'cd ' . l:project_dir . ' && MIX_BUILD_PATH=/tmp/mix mix compile %s' endfunction call ale#linter#Define('elixir', { From 81739be0a04956458471f196874d4206b21e60fb Mon Sep 17 00:00:00 2001 From: Colby Dehart Date: Sat, 2 Jun 2018 13:03:56 -0400 Subject: [PATCH 3/7] handled temp file and env variable correctly; added tests --- ale_linters/elixir/mix.vim | 18 ++++++++++++++---- test/handler/test_mix_handler.vader | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 test/handler/test_mix_handler.vader diff --git a/ale_linters/elixir/mix.vim b/ale_linters/elixir/mix.vim index d31dbb28..c4beb42d 100644 --- a/ale_linters/elixir/mix.vim +++ b/ale_linters/elixir/mix.vim @@ -1,5 +1,6 @@ " Author: evnu - https://github.com/evnu " Author: colbydehart - https://github.com/colbydehart +" Description: Mix compile checking for Elixir files function! ale_linters#elixir#mix#Handle(buffer, lines) abort " Matches patterns like the following: @@ -29,14 +30,23 @@ function! ale_linters#elixir#mix#Handle(buffer, lines) abort return l:output endfunction -function! ale_linters#elixir#mix#Command(buffer) abort - let l:project_dir = fnamemodify(ale#path#FindNearestFile(a:buffer, 'mix.exs'), ':h') - return 'cd ' . l:project_dir . ' && MIX_BUILD_PATH=/tmp/mix mix compile %s' +function! ale_linters#elixir#mix#GetCommand(buffer) abort + let l:project_root = fnamemodify(ale#path#FindNearestFile(a:buffer, 'mix.exs'), ':h') + + let l:temp_dir = ale#engine#CreateDirectory(a:buffer) + + let l:mix_build_path = has('win32') + \ ? 'set MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' && ' + \ : 'MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' ' + + return ale#path#CdString(l:project_root) + \ . l:mix_build_path + \ . ' mix compile %s' endfunction call ale#linter#Define('elixir', { \ 'name': 'mix', \ 'executable': 'mix', -\ 'command_callback': 'ale_linters#elixir#mix#Command', +\ 'command_callback': 'ale_linters#elixir#mix#GetCommand', \ 'callback': 'ale_linters#elixir#mix#Handle', \}) diff --git a/test/handler/test_mix_handler.vader b/test/handler/test_mix_handler.vader new file mode 100644 index 00000000..a5549b5d --- /dev/null +++ b/test/handler/test_mix_handler.vader @@ -0,0 +1,21 @@ +Before: + runtime ale_linters/elixir/mix.vim + +After: + call ale#linter#Reset() + +Execute(The mix handler should parse lines correctly): + + AssertEqual + \ [ + \ { + \ 'bufnr': 347, + \ 'lnum': 87, + \ 'col': 0, + \ 'text': 'undefined function update_in/4', + \ 'type': 'E', + \ }, + \ ], + \ ale_linters#elixir#mix#Handle(347, [ + \ '** (CompileError) apps/sim/lib/sim/server.ex:87: undefined function update_in/4' + \ ]) From f0f569f14a18459dcd99a381c5776f7464dfb125 Mon Sep 17 00:00:00 2001 From: Colby Dehart Date: Wed, 6 Jun 2018 22:58:32 -0500 Subject: [PATCH 4/7] added test for command callback --- ale_linters/elixir/mix.vim | 11 +++++- .../test_elixir_mix_command_callbacks.vader | 36 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/command_callback/test_elixir_mix_command_callbacks.vader diff --git a/ale_linters/elixir/mix.vim b/ale_linters/elixir/mix.vim index c4beb42d..25ee8fb8 100644 --- a/ale_linters/elixir/mix.vim +++ b/ale_linters/elixir/mix.vim @@ -30,8 +30,16 @@ function! ale_linters#elixir#mix#Handle(buffer, lines) abort return l:output endfunction +function! ale_linters#elixir#mix#FindProjectRoot(buffer) abort + let l:project_root = ale#path#FindNearestFile(a:buffer, 'mix.exs') + if !empty(l:project_root) + return fnamemodify(l:project_root, ':h') + endif + return '' +endfunction + function! ale_linters#elixir#mix#GetCommand(buffer) abort - let l:project_root = fnamemodify(ale#path#FindNearestFile(a:buffer, 'mix.exs'), ':h') + let l:project_root = ale_linters#elixir#mix#FindProjectRoot(a:buffer) let l:temp_dir = ale#engine#CreateDirectory(a:buffer) @@ -49,4 +57,5 @@ call ale#linter#Define('elixir', { \ 'executable': 'mix', \ 'command_callback': 'ale_linters#elixir#mix#GetCommand', \ 'callback': 'ale_linters#elixir#mix#Handle', +\ 'lint_file': 1, \}) diff --git a/test/command_callback/test_elixir_mix_command_callbacks.vader b/test/command_callback/test_elixir_mix_command_callbacks.vader new file mode 100644 index 00000000..28c09e33 --- /dev/null +++ b/test/command_callback/test_elixir_mix_command_callbacks.vader @@ -0,0 +1,36 @@ +Before: + runtime ale_linters/elixir/mix.vim + + call ale#test#SetDirectory('/testplugin/test/command_callback') + + let g:env_prefix = has('win32') + \ ? 'set MIX_BUILD_PATH=TEMP && ' + \ : 'MIX_BUILD_PATH=TEMP ' + + + function! GetCommand() abort + let l:command = ale_linters#elixir#mix#GetCommand(bufnr('')) + + let l:split_command = split(l:command, 'MIX_BUILD_PATH=[^ ]*\s') + + return l:split_command[0] . 'MIX_BUILD_PATH=TEMP' . l:split_command[1] + endfunction + + + +After: + Restore + + unlet! g:env_prefix + + call ale#linter#Reset() + call ale#test#RestoreDirectory() + + delfunction GetCommand + +Execute(The default mix command should be correct): + AssertEqual + \ GetCommand(), + \ 'cd '''' && ' + \ . g:env_prefix + \ . 'mix compile %s' From 864818a38528e91363fab3561c6359e272b35929 Mon Sep 17 00:00:00 2001 From: Colby Dehart Date: Thu, 7 Jun 2018 11:47:57 -0500 Subject: [PATCH 5/7] WIP cd to project path --- ale_linters/elixir/mix.vim | 10 +++++----- .../mix_paths/wrapped_project/mix.exs | 1 + .../test_elixir_mix_command_callbacks.vader | 13 +++++++++---- 3 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 test/command_callback/mix_paths/wrapped_project/mix.exs diff --git a/ale_linters/elixir/mix.vim b/ale_linters/elixir/mix.vim index 25ee8fb8..3af99ab4 100644 --- a/ale_linters/elixir/mix.vim +++ b/ale_linters/elixir/mix.vim @@ -31,11 +31,11 @@ function! ale_linters#elixir#mix#Handle(buffer, lines) abort endfunction function! ale_linters#elixir#mix#FindProjectRoot(buffer) abort - let l:project_root = ale#path#FindNearestFile(a:buffer, 'mix.exs') - if !empty(l:project_root) - return fnamemodify(l:project_root, ':h') + let l:mix_file = ale#path#FindNearestFile(a:buffer, 'mix.exs') + if !empty(l:mix_file) + return fnamemodify(l:mix_file, ':p:h') endif - return '' + return '.' endfunction function! ale_linters#elixir#mix#GetCommand(buffer) abort @@ -47,7 +47,7 @@ function! ale_linters#elixir#mix#GetCommand(buffer) abort \ ? 'set MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' && ' \ : 'MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' ' - return ale#path#CdString(l:project_root) + return ale#path#CdString(l:project_root) \ . l:mix_build_path \ . ' mix compile %s' endfunction diff --git a/test/command_callback/mix_paths/wrapped_project/mix.exs b/test/command_callback/mix_paths/wrapped_project/mix.exs new file mode 100644 index 00000000..d2d855e6 --- /dev/null +++ b/test/command_callback/mix_paths/wrapped_project/mix.exs @@ -0,0 +1 @@ +use Mix.Config diff --git a/test/command_callback/test_elixir_mix_command_callbacks.vader b/test/command_callback/test_elixir_mix_command_callbacks.vader index 28c09e33..22d35eeb 100644 --- a/test/command_callback/test_elixir_mix_command_callbacks.vader +++ b/test/command_callback/test_elixir_mix_command_callbacks.vader @@ -3,13 +3,15 @@ Before: call ale#test#SetDirectory('/testplugin/test/command_callback') + let g:project_root = ale#path#Simplify(g:dir . '/mix_paths/wrapped_project') + let g:env_prefix = has('win32') \ ? 'set MIX_BUILD_PATH=TEMP && ' \ : 'MIX_BUILD_PATH=TEMP ' - function! GetCommand() abort - let l:command = ale_linters#elixir#mix#GetCommand(bufnr('')) + function! GetCommand(buffer) abort + let l:command = ale_linters#elixir#mix#GetCommand(a:buffer) let l:split_command = split(l:command, 'MIX_BUILD_PATH=[^ ]*\s') @@ -22,6 +24,7 @@ After: Restore unlet! g:env_prefix + unlet! g:project_root call ale#linter#Reset() call ale#test#RestoreDirectory() @@ -29,8 +32,10 @@ After: delfunction GetCommand Execute(The default mix command should be correct): + call ale#test#SetFilename('mix_paths/wrapped_project/lib/app.ex') + AssertEqual - \ GetCommand(), - \ 'cd '''' && ' + \ GetCommand(bufnr('')), + \ ale#path#CdString(g:project_root) \ . g:env_prefix \ . 'mix compile %s' From d7efb13203a1ebec24c963ff2f7c9dc99a4ea2a8 Mon Sep 17 00:00:00 2001 From: w0rp Date: Wed, 20 Jun 2018 22:41:19 +0100 Subject: [PATCH 6/7] Try to fix the tests on Windows --- .../test_elixir_mix_command_callbacks.vader | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/command_callback/test_elixir_mix_command_callbacks.vader b/test/command_callback/test_elixir_mix_command_callbacks.vader index 22d35eeb..67785881 100644 --- a/test/command_callback/test_elixir_mix_command_callbacks.vader +++ b/test/command_callback/test_elixir_mix_command_callbacks.vader @@ -13,13 +13,9 @@ Before: function! GetCommand(buffer) abort let l:command = ale_linters#elixir#mix#GetCommand(a:buffer) - let l:split_command = split(l:command, 'MIX_BUILD_PATH=[^ ]*\s') - - return l:split_command[0] . 'MIX_BUILD_PATH=TEMP' . l:split_command[1] + return substitute(l:command, 'MIX_BUILD_PATH=[^ ]\+', 'MIX_BUILD_PATH=TEMP', '') endfunction - - After: Restore From b8be25adb408bffc7af9f2f92c8c5cc7a6813601 Mon Sep 17 00:00:00 2001 From: w0rp Date: Wed, 20 Jun 2018 22:44:56 +0100 Subject: [PATCH 7/7] Remove redundant spaces. --- ale_linters/elixir/mix.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ale_linters/elixir/mix.vim b/ale_linters/elixir/mix.vim index 3af99ab4..1a95e37f 100644 --- a/ale_linters/elixir/mix.vim +++ b/ale_linters/elixir/mix.vim @@ -44,8 +44,8 @@ function! ale_linters#elixir#mix#GetCommand(buffer) abort let l:temp_dir = ale#engine#CreateDirectory(a:buffer) let l:mix_build_path = has('win32') - \ ? 'set MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' && ' - \ : 'MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' ' + \ ? 'set MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) . ' &&' + \ : 'MIX_BUILD_PATH=' . ale#Escape(l:temp_dir) return ale#path#CdString(l:project_root) \ . l:mix_build_path