Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add naga linter for WGSL support #4047

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ale_linters/wgsl/naga.vim
Original file line number Diff line number Diff line change
@@ -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',
\})
30 changes: 30 additions & 0 deletions autoload/ale/handlers/naga.vim
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions doc/ale-supported-languages-and-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ Notes:
* `prettier`
* `vls`
* `volar`
* WGSL
* `naga`
* XHTML
* `alex`
* `cspell`
Expand Down
17 changes: 17 additions & 0 deletions doc/ale-wgsl.txt
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 2 additions & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3222,6 +3222,8 @@ documented in additional help files.
prettier..............................|ale-vue-prettier|
vls...................................|ale-vue-vls|
volar.................................|ale-vue-volar|
wgsl....................................|ale-wgsl-options|
naga..................................|ale-wgsl-naga|
xhtml...................................|ale-xhtml-options|
cspell................................|ale-xhtml-cspell|
write-good............................|ale-xhtml-write-good|
Expand Down
2 changes: 2 additions & 0 deletions supported-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ formatting.
* [prettier](https://github.com/prettier/prettier)
* [vls](https://github.com/vuejs/vetur/tree/master/server)
* [volar](https://github.com/johnsoncodehk/volar)
* WGSL
* [naga](https://github.com/gfx-rs/naga)
* XHTML
* [alex](https://github.com/get-alex/alex)
* [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell)
Expand Down
23 changes: 23 additions & 0 deletions test/handler/test_naga_handler.vader
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions test/linter/test_naga.vader
Original file line number Diff line number Diff line change
@@ -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'