4 local micro = import("micro")
5 local config = import("micro/config")
6 local shell = import("micro/shell")
8 local COMMAND_NAME = "zigfmt"
9 local LINTER_NAME = "zigfmt"
11 local function fullOptionName(name)
12 return PLUGIN_NAME .. "." .. name
15 local ONSAVE_OPTION_NAME = "fmt"
16 local ONSAVE_OPTION = fullOptionName(ONSAVE_OPTION_NAME)
17 local LINTER_OPTION_NAME = "lint"
18 local LINTER_OPTION = fullOptionName(LINTER_OPTION_NAME)
20 -- /path/to/test.zig:1:51: error: expected ';', found '}'
21 local ERROR_PATTERN = "%f:%l:%c: %m"
23 config.RegisterCommonOption(PLUGIN_NAME, ONSAVE_OPTION_NAME, true)
24 config.RegisterCommonOption(PLUGIN_NAME, LINTER_OPTION_NAME, true)
27 config.MakeCommand(COMMAND_NAME, zigfmt, config.NoComplete)
28 config.AddRuntimeFile(PLUGIN_NAME, config.RTHelp, "help/zigfmt.md")
31 linter.makeLinter(LINTER_NAME, "zig", "zig", {"fmt", "--check", "%f"}, ERROR_PATTERN, {}, false, false, 0, 0, function(buf)
32 return buf.Settings[LINTER_OPTION]
38 local shouldFmt = bp.Buf:FileType() == "zig" and bp.Buf.Settings[ONSAVE_OPTION]
47 local output, err = shell.ExecCommand("zig", "fmt", bp.Buf.Path)
48 -- any failure here is a parse error, the linter will handle that
52 -- no files were changed (zig fmt prints the name of changed files)
56 -- the file was changed by zig fmt, so reload it
58 micro.InfoBar():Message("Formatted " .. bp.Buf.Path)