-
Notifications
You must be signed in to change notification settings - Fork 0
/
shfmt.lua
72 lines (61 loc) · 1.52 KB
/
shfmt.lua
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
-- mod-version:3
-- Automatic formatter for bash scripts
-- Dependencies: `shfmt` command in PATH or whatever formatter is chosen in
-- configuration
-- Run with
-- Shfmt: format
-- Imports
local core = require "core"
local command = require "core.command"
local config = require "core.config"
local common = require "core.common"
-- Config
config.plugins.shfmt = common.merge({
enabled = true,
exec = "shfmt",
args = "-i 4 -w",
config_spec = {
name = "Shell Scripts Formatter",
{
label = "Formatter command",
description = "Path to executable.",
path = "exec",
type = "string",
default = "shfmt"
},
{
label = "Formatter arguments",
description = "Command line arguments.",
path = "args",
type = "string",
default = "-i 4 -w",
}
}
}, config.plugins.shfmt)
-- Main logic
local function format()
-- Save the file if it is unsaved or dirty
local doc = core.active_view.doc
if doc:get_name() == "unsaved" or doc:is_dirty() then
doc:save()
end
-- Run command
local ok, _, code = os.execute(
string.format(
"%s %s %s",
config.plugins.shfmt.exec,
config.plugins.shfmt.args,
doc:get_name()
)
)
-- Return with error code if any
if not ok then
core.log("Failed to format file, return code is " .. code)
return
end
-- Log success and reload file
core.log("File is reformatted, reloading document")
doc:reload()
end
-- Register the command
command.add("core.docview", {["shfmt:format"] = format})