Skip to content

Commit 29ceb12

Browse files
euclidianAceandrewrk
authored andcommitted
Do fmt and ast-check steps separately
closes #69 as this allows for configuration to disable --ast-check
1 parent 8a204da commit 29ceb12

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ This plugin enables automatic code formatting on save by default using
3737
let g:zig_fmt_autosave = 0
3838
```
3939

40+
Additionally, `zig ast-check` is run after formatting, to disable this,
41+
set the following:
42+
43+
```
44+
let g:zig_ast_check_autosave = 0
45+
```
46+
47+
The above `zig fmt` and `zig ast-check` are done in separate steps, to do them
48+
in one step (as `zig fmt --ast-check`) set the following:
49+
50+
```
51+
let g:zig_fmt_ast_check_autosave = 1
52+
```
53+
4054
The default compiler which gets used by `:make` (`:help :compiler` for details)
4155
is `zig_build` and it runs `zig build`. The other options are:
4256
* `:compiler zig_test` which runs `zig test` on the current file.

autoload/zig/fmt.vim

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
" Use of this source code is governed by a BSD-style
55
" license that can be found in the LICENSE file.
66

7-
function! zig#fmt#Format() abort
7+
function! zig#fmt#Format(do_astcheck) abort
88
" Save cursor position and many other things.
99
let view = winsaveview()
1010

@@ -13,7 +13,10 @@ function! zig#fmt#Format() abort
1313
return
1414
endif
1515

16-
let cmdline = 'zig fmt --stdin --ast-check'
16+
let cmdline = 'zig fmt --stdin'
17+
if a:do_astcheck
18+
let cmdline .= ' --ast-check'
19+
endif
1720
let current_buf = bufnr('')
1821

1922
" The formatted code is output on stdout, the errors go on stderr.
@@ -77,6 +80,38 @@ function! zig#fmt#Format() abort
7780
syntax sync fromstart
7881
endfunction
7982

83+
function! zig#fmt#Astcheck() abort
84+
if !executable('zig')
85+
echohl Error | echomsg "no zig binary found in PATH" | echohl None
86+
return
87+
endif
88+
89+
let cmdline = 'zig ast-check'
90+
let current_buf = bufnr('')
91+
if exists('*systemlist')
92+
silent let out = systemlist(cmdline, current_buf)
93+
else
94+
silent let out = split(system(cmdline, current_buf))
95+
endif
96+
let err = v:shell_error
97+
98+
if err == 0
99+
call setloclist(0, [], 'r')
100+
lclose
101+
else
102+
let errors = s:parse_errors(expand('%'), out)
103+
104+
call setloclist(0, [], 'r', {
105+
\ 'title': 'Errors',
106+
\ 'items': errors,
107+
\ })
108+
109+
let max_win_height = get(g:, 'zig_fmt_max_window_height', 5)
110+
let win_height = min([max_win_height, len(errors)])
111+
execute 'silent! lwindow ' . win_height
112+
endif
113+
endfunction
114+
80115
" parse_errors parses the given errors and returns a list of parsed errors
81116
function! s:parse_errors(filename, lines) abort
82117
" list of errors to be put into location list

plugin/zig.vim

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@ if exists("g:zig_loaded")
33
endif
44
let g:zig_loaded = 1
55

6-
function! s:fmt_autosave()
7-
if get(g:, "zig_fmt_autosave", 1)
8-
call zig#fmt#Format()
6+
function! s:on_save()
7+
if get(g:, "zig_fmt_ast_check_autosave", 0)
8+
call zig#fmt#Format(v:true)
9+
else
10+
if get(g:, "zig_fmt_autosave", 1)
11+
call zig#fmt#Format(v:false)
12+
endif
13+
if get(g:, "zig_ast_check_autosave", 1)
14+
call zig#fmt#Astcheck()
15+
endif
916
endif
1017
endfunction
1118

1219
augroup vim-zig
1320
autocmd!
14-
autocmd BufWritePre *.zig call s:fmt_autosave()
21+
autocmd BufWritePre *.zig call s:on_save()
1522
augroup end
1623

1724
" vim: sw=2 ts=2 et

0 commit comments

Comments
 (0)