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
This commit is contained in:
Nils Leuzinger 2018-04-05 21:04:11 +02:00 committed by w0rp
parent a2acdecbc2
commit 912f632bf5
8 changed files with 120 additions and 41 deletions

View File

@ -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) |

29
ale_linters/scala/fsc.vim Normal file
View File

@ -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',
\})

View File

@ -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',
\})

View File

@ -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

View File

@ -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`

View File

@ -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(''))

View File

@ -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

View File

@ -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",
\ ])