[QUESTION] Formatting/beautifying php, js, html,...
Last weeks using this awesome Micro editor. Trying customizing some settings and keybindings. I'm a webdeveloper, but missing a formatting tool in Micro. Are there plans adding a formatting-tool/plugin for Micro? Found the fmt-plugin, but doesn't work in the latest Micro version (2.0.8).
Have the same question, formatting is very important and fmt stopped supporting anything that is over v2.0
You can make a custom formatter with a small amount of lua code. Here is the example from the Go plugin that uses gofmt (this code would go in ~/.config/micro/init.lua or a custom plugin in ~/.config/micro/plug/... if you choose):
local micro = import("micro")
local shell = import("micro/shell")
function gofmt(bp)
bp:Save()
local _, err = shell.RunCommand("gofmt -w " .. bp.Buf.Path)
if err ~= nil then
micro.InfoBar():Error(err)
return
end
bp.Buf:ReOpen()
end
-- to run the formatter on every save:
function onSave(bp)
if bp.Buf:FileType() == "go" then
gofmt(bp)
end
return true
end
Of course, you would want to change gofmt to whatever tool you are using for formatting (it should do the formatting in-place on the file), and replace the filetype check with the filetype you are formatting.
Anyone got this working for formatting php, html, c++ code?
Yes @Heart1010 ! I actually got HTML, JS, TS, SCSS, etc formatting with Prettier on save, thanks to @zyedidia 's example.
npm i -g prettier
nano ~/.config/micro/init.lua
Then pop this in:
local micro = import("micro")
local shell = import("micro/shell")
function gofmt(bp)
bp:Save()
local _, err = shell.RunCommand("prettier --write " .. bp.Buf.Path)
if err ~= nil then
micro.InfoBar():Error(_)
return
end
bp.Buf:ReOpen()
end
-- to run the formatter on every save:
function onSave(bp)
if bp.Buf:FileType() == "javascript"
or bp.Buf:FileType() == "jsx"
or bp.Buf:FileType() == "angular"
or bp.Buf:FileType() == "vue"
or bp.Buf:FileType() == "flow"
or bp.Buf:FileType() == "typescript"
or bp.Buf:FileType() == "css"
or bp.Buf:FileType() == "less"
or bp.Buf:FileType() == "scss"
or bp.Buf:FileType() == "html"
or bp.Buf:FileType() == "json"
or bp.Buf:FileType() == "graphql"
or bp.Buf:FileType() == "markdown"
or bp.Buf:FileType() == "yaml" then
gofmt(bp)
end
return true
end
This makes me wonder if we can have a plugin that handles code formatting? I created the plugin below to use python's black, based on go plugin. This makes me think of generalizing to a code formatter plugin, similar to lint. Unlike lint, there can only be one formatter per language as formatting modifies the code.
-- using python's black for format python code
local micro = import("micro")
local config = import("micro/config")
local shell = import("micro/shell")
local buffer = import("micro/buffer")
function init()
config.MakeCommand("pyblack", pyblack, config.NoComplete)
config.MakeCommand("pyblack-all", pyblack_all, config.NoComplete)
end
function pyblack(bp)
bp:Save()
local _, err = shell.RunCommand("python -m black " .. bp.Buf.Path)
if err ~= nil then
micro.InfoBar():Error(err)
return
end
bp.Buf:ReOpen()
end
function pyblack_all(bp)
bp:Save()
local dir = bp.Buf:DirName()
local _, err = shell.RunCommand("python -m black " .. dir)
if err ~= nil then
micro.InfoBar():Error(err)
return
end
bp.Buf:ReOpen()
end