-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyfmt.lua
74 lines (62 loc) · 1.58 KB
/
pyfmt.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
72
73
-- mod-version:3
-- Automatic formatter for python code
-- Dependencies: `black` command in PATH or whatever formatter is chosen in
-- configuration
-- This is a slightly re-written version of https://git.sr.ht/~tmpod/black-lite
-- Run with
-- Pyfmt: format
-- Imports
local core = require "core"
local command = require "core.command"
local config = require "core.config"
local common = require "core.common"
-- Config
config.plugins.pyfmt = common.merge({
enabled = true,
exec = "black",
args = "",
config_spec = {
name = "Python Code Formatter",
{
label = "Formatter command",
description = "Path to executable.",
path = "exec",
type = "string",
default = "black"
},
{
label = "Formatter arguments",
description = "Command line arguments.",
path = "args",
type = "string",
default = "",
}
}
}, config.plugins.pyfmt)
-- 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.pyfmt.exec,
config.plugins.pyfmt.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", {["pyfmt:format"] = format})