From 347cec0f7e3069becd21934a63488277ee24ef20 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Thu, 28 May 2020 16:46:57 -0700 Subject: [PATCH] Initial commit --- help/zigfmt.md | 19 +++++++++++++++++ repo.json | 14 +++++++++++++ zigfmt.lua | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 help/zigfmt.md create mode 100644 repo.json create mode 100644 zigfmt.lua diff --git a/help/zigfmt.md b/help/zigfmt.md new file mode 100644 index 0000000..9d19d47 --- /dev/null +++ b/help/zigfmt.md @@ -0,0 +1,19 @@ +# Zig Fmt Plugin + +To run `zig fmt` on the current file: + +``` +> zigfmt +``` + +To run `zig fmt --check` on the current file (using the linter plugin): + +``` +> lint +``` + +To automatically run these when you save the file, use the following +options: + +* `zigfmt.fmt`: run `zig fmt` on file saved (will not report parse errors). Default value: `on` +* `zigfmt.lint`: enable `zig fmt --check` integration with the linter plugin. Default value: `on` diff --git a/repo.json b/repo.json new file mode 100644 index 0000000..336ef31 --- /dev/null +++ b/repo.json @@ -0,0 +1,14 @@ +[{ + "Name": "zigfmt", + "Description": "zig fmt on save and zig fmt --check linter support", + "Tags": ["zig", "ziglang"], + "Website": "https://github.com/squeek502/micro-zigfmt", + "Versions": [ + { + "Version": "0.1.0", + "Require": { + "micro": ">=2.0.0" + } + }, + ] +}] diff --git a/zigfmt.lua b/zigfmt.lua new file mode 100644 index 0000000..c9e1c72 --- /dev/null +++ b/zigfmt.lua @@ -0,0 +1,57 @@ +VERSION = "0.1.0" +PLUGIN_NAME = "zigfmt" + +local micro = import("micro") +local config = import("micro/config") +local shell = import("micro/shell") + +local COMMAND_NAME = "zigfmt" +local LINTER_NAME = "zigfmt" + +local function fullOptionName(name) + return PLUGIN_NAME .. "." .. name +end + +local ONSAVE_OPTION_NAME = "fmt" +local ONSAVE_OPTION = fullOptionName(ONSAVE_OPTION_NAME) +local LINTER_OPTION_NAME = "lint" +local LINTER_OPTION = fullOptionName(LINTER_OPTION_NAME) + +-- /path/to/test.zig:1:51: error: expected ';', found '}' +local ERROR_PATTERN = "%f:%l:%c: %m" + +config.RegisterCommonOption(PLUGIN_NAME, ONSAVE_OPTION_NAME, true) +config.RegisterCommonOption(PLUGIN_NAME, LINTER_OPTION_NAME, true) + +function init() + config.MakeCommand(COMMAND_NAME, zigfmt, config.NoComplete) + config.AddRuntimeFile(PLUGIN_NAME, config.RTHelp, "help/zigfmt.md") + + linter.makeLinter(LINTER_NAME, "zig", "zig", {"fmt", "--check", "%f"}, ERROR_PATTERN, {}, false, false, 0, 0, function(buf) + return buf.Settings[LINTER_OPTION] + end) +end + +function onSave(bp) + local shouldFmt = bp.Buf:FileType() == "zig" and bp.Buf.Settings[ONSAVE_OPTION] + if shouldFmt then + zigfmt(bp) + end + return true +end + +function zigfmt(bp) + bp:Save() + local output, err = shell.ExecCommand("zig", "fmt", bp.Buf.Path) + -- any failure here is a parse error, the linter will handle that + if err then + return + end + -- no files were changed (zig fmt prints the name of changed files) + if output == "" then + return + end + -- the file was changed by zig fmt, so reload it + bp.Buf:ReOpen() + micro.InfoBar():Message("Formatted " .. bp.Buf.Path) +end -- 2.44.0