mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-02 10:50:25 +09:00
This may be a breaking change if you are using a plugin that checks the filetype. All the default plugins are now updated, just make the filetype you were checking for previously all lowercase.
51 lines
1.1 KiB
Lua
51 lines
1.1 KiB
Lua
if GetOption("goimports") == nil then
|
|
AddOption("goimports", false)
|
|
end
|
|
if GetOption("gofmt") == nil then
|
|
AddOption("gofmt", true)
|
|
end
|
|
|
|
if CurView().Buf.FileType == "go" then
|
|
SetOption("tabstospaces", "off")
|
|
end
|
|
|
|
MakeCommand("goimports", "go.goimports", 0)
|
|
MakeCommand("gofmt", "go.gofmt", 0)
|
|
|
|
function onSave()
|
|
if CurView().Buf.FileType == "go" then
|
|
if GetOption("goimports") then
|
|
goimports()
|
|
elseif GetOption("gofmt") then
|
|
gofmt()
|
|
end
|
|
end
|
|
end
|
|
|
|
function gofmt()
|
|
CurView():Save(false)
|
|
local handle = io.popen("gofmt -w " .. CurView().Buf.Path)
|
|
local result = handle:read("*a")
|
|
handle:close()
|
|
|
|
CurView():ReOpen()
|
|
end
|
|
|
|
function goimports()
|
|
CurView():Save(false)
|
|
local handle = io.popen("goimports -w " .. CurView().Buf.Path)
|
|
local result = split(handle:read("*a"), ":")
|
|
handle:close()
|
|
|
|
CurView():ReOpen()
|
|
end
|
|
|
|
function split(str, sep)
|
|
local result = {}
|
|
local regex = ("([^%s]+)"):format(sep)
|
|
for each in str:gmatch(regex) do
|
|
table.insert(result, each)
|
|
end
|
|
return result
|
|
end
|