Skip to content

Instantly share code, notes, and snippets.

@kevinw
Created September 8, 2019 19:23
Show Gist options
  • Save kevinw/9fea6317ee344e6203fcc22f6758be34 to your computer and use it in GitHub Desktop.
Save kevinw/9fea6317ee344e6203fcc22f6758be34 to your computer and use it in GitHub Desktop.
ALE linter for zig
" Author: Kevin Watters <[email protected]>
" Description: This file adds support for checking zig code.
function! ale_linters#zig#zig#GetExecutable(buffer) abort
return g:ale_zig_compiler
endfunction
function! s:find_build_dir(direc, count) abort
let l:path = a:direc . "/build.zig"
if filereadable(l:path)
return a:direc
else
if a:count < 10
return s:find_build_dir(expand(a:direc . "../"), a:count + 1)
endif
endif
return "."
endif
endfunction
function! ale_linters#zig#zig#GetCommand(buffer) abort
let l:buf_direc = expand('%:h')
let l:direc = s:find_build_dir(l:buf_direc, 0)
return 'cd ' . direc . ' && ' . fnameescape(ale_linters#zig#zig#GetExecutable(a:buffer))
\ . ' build --verbose'
endfunction
function! ale_linters#zig#zig#HandleZigCompilerCheck(buffer, lines) abort
" Regular expression to match messages:
" They look like:
"
" C:\Users\Foo\src\myproject\src\main.zig:24:4: error: invalid token: '.'
"
let l:pattern = '\v([^\(]+):(\d+):(\d+): error: (.*)$'
let l:output = [] " For each match, update the l:output list:
for l:match in ale#util#GetMatches(a:lines, l:pattern)
call add(l:output, {
\ 'filename': expand(l:match[1]),
\ 'lnum': str2nr(l:match[2]),
\ 'col': str2nr(l:match[3]),
\ 'text': l:match[4],
\ 'type': 'E'
\})
endfor
return l:output
endfunction
call ale#linter#Define('zig', {
\ 'name': 'zig',
\ 'executable_callback': 'ale_linters#zig#zig#GetExecutable',
\ 'command_callback': 'ale_linters#zig#zig#GetCommand',
\ 'callback': 'ale_linters#zig#zig#HandleZigCompilerCheck',
\ 'output_stream': 'both'
\})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment