From 912f632bf591377a69bf688f6a85668d93be8841 Mon Sep 17 00:00:00 2001 From: Nils Leuzinger Date: Thu, 5 Apr 2018 21:04:11 +0200 Subject: [PATCH] Add fsc linter for Scala (#1452) * Add fsc as a Scala linter * Pull reused code into `autoload/ale/` directory * Include fsc into the README * Add unit test for testing the scala handler * Add unit test for scala's fsc linter * Rename scala unit tests for clarity * Fix typo in README * Fix typos in doc/ale.txt * Fix author headline * Put methods for fsc commands back into fsc.vim * Move command_callback tests to correct location * Rewrite handler test so it actually tests handler * Clarify description of test in test_scala_handler --- README.md | 2 +- ale_linters/scala/fsc.vim | 29 ++++++++++++++ ale_linters/scala/scalac.vim | 39 +------------------ autoload/ale/handlers/scala.vim | 37 ++++++++++++++++++ doc/ale.txt | 4 +- .../test_fsc_command_callback.vader | 17 ++++++++ .../test_scalac_command_callback.vader} | 1 - test/handler/test_scala_handler.vader | 32 +++++++++++++++ 8 files changed, 120 insertions(+), 41 deletions(-) create mode 100644 ale_linters/scala/fsc.vim create mode 100644 autoload/ale/handlers/scala.vim create mode 100644 test/command_callback/test_fsc_command_callback.vader rename test/{handler/test_scalac_handler.vader => command_callback/test_scalac_command_callback.vader} (99%) create mode 100644 test/handler/test_scala_handler.vader diff --git a/README.md b/README.md index 926c209a..b5dd0e9d 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ formatting. | Rust | cargo !! (see `:help ale-integration-rust` for configuration instructions), [rls](https://github.com/rust-lang-nursery/rls), [rustc](https://www.rust-lang.org/), [rustfmt](https://github.com/rust-lang-nursery/rustfmt) | | SASS | [sass-lint](https://www.npmjs.com/package/sass-lint), [stylelint](https://github.com/stylelint/stylelint) | | SCSS | [prettier](https://github.com/prettier/prettier), [sass-lint](https://www.npmjs.com/package/sass-lint), [scss-lint](https://github.com/brigade/scss-lint), [stylelint](https://github.com/stylelint/stylelint) | -| Scala | [scalac](http://scala-lang.org), [scalastyle](http://www.scalastyle.org) | +| Scala | [fsc](https://www.scala-lang.org/old/sites/default/files/linuxsoft_archives/docu/files/tools/fsc.html), [scalac](http://scala-lang.org), [scalastyle](http://www.scalastyle.org) | | Slim | [slim-lint](https://github.com/sds/slim-lint) | | SML | [smlnj](http://www.smlnj.org/) | | Solidity | [solhint](https://github.com/protofire/solhint), [solium](https://github.com/duaraghav8/Solium) | diff --git a/ale_linters/scala/fsc.vim b/ale_linters/scala/fsc.vim new file mode 100644 index 00000000..17b26f0b --- /dev/null +++ b/ale_linters/scala/fsc.vim @@ -0,0 +1,29 @@ +" Author: Nils Leuzinger - https://github.com/PawkyPenguin +" Description: Basic scala support using fsc +" +function! ale_linters#scala#fsc#GetExecutable(buffer) abort + if index(split(getbufvar(a:buffer, '&filetype'), '\.'), 'sbt') >= 0 + " Don't check sbt files + return '' + endif + + return 'fsc' +endfunction + +function! ale_linters#scala#fsc#GetCommand(buffer) abort + let l:executable = ale_linters#scala#fsc#GetExecutable(a:buffer) + + if empty(l:executable) + return '' + endif + + return ale#Escape(l:executable) . ' -Ystop-after:parser %t' +endfunction + +call ale#linter#Define('scala', { +\ 'name': 'fsc', +\ 'executable_callback': 'ale_linters#scala#fsc#GetExecutable', +\ 'command_callback': 'ale_linters#scala#fsc#GetCommand', +\ 'callback': 'ale#handlers#scala#HandleScalacLintFormat', +\ 'output_stream': 'stderr', +\}) diff --git a/ale_linters/scala/scalac.vim b/ale_linters/scala/scalac.vim index 584aee74..551284af 100644 --- a/ale_linters/scala/scalac.vim +++ b/ale_linters/scala/scalac.vim @@ -4,7 +4,7 @@ function! ale_linters#scala#scalac#GetExecutable(buffer) abort if index(split(getbufvar(a:buffer, '&filetype'), '\.'), 'sbt') >= 0 - " Don't check sbt files with scalac. + " Don't check sbt files return '' endif @@ -21,45 +21,10 @@ function! ale_linters#scala#scalac#GetCommand(buffer) abort return ale#Escape(l:executable) . ' -Ystop-after:parser %t' endfunction -function! ale_linters#scala#scalac#Handle(buffer, lines) abort - " Matches patterns line the following: - " - " /var/folders/5q/20rgxx3x1s34g3m14n5bq0x80000gn/T/vv6pSsy/0:26: error: expected class or object definition - let l:pattern = '^.\+:\(\d\+\): \(\w\+\): \(.\+\)' - let l:output = [] - let l:ln = 0 - - for l:line in a:lines - let l:ln = l:ln + 1 - let l:match = matchlist(l:line, l:pattern) - - if len(l:match) == 0 - continue - endif - - let l:text = l:match[3] - let l:type = l:match[2] is# 'error' ? 'E' : 'W' - let l:col = 0 - - if l:ln + 1 < len(a:lines) - let l:col = stridx(a:lines[l:ln + 1], '^') - endif - - call add(l:output, { - \ 'lnum': l:match[1] + 0, - \ 'col': l:col + 1, - \ 'text': l:text, - \ 'type': l:type, - \}) - endfor - - return l:output -endfunction - call ale#linter#Define('scala', { \ 'name': 'scalac', \ 'executable_callback': 'ale_linters#scala#scalac#GetExecutable', \ 'command_callback': 'ale_linters#scala#scalac#GetCommand', -\ 'callback': 'ale_linters#scala#scalac#Handle', +\ 'callback': 'ale#handlers#scala#HandleScalacLintFormat', \ 'output_stream': 'stderr', \}) diff --git a/autoload/ale/handlers/scala.vim b/autoload/ale/handlers/scala.vim new file mode 100644 index 00000000..3fe7a12e --- /dev/null +++ b/autoload/ale/handlers/scala.vim @@ -0,0 +1,37 @@ +" Author: Nils Leuzinger - https://github.com/PawkyPenguin +" Description: Scala linting handlers for scalac-like compilers. + +function! ale#handlers#scala#HandleScalacLintFormat(buffer, lines) abort + " Matches patterns line the following: + " + " /var/folders/5q/20rgxx3x1s34g3m14n5bq0x80000gn/T/vv6pSsy/0:26: error: expected class or object definition + let l:pattern = '^.\+:\(\d\+\): \(\w\+\): \(.\+\)' + let l:output = [] + let l:ln = 0 + + for l:line in a:lines + let l:ln = l:ln + 1 + let l:match = matchlist(l:line, l:pattern) + + if len(l:match) == 0 + continue + endif + + let l:text = l:match[3] + let l:type = l:match[2] is# 'error' ? 'E' : 'W' + let l:col = 0 + + if l:ln + 1 < len(a:lines) + let l:col = stridx(a:lines[l:ln + 1], '^') + endif + + call add(l:output, { + \ 'lnum': l:match[1] + 0, + \ 'col': l:col + 1, + \ 'text': l:text, + \ 'type': l:type, + \}) + endfor + + return l:output +endfunction diff --git a/doc/ale.txt b/doc/ale.txt index 99d2befa..7ad46297 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -378,10 +378,10 @@ Notes: * Rust: `cargo`!!, `rls`, `rustc` (see |ale-integration-rust|), `rustfmt` * SASS: `sass-lint`, `stylelint` * SCSS: `prettier`, `sass-lint`, `scss-lint`, `stylelint` -* Scala: `scalac`, `scalastyle` +* Scala: `fsc`, `scalac`, `scalastyle` * Slim: `slim-lint` * SML: `smlnj` -* Solidity: `solhint, solium` +* Solidity: `solhint`, `solium` * Stylus: `stylelint` * SQL: `sqlint` * Swift: `swiftlint`, `swiftformat` diff --git a/test/command_callback/test_fsc_command_callback.vader b/test/command_callback/test_fsc_command_callback.vader new file mode 100644 index 00000000..451fa108 --- /dev/null +++ b/test/command_callback/test_fsc_command_callback.vader @@ -0,0 +1,17 @@ +Before: + runtime ale_linters/scala/fsc.vim + +After: + call ale#linter#Reset() + +Given scala(An empty Scala file): +Execute(The default executable and command should be correct): + AssertEqual 'fsc', ale_linters#scala#fsc#GetExecutable(bufnr('')) + AssertEqual + \ ale#Escape('fsc') . ' -Ystop-after:parser %t', + \ ale_linters#scala#fsc#GetCommand(bufnr('')) + +Given scala.sbt(An empty SBT file): +Execute(fsc should not be run for sbt files): + AssertEqual '', ale_linters#scala#fsc#GetExecutable(bufnr('')) + AssertEqual '', ale_linters#scala#fsc#GetCommand(bufnr('')) diff --git a/test/handler/test_scalac_handler.vader b/test/command_callback/test_scalac_command_callback.vader similarity index 99% rename from test/handler/test_scalac_handler.vader rename to test/command_callback/test_scalac_command_callback.vader index fd222f67..6bb31b3f 100644 --- a/test/handler/test_scalac_handler.vader +++ b/test/command_callback/test_scalac_command_callback.vader @@ -5,7 +5,6 @@ After: call ale#linter#Reset() Given scala(An empty Scala file): - Execute(The default executable and command should be correct): AssertEqual 'scalac', ale_linters#scala#scalac#GetExecutable(bufnr('')) AssertEqual diff --git a/test/handler/test_scala_handler.vader b/test/handler/test_scala_handler.vader new file mode 100644 index 00000000..3214bdbc --- /dev/null +++ b/test/handler/test_scala_handler.vader @@ -0,0 +1,32 @@ +After: + call ale#linter#Reset() + +Execute(The handler should return an empty list with empty input): + AssertEqual [], ale#handlers#scala#HandleScalacLintFormat(bufnr(''), []) + +Execute(The handler should correctly parse error messages): + AssertEqual + \ [ + \ { + \ 'lnum': 4, + \ 'col': 8, + \ 'text': ''':'' expected but identifier found.', + \ 'type': 'E' + \ }, + \ { + \ 'lnum': 6, + \ 'col': 2, + \ 'text': 'identifier expected but eof found.', + \ 'type': 'E' + \ } + \ ], + \ ale#handlers#scala#HandleScalacLintFormat(bufnr(''), + \ [ + \ "hi.scala:4: error: ':' expected but identifier found.", + \ " Some stupid scala code", + \ " ^", + \ "hi.scala:6: error: identifier expected but eof found.", + \ ")", + \ " ^", + \ "two errors found", + \ ])