Skip to content

Instantly share code, notes, and snippets.

@GoNZooo
Created September 20, 2020 11:11
Show Gist options
  • Save GoNZooo/e9b7ff3153210642c320fe381109009f to your computer and use it in GitHub Desktop.
Save GoNZooo/e9b7ff3153210642c320fe381109009f to your computer and use it in GitHub Desktop.
`zig build` on save with `ALE`
" zig ALE linter by fubd @ FreeNode
" Author: Kevin Watters <[email protected]>
" Description: This file adds support for checking zig code.
"
let g:ale_lint_on_text_changed = 'never'
let g:ale_zig_compiler = "zig"
function! ZigGetExecutable(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! ZigGetCommand(buffer) abort
let l:buf_direc = expand('%:h')
let l:direc = s:find_build_dir(l:buf_direc, 0)
return 'cd ' . direc . ' && ' . fnameescape(ZigGetExecutable(a:buffer))
\ . ' build --verbose'
endfunction
function! ZigHandleZigCompilerCheck(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': 'zigbuild',
\ 'executable_callback': 'ZigGetExecutable',
\ 'command_callback': 'ZigGetCommand',
\ 'callback': 'ZigHandleZigCompilerCheck',
\ 'output_stream': 'both'
\})
" zig bindings
augroup filetype_zig
autocmd FileType zig nnoremap <buffer> <leader>en :ALENextWrap<CR>
autocmd FileType zig nnoremap <buffer> <leader>ep :ALEPreviousWrap<CR>
autocmd FileType zig nnoremap <silent> <localleader>= :call zig#fmt#Format()<CR>
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment