forked from a11ce/micro-autofmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprettier.lua
More file actions
49 lines (42 loc) · 1.19 KB
/
prettier.lua
File metadata and controls
49 lines (42 loc) · 1.19 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
VERSION = "1.0.0"
local config = import("micro/config")
local shell = import("micro/shell")
local filepath = import("path/filepath")
local micro = import("micro")
local commonCommand = "prettier --write --log-level silent"
local fmtCommands = {
javascript = commonCommand,
jsx = commonCommand,
typescript = commonCommand,
css = commonCommand,
html = commonCommand,
json = commonCommand,
markdown = commonCommand,
yaml = commonCommand
}
function init()
config.RegisterCommonOption("prettier", "onsave", true)
config.MakeCommand("prettier", tryFmt, config.NoComplete)
config.AddRuntimeFile("prettier", config.RTHelp, "help/prettier.md")
end
function onSave(bp)
if bp.Buf.Settings["prettier.onsave"] == false then
return
end
tryFmt(bp)
end
function tryFmt(bp)
if fmtCommands[bp.Buf:FileType()] ~= nil then
doFmt(bp, fmtCommands[bp.Buf:FileType()])
end
end
function doFmt(bp, fmtCmd)
bp:Save()
local dirPath, _ = filepath.Split(bp.Buf.AbsPath)
local _, err = os.execute("cd \"" .. dirPath .. "\"; " .. fmtCmd .. " " .. bp.Buf.AbsPath)
if err ~= nil then
micro.InfoBar():Error(err)
return
end
bp.Buf:ReOpen()
end