-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperltidy.lua
44 lines (35 loc) · 1.07 KB
/
perltidy.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
-- mod-version:3
local core = require "core"
local command = require "core.command"
local keymap = require "core.keymap"
local PERLTIDY_EXECUTABLE = "perltidy"
local function exec(cmd)
local file_handle = io.popen(cmd, "r")
local res = file_handle:read("*a")
local success = file_handle:close()
return res:gsub("%\n$", ""), success
end
local function get_doc_name(doc)
return doc and system.absolute_path(doc.filename or "")
end
local function update_doc(doc)
local cmd = string.format("%s --standard-output %s", PERLTIDY_EXECUTABLE, get_doc_name(doc))
local text, success = exec(cmd)
if success == nil then
core.error("Perltidy is not on your path, you can edit perltidy.lua to change to an absolute path")
return
end
local selection = { doc:get_selection() }
doc:remove(1, 1, math.huge, math.huge)
doc:insert(1, 1, text)
doc:set_selection(table.unpack(selection))
command.perform "doc:save"
end
command.add("core.docview!", {
["perltidy:perltidy"] = function(dv)
update_doc(dv.doc)
end
})
keymap.add {
["ctrl+i"] = "perltidy:perltidy"
}