Commit: runtime(zig): refactor zig ftplugin, remove auto format

5 views
Skip to first unread message

Christian Brabandt

unread,
May 21, 2024, 5:30:12 PMMay 21
to vim...@googlegroups.com
runtime(zig): refactor zig ftplugin, remove auto format

Commit: https://github.com/vim/vim/commit/d1d9316c6621d275593f58664975faf0fc23985f
Author: Tiseno <mathias....@stabelo.se>
Date: Tue May 21 23:27:03 2024 +0200

runtime(zig): refactor zig ftplugin, remove auto format

Refactored zig ftplugin, removed upstream comment, aucmd and
auto formatting support. Updated documentation for zig configuration
settings and added new maintainer.

closes: #13803

Signed-off-by: Tiseno <mathias....@stabelo.se>
Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/runtime/autoload/zig/fmt.vim b/runtime/autoload/zig/fmt.vim
deleted file mode 100644
index b78c1994d..000000000
--- a/runtime/autoload/zig/fmt.vim
+++ /dev/null
@@ -1,100 +0,0 @@
-" Adapted from fatih/vim-go: autoload/go/fmt.vim
-"
-" Copyright 2011 The Go Authors. All rights reserved.
-" Use of this source code is governed by a BSD-style
-" license that can be found in the LICENSE file.
-"
-" Upstream: https://github.com/ziglang/zig.vim
-
-function! zig#fmt#Format() abort
- " Save cursor position and many other things.
- let view = winsaveview()
-
- if !executable('zig')
- echohl Error | echomsg "no zig binary found in PATH" | echohl None
- return
- endif
-
- let cmdline = 'zig fmt --stdin --ast-check'
- let current_buf = bufnr('')
-
- " The formatted code is output on stdout, the errors go on stderr.
- if exists('*systemlist')
- silent let out = systemlist(cmdline, current_buf)
- else
- silent let out = split(system(cmdline, current_buf))
- endif
- if len(out) == 1
- if out[0] == "error: unrecognized parameter: '--ast-check'"
- let cmdline = 'zig fmt --stdin'
- if exists('*systemlist')
- silent let out = systemlist(cmdline, current_buf)
- else
- silent let out = split(system(cmdline, current_buf))
- endif
- endif
- endif
- let err = v:shell_error
-
-
- if err == 0
- " remove undo point caused via BufWritePre.
- try | silent undojoin | catch | endtry
-
- " Replace the file content with the formatted version.
- if exists('*deletebufline')
- call deletebufline(current_buf, len(out), line('$'))
- else
- silent execute ':' . len(out) . ',' . line('$') . ' delete _'
- endif
- call setline(1, out)
-
- " No errors detected, close the loclist.
- call setloclist(0, [], 'r')
- lclose
- elseif get(g:, 'zig_fmt_parse_errors', 1)
- let errors = s:parse_errors(expand('%'), out)
-
- call setloclist(0, [], 'r', {
- \ 'title': 'Errors',
- \ 'items': errors,
- \ })
-
- let max_win_height = get(g:, 'zig_fmt_max_window_height', 5)
- " Prevent the loclist from becoming too long.
- let win_height = min([max_win_height, len(errors)])
- " Open the loclist, but only if there's at least one error to show.
- execute 'silent! lwindow ' . win_height
- endif
-
- call winrestview(view)
-
- if err != 0
- echohl Error | echomsg "zig fmt returned error" | echohl None
- return
- endif
-
- " Run the syntax highlighter on the updated content and recompute the folds if
- " needed.
- syntax sync fromstart
-endfunction
-
-" parse_errors parses the given errors and returns a list of parsed errors
-function! s:parse_errors(filename, lines) abort
- " list of errors to be put into location list
- let errors = []
- for line in a:lines
- let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)')
- if !empty(tokens)
- call add(errors,{
- \"filename": a:filename,
- \"lnum": tokens[2],
- \"col": tokens[3],
- \"text": tokens[4],
- \ })
- endif
- endfor
-
- return errors
-endfunction
-" vim: sw=2 ts=2 et
diff --git a/runtime/doc/filetype.txt b/runtime/doc/filetype.txt
index 8f4dc1385..c38f9bf95 100644
--- a/runtime/doc/filetype.txt
+++ b/runtime/doc/filetype.txt
@@ -1,4 +1,4 @@
-*filetype.txt* For Vim version 9.1. Last change: 2024 Apr 18
+*filetype.txt* For Vim version 9.1. Last change: 2024 May 21


VIM REFERENCE MANUAL by Bram Moolenaar
@@ -889,6 +889,31 @@ The mappings can be disabled with: >
let g:no_vim_maps = 1


