VERSION = "1.0.0" -- local linter = import("linter") local micro = import("micro") local config = import("micro/config") local buffer = import("micro/buffer") function os_cmd(cmd) local f = assert(io.popen(cmd, 'r')) local s = assert(f:read('*a')) f:close() return s end function getTextLoc(c) local v = micro.CurPane() local a, b = nil, nil if c:HasSelection() then if c.CurSelection[1]:GreaterThan(-c.CurSelection[2]) then a, b = c.CurSelection[2], c.CurSelection[1] else a, b = c.CurSelection[1], c.CurSelection[2] end else local eol = string.len(v.Buf:Line(c.Loc.Y)) a, b = c.Loc, buffer.Loc(eol, c.Y) end return buffer.Loc(a.X, a.Y), buffer.Loc(b.X, b.Y) end -- Returns the current marked text or whole line function getText(a, b) local txt, buf = {}, micro.CurPane().Buf -- Editing a single line? if a.Y == b.Y then return buf:Line(a.Y):sub(a.X + 1, b.X) end -- Add first part of text selection (a.X+1 as Lua is 1-indexed) table.insert(txt, buf:Line(a.Y):sub(a.X + 1)) -- Stuff in the middle for lineNo = a.Y + 1, b.Y - 1 do table.insert(txt, buf:Line(lineNo)) end -- Insert last part of selection table.insert(txt, buf:Line(b.Y):sub(1, b.X)) return table.concat(txt, "\n") end function insertate(insertator) local num = math.inf or num local v = micro.CurPane() -- perform the replacement for each cursor local cs = v.Buf:GetCursors() for i = 1, #cs do local c = cs[i] local a, b = getTextLoc(c) local oldTxt = getText(a, b) local newTxt = insertator() local sel = nil if c:HasSelection() then -- copied from comment plugin sel = -c.CurSelection end v.Buf:Replace(a, b, newTxt) -- Fix selection, if transformation changes text length (e.g. base64) local d = string.len(newTxt) - string.len(oldTxt) if sel ~= nil then -- copied from comment plugin c.CurSelection[1].Y = sel[1].Y c.CurSelection[2].Y = sel[2].Y c.CurSelection[1].X = sel[1].X c.CurSelection[2].X = sel[2].X if c.CurSelection[1]:GreaterThan(-c.CurSelection[2]) then c.CurSelection[1].X = sel[1].X + d else c.CurSelection[2].X = sel[2].X + d end end c:Relocate() c.LastVisualX = c:GetVisualX() end end function uuid() insertate(function() return os_cmd('uuid -v4 | tr -d "\n"') end) end function init() -- linter.makeLinter("phpmd.phar", "php", "phpmd.phar", {"%f", "text", "phpmd.xml"}, "%f:%l %m") linter.makeLinter("phpstan", "php", "phpstan", {"--memory-limit=2G", "--level=9", "--error-format=raw", "analyze", "--", "%f"}, "%f:%l:%m") -- linter.makeLinter("phpl", "php", "phpl", {"%f"}, "%m %l") -- linter.makeLinter("phpl", "php", "phpl", {"%f"}, "%m in %f on line %l") linter.makeLinter("ruff", "python", "ruff", {"check", "--output-format=concise", "%f"}, "%f:%l:%c: %m") config.MakeCommand("uuid", uuid, config.NoComplete) end