-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathautofmt.lua
More file actions
96 lines (86 loc) · 2.66 KB
/
autofmt.lua
File metadata and controls
96 lines (86 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
VERSION = "3.0.0"
local config = import("micro/config")
local filepath = import("path/filepath")
local micro = import("micro")
local fmtNames = {}
fmtNames["yapf"] = "yapf -i"
fmtNames["ruff"] = "ruff format -s"
fmtNames["ruff+check"] = {"ruff check --fix", "ruff format -s"}
fmtNames["black"] = "black -q"
fmtNames["clang-format"] = "clang-format -i"
fmtNames["prettier"] = "prettier --write --log-level silent"
fmtNames["rustfmt"] = "rustfmt"
fmtNames["rustfmt+nightly"] = "rustfmt +nightly"
fmtNames["gofmt"] = "gofmt -w"
fmtNames["raco-fmt"] = "raco fmt --width 80 --max-blank-lines 2 -i"
fmtNames["forge-fmt"] = "forge fmt"
fmtNames["stylua"] = "stylua"
fmtNames["nixfmt"] = "nixfmt"
local fmtFor = {}
fmtFor["python"] = {"yapf", "ruff", "ruff+check", "black"}
fmtFor["c"] = {"clang-format"}
fmtFor["c++"] = {"clang-format"}
fmtFor["csharp"] = {"clang-format"}
fmtFor["racket"] = {"raco-fmt"}
fmtFor["rust"] = {"rustfmt", "rustfmt+nightly"}
fmtFor["go"] = {"gofmt"}
fmtFor["solidity"] = {"forge-fmt"}
fmtFor["javascript"] = {"prettier"}
fmtFor["json"] = {"prettier"}
fmtFor["html"] = {"prettier"}
fmtFor["typescript"] = {"prettier"}
fmtFor["css"] = {"prettier"}
fmtFor["lua"] = {"stylua"}
fmtFor["nix"] = {"nixfmt"}
config.RegisterCommonOption("autofmt", "onsave", true)
for lang, formatters in pairs(fmtFor) do
config.RegisterCommonOption("autofmt", "for-" .. lang, formatters[1])
end
function init()
config.MakeCommand("fmt", fmtCommand, config.NoComplete)
config.AddRuntimeFile("fmt", config.RTHelp, "help/autofmt.md")
end
function onSave(bp)
if bp.Buf.Settings["autofmt.onsave"] then
tryFmt(bp)
end
end
function fmtCommand(bp, args)
if #args == 0 then
tryFmt(bp)
return
end
local cmd = fmtNames[args[1]]
if cmd == nil then
micro.InfoBar():Error("autofmt: unknown formatter '" .. args[1] .. "'")
return
end
doFmt(bp, cmd)
end
function tryFmt(bp)
local lang = bp.Buf:FileType()
if fmtFor[lang] == nil then return end
local fmtName = bp.Buf.Settings["autofmt.for-" .. lang]
local cmd = fmtNames[fmtName]
if cmd == nil then
micro.InfoBar():Error("autofmt: unknown formatter '" .. fmtName .. "' for " .. lang)
return
end
doFmt(bp, cmd)
end
function doFmt(bp, fmtCmd)
bp:Save()
local dirPath, _ = filepath.Split(bp.Buf.AbsPath)
local cmds = fmtCmd
if type(cmds) ~= "table" then
cmds = {cmds}
end
for _, cmd in ipairs(cmds) do
local _, err = os.execute("cd \"" .. dirPath .. "\"; " .. cmd .. " " .. bp.Buf.AbsPath)
if err ~= nil then
micro.InfoBar():Error(err)
return
end
end
bp.Buf:ReOpen()
end