+ZIG *ft-zig-plugin*
+
+ *g:zig_recommended_style*
+By default the following indentation options are set, in accordance with Zig's
+recommended style (https://ziglang.org/documentation/master/): >
+
+ setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
+<
+To disable this behavior, set |g:zig_recommended_style| to 0: >
+
+ let g:zig_recommended_style = 0
+<
+ *g:zig_std_dir*
+The path to the Zig standard library. The Zig |ftplugin| reads |g:zig_std_dir|
+and appends it to the 'path' for Zig files. Where the Zig standard library
+is located is system and installation method dependent.
+
+One can automatically set |g:zig_std_dir| using `zig env`: >
+
+ let g:zig_std_dir = json_decode(system('zig env'))['std_dir']
+<
+This can, for example, be put in a FileType |:autocmd| or user |ftplugin| to
+only load when a Zig file is opened.
+
+
ZIMBU *ft-zimbu-plugin*

The Zimbu filetype plugin defines mappings to move to the start and end of
diff --git a/runtime/doc/tags b/runtime/doc/tags
index a6bc52339..4e59db7d5 100644
--- a/runtime/doc/tags
+++ b/runtime/doc/tags
@@ -7404,6 +7404,7 @@ ft-xml-syntax syntax.txt /*ft-xml-syntax*
ft-xpm-syntax syntax.txt /*ft-xpm-syntax*
ft-yaml-indent indent.txt /*ft-yaml-indent*
ft-yaml-syntax syntax.txt /*ft-yaml-syntax*
+ft-zig-plugin filetype.txt /*ft-zig-plugin*
ft-zimbu-plugin filetype.txt /*ft-zimbu-plugin*
ft-zsh-syntax syntax.txt /*ft-zsh-syntax*
ft_ada.txt ft_ada.txt /*ft_ada.txt*
@@ -7702,6 +7703,8 @@ g:vimsyn_maxlines syntax.txt /*g:vimsyn_maxlines*
g:vimsyn_minlines syntax.txt /*g:vimsyn_minlines*
g:vimsyn_noerror syntax.txt /*g:vimsyn_noerror*
g:yaml_schema syntax.txt /*g:yaml_schema*
+g:zig_recommended_style filetype.txt /*g:zig_recommended_style*
+g:zig_std_dir filetype.txt /*g:zig_std_dir*
g:zipPlugin_ext pi_zip.txt /*g:zipPlugin_ext*
g:zip_exec pi_zip.txt /*g:zip_exec*
g:zip_extractcmd pi_zip.txt /*g:zip_extractcmd*
diff --git a/runtime/ftplugin/zig.vim b/runtime/ftplugin/zig.vim
index 28b8cd5a6..ea229b6a4 100644
--- a/runtime/ftplugin/zig.vim
+++ b/runtime/ftplugin/zig.vim
@@ -1,68 +1,52 @@
" Vim filetype plugin file
-" Language: Zig
-" Upstream: https://github.com/ziglang/zig.vim
+" Language: Zig
+" Maintainer: Mathias Lindgren <math.l...@gmail.com>
+" Last Change: 2024 May 21
+" Based on: https://github.com/ziglang/zig.vim

-" Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
finish
endif

let b:did_ftplugin = 1

-let s:cpo_orig = &cpo
+let s:cpo_save = &cpo
set cpo&vim

compiler zig_build

" Match Zig builtin fns
setlocal iskeyword+=@-@
-
-" Recommended code style, no tabs and 4-space indentation
-setlocal expandtab
-setlocal tabstop=8
-setlocal softtabstop=4
-setlocal shiftwidth=4
-
setlocal formatoptions-=t formatoptions+=croql
-
-setlocal suffixesadd=.zig,.zir
+setlocal suffixesadd=.zig,.zir,.zon
+let &l:define=' (<fn>|<const>|<var>|^\s*\#\s*define)'
+let b:undo_ftplugin = 'setl isk< fo< sua< mp< def<'
+
+if get(g:, 'zig_recommended_style', 1)
+ setlocal expandtab
+ setlocal tabstop=8
+ setlocal softtabstop=4
+ setlocal shiftwidth=4
+ let b:undo_ftplugin .= ' | setl et< ts< sts< sw<'
+endif

if has('comments')
setlocal comments=:///,://!,://
setlocal commentstring=//\ %s
+ let b:undo_ftplugin .= ' | setl com< cms<'
endif

if has('find_in_path')
let &l:includeexpr='substitute(v:fname, "^([^.])$", " .zig", "")'
let &l:include=' (\@import>|\@cInclude>|^\s*\#\s*include)'
-endif
-
-let &l:define=' (<fn>|<const>|<var>|^\s*\#\s*define)'
-
-" Safety check: don't execute zig from current directory
-if !exists('g:zig_std_dir') && exists('*json_decode') &&
- \ executable('zig') && dist#vim#IsSafeExecutable('zig', 'zig')
- silent let s:env = system('zig env')
- if v:shell_error == 0
- let g:zig_std_dir = json_decode(s:env)['std_dir']
- endif
- unlet! s:env
+ let b:undo_ftplugin .= ' | setl inex< inc<'
endif

if exists('g:zig_std_dir')
- let &l:path = g:zig_std_dir . ',' . &l:path
+ let &l:path .= ',' . g:zig_std_dir
+ let b:undo_ftplugin .= ' | setl pa<'
endif

-let b:undo_ftplugin =
- \ 'setl isk< et< ts< sts< sw< fo< sua< mp< com< cms< inex< inc< pa<'
-
-augroup vim-zig
- autocmd! * <buffer>
- autocmd BufWritePre <buffer> if get(g:, 'zig_fmt_autosave', 1) | call zig#fmt#Format() | endif
-augroup END
-
-let b:undo_ftplugin .= '|au! vim-zig * <buffer>'
-
-let &cpo = s:cpo_orig
-unlet s:cpo_orig
+let &cpo = s:cpo_save
+unlet s:cpo_save
" vim: tabstop=8 shiftwidth=4 softtabstop=4 expandtab
Reply all
Reply to author
Forward
0 new messages