micro icon indicating copy to clipboard operation
micro copied to clipboard

[QUESTION] Formatting/beautifying php, js, html,...

Open ctuxboy opened this issue 6 years ago • 5 comments

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).

ctuxboy avatar Oct 21 '20 10:10 ctuxboy

Have the same question, formatting is very important and fmt stopped supporting anything that is over v2.0

jb2328 avatar Nov 18 '20 15:11 jb2328

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.

zyedidia avatar Nov 18 '20 18:11 zyedidia

Anyone got this working for formatting php, html, c++ code?

Heart1010 avatar Jun 01 '21 12:06 Heart1010

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

entozoon avatar Dec 01 '21 22:12 entozoon

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

OnesimusUnbound avatar Dec 10 '25 06:12 OnesimusUnbound