Add naga linter for WGSL support (#4047)

* Add WGSL support using `naga` command

* Add documents for wgsl

* Add test for `naga` linter

* Separate naga handler callback to hanlder/naga.vim
This commit is contained in:
Linda_pp 2022-02-04 16:29:28 +09:00 committed by GitHub
parent d1e2aaf85d
commit 0d529d9b94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 98 additions and 0 deletions

12
ale_linters/wgsl/naga.vim Normal file
View File

@ -0,0 +1,12 @@
" Author: rhysd <https://github.com/rhysd>
" Description: naga-cli linter for WGSL syntax.
call ale#Set('wgsl_naga_executable', 'naga')
call ale#linter#Define('wgsl', {
\ 'name': 'naga',
\ 'executable': {b -> ale#Var(b, 'wgsl_naga_executable')},
\ 'output_stream': 'stderr',
\ 'command': {b -> '%e --stdin-file-path %s'},
\ 'callback': 'ale#handlers#naga#Handle',
\})

View File

@ -0,0 +1,30 @@
" Author: rhysd <https://github.com/rhysd>
" Description: Handle errors for naga-cli.
function! ale#handlers#naga#Handle(buffer, lines) abort
let l:errors = []
let l:current_error = v:null
for l:line in a:lines
if l:line =~# '^error: '
let l:text = l:line[7:]
let l:current_error = { 'text': l:text, 'type': 'E' }
continue
endif
if l:current_error isnot v:null
let l:matches = matchlist(l:line, '\v:(\d+):(\d+)$')
if !empty(l:matches)
let l:current_error.lnum = str2nr(l:matches[1])
let l:current_error.col = str2nr(l:matches[2])
call add(l:errors, l:current_error)
let l:current_error = v:null
continue
endif
endif
endfor
return l:errors
endfunction

View File

@ -628,6 +628,8 @@ Notes:
* `prettier` * `prettier`
* `vls` * `vls`
* `volar` * `volar`
* WGSL
* `naga`
* XHTML * XHTML
* `alex` * `alex`
* `cspell` * `cspell`

17
doc/ale-wgsl.txt Normal file
View File

@ -0,0 +1,17 @@
===============================================================================
ALE WGSL Integration *ale-wgsl-options*
===============================================================================
naga *ale-wgsl-naga*
g:ale_wgsl_naga_executable *g:ale_wgsl_naga_executable*
*b:ale_wgsl_naga_executable*
Type: |String|
Default: `'naga'`
The executable that will be run for the `naga` linter.
===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

View File

@ -3222,6 +3222,8 @@ documented in additional help files.
prettier..............................|ale-vue-prettier| prettier..............................|ale-vue-prettier|
vls...................................|ale-vue-vls| vls...................................|ale-vue-vls|
volar.................................|ale-vue-volar| volar.................................|ale-vue-volar|
wgsl....................................|ale-wgsl-options|
naga..................................|ale-wgsl-naga|
xhtml...................................|ale-xhtml-options| xhtml...................................|ale-xhtml-options|
cspell................................|ale-xhtml-cspell| cspell................................|ale-xhtml-cspell|
write-good............................|ale-xhtml-write-good| write-good............................|ale-xhtml-write-good|

View File

@ -637,6 +637,8 @@ formatting.
* [prettier](https://github.com/prettier/prettier) * [prettier](https://github.com/prettier/prettier)
* [vls](https://github.com/vuejs/vetur/tree/master/server) * [vls](https://github.com/vuejs/vetur/tree/master/server)
* [volar](https://github.com/johnsoncodehk/volar) * [volar](https://github.com/johnsoncodehk/volar)
* WGSL
* [naga](https://github.com/gfx-rs/naga)
* XHTML * XHTML
* [alex](https://github.com/get-alex/alex) * [alex](https://github.com/get-alex/alex)
* [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell) * [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell)

View File

@ -0,0 +1,23 @@
Before:
runtime ale_linters/wgsl/naga.vim
After:
call ale#linter#Reset()
Execute(Error handler should parse error message and position from input):
let example_output = [
\ "error: expected global item ('struct', 'let', 'var', 'type', ';', 'fn') or the end of the file, found '['",
\ " ┌─ wgsl:5:1",
\ " │",
\ "5 │ [[group(1), binding(0)]]",
\ " │ ^ expected global item ('struct', 'let', 'var', 'type', ';', 'fn') or the end of the file",
\ "Could not parse WGSL",
\ ]
let actual = ale#handlers#naga#Handle(0, example_output)
let expected = [{
\ "text": "expected global item ('struct', 'let', 'var', 'type', ';', 'fn') or the end of the file, found '['",
\ "lnum": 5,
\ "col": 1,
\ "type": "E",
\ }]
AssertEqual actual, expected

View File

@ -0,0 +1,10 @@
Before:
call ale#assert#SetUpLinterTest('wgsl', 'naga')
After:
call ale#assert#TearDownLinterTest()
Execute(The naga command should be customizable):
let g:ale_wgsl_naga_executable = '/path/to/naga'
AssertLinter '/path/to/naga',
\ ale#Escape('/path/to/naga') . ' --stdin-file-path %s'