]> git.lizzy.rs Git - micro-zigfmt.git/commitdiff
Initial commit
authorRyan Liptak <squeek502@hotmail.com>
Thu, 28 May 2020 23:46:57 +0000 (16:46 -0700)
committerRyan Liptak <squeek502@hotmail.com>
Fri, 29 May 2020 00:01:18 +0000 (17:01 -0700)
help/zigfmt.md [new file with mode: 0644]
repo.json [new file with mode: 0644]
zigfmt.lua [new file with mode: 0644]

diff --git a/help/zigfmt.md b/help/zigfmt.md
new file mode 100644 (file)
index 0000000..9d19d47
--- /dev/null
@@ -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 (file)
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 (file)
index 0000000..c9e1c72
--- /dev/null
@@ -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