Last active
March 21, 2020 05:16
-
-
Save rochacbruno/45b674e608837b096777fc7ac1608405 to your computer and use it in GitHub Desktop.
micro/init.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Save this file to ~/.config/micro/init.lua | |
-- requirements: | |
-- Python3 | |
-- - isort | |
-- - black | |
-- - flake8 | |
-- - ipython | |
-- Rust | |
-- - cargo-play | |
-- - evcxr_repl | |
-- - clippy | |
-- - fmt | |
VERSION = "0.0.3" | |
local micro = import("micro") | |
local config = import("micro/config") | |
local shell = import("micro/shell") | |
local buffer = import("micro/buffer") | |
function init() | |
-- this will modify the bindings.json file | |
-- true means overwrite any existing binding | |
config.TryBindKey("Alt-b", "lua:initlua.build", true) | |
config.TryBindKey("Alt-o", "lua:initlua.output", true) | |
config.TryBindKey("Alt-t", "lua:initlua.test", true) | |
config.TryBindKey("Alt-f", "lua:initlua.format", true) | |
config.TryBindKey("Alt-i", "lua:initlua.repl", true) | |
config.TryBindKey("Alt-l", "lua:initlua.lint", true) | |
config.TryBindKey("Alt-y", "lua:initlua.sort_imports", true) | |
config.TryBindKey("CtrlRightSq", "lua:initlua.vsplit_left", true) | |
config.TryBindKey("Alt-|", "lua:initlua.new_view", true) | |
-- TODO: Add rename variable utility (example below) | |
-- https://github.com/micro-editor/go-plugin/blob/8d7c7dfd4488e25a2e3f5eb37aac3ccacc0143bc/go.lua#L44 | |
end | |
-- utils | |
function setContains(set, key) | |
return set[key] ~= nil | |
end | |
-- below code adds new buffer with "hello content" | |
-- micro.CurPane():VSplitIndex(buffer.NewBuffer("hello", "filename"), true) | |
-- actions | |
function vsplit_left(bp) | |
-- Open a new Vsplit (on the very left) | |
-- false means right=false | |
micro.CurPane():VSplitIndex(buffer.NewBuffer("", ""), false) | |
end | |
function new_view(bp) | |
-- Open same file Vsplit (on the very right) | |
-- true means right=true | |
-- bp.Buf.Type.Readonly = true | |
micro.CurPane():VSplitIndex(bp.Buf, true) | |
micro.InfoBar():Message("New View same file") | |
end | |
function output(bp) | |
bp:Save() | |
local buf = bp.Buf | |
_command = {} | |
_command["go"] = "go run " .. buf.Path | |
-- cargo install cargo-play | |
_command["rust"] = "cargo play " .. buf.Path | |
_command["python"] = "python3 " .. buf.Path | |
run_action(bp.Buf, _command, "Output", true) -- false=no bottom panel | |
end | |
function build(bp) | |
bp:Save() | |
local buf = bp.Buf | |
_command = {} | |
_command["go"] = "go run " .. buf.Path | |
-- cargo install cargo-play | |
_command["rust"] = "cargo play " .. buf.Path | |
_command["python"] = "python3 " .. buf.Path | |
-- the true means run in the foreground | |
-- the false means send output to stdout (instead of returning it) | |
shell.RunInteractiveShell(_command[buf:FileType()], true, false) | |
end | |
function test(bp) | |
bp:Save() | |
local buf = bp.Buf | |
_command = {} | |
_command["go"] = "go test -v " .. buf.Path | |
-- TODO: make cargo to run specific file tests | |
_command["rust_"] = "cargo play --test " .. buf.Path | |
_command["rust"] = "cargo test -v --color always " | |
_command["python"] = "python3 -m pytest -svx " .. buf.Path | |
-- the true means run in the foreground | |
-- the false means send output to stdout (instead of returning it) | |
-- shell.RunInteractiveShell(_command[buf:FileType()], true, false) | |
-- Opens test output in a new bottom panel | |
run_action(bp.Buf, _command, "Test", true) -- false=no bottom panel | |
end | |
function format(bp) | |
local buf = bp.Buf | |
local filetype = buf:FileType() | |
_command = {} | |
_command["go"] = "go fmt -w" .. buf.Path | |
_command["rust"] = "rustfmt -v -l --backup --edition=2018 " .. buf.Path | |
_command["python"] = "black -l 79 " .. buf.Path | |
if not setContains(_command, filetype) then | |
return | |
end | |
bp:Save() -- TODO: is it saving twice | |
run_action(bp.Buf, _command, "Format", false) -- false=no bottom panel | |
buf:ReOpen() | |
end | |
function sort_imports(bp) | |
local buf = bp.Buf | |
local filetype = buf:FileType() | |
_command = {} | |
_command["go"] = "goimports -w " .. buf.Path | |
_command["python"] = "isort " .. buf.Path | |
if not setContains(_command, filetype) then | |
return | |
end | |
bp:Save() -- TODO: is it saving twice | |
run_action(bp.Buf, _command, "SortImports", false) -- false=no bottom panel | |
buf:ReOpen() | |
end | |
function repl(bp) | |
bp:Save() | |
local buf = bp.Buf | |
_command = {} | |
-- _command["go"] = "go ? " .. buf.Path | |
-- cargo install evcxr_repl | |
_command["rust"] = "evcxr " | |
-- TODO: make evcxr interactive? print usage info before opening | |
_command["python"] = "ipython -i " .. buf.Path | |
-- the true means run in the foreground | |
-- the false means send output to stdout (instead of returning it) | |
shell.RunInteractiveShell(_command[buf:FileType()], true, false) | |
end | |
function lint(bp) | |
bp:Save() | |
_command = {} | |
-- _command["go"] = "go ? " .. buf.Path | |
_command["rust"] = "cargo-clippy " | |
_command["python"] = "flake8 " .. bp.Buf.Path | |
run_action(bp.Buf, _command, "Linter", true) | |
end | |
function onSave(bp) | |
sort_imports(bp) | |
format(bp) | |
-- TODO: Use config to decide what to run on save. | |
-- example: https://github.com/micro-editor/go-plugin/blob/8d7c7dfd4488e25a2e3f5eb37aac3ccacc0143bc/go.lua#L10 | |
-- if bp.Buf:FileType() == "go" then | |
-- if bp.Buf.Settings["go.goimports"] then | |
-- goimports(bp) | |
-- elseif bp.Buf.Settings["go.gofmt"] then | |
-- gofmt(bp) | |
-- end | |
-- end | |
return true | |
end | |
function run_action(buf, commands, identifier, bottom_panel) | |
local filetype = buf:FileType() | |
if not setContains(commands, filetype) then | |
return -- if filetype does not support action just return | |
end | |
local output, err = shell.RunCommand(commands[filetype]) | |
local msg = output | |
if err ~= nil then | |
msg = msg .. tostring(err) | |
end | |
if msg ~= "" then | |
if bottom_panel then | |
micro.CurPane():HSplitIndex( | |
buffer.NewBuffer(msg, identifier), | |
true -- means bottom split | |
) | |
else | |
if err ~= nil then | |
micro.InfoBar():Error(msg) | |
else | |
micro.InfoBar():Message(msg) | |
end | |
return | |
end | |
else | |
micro.InfoBar():Message(identifier .. ": all good :)") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bruno, acredito que para recarregar o buffer formatado você deverá utilizar
bp.Buf:ReOpen()
.https://github.com/micro-editor/go-plugin/blob/master/go.lua#L41