Skip to content

Instantly share code, notes, and snippets.

@PuercoPop
Last active December 18, 2017 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PuercoPop/233c68a8f1243878d80daa043ed7826c to your computer and use it in GitHub Desktop.
Save PuercoPop/233c68a8f1243878d80daa043ed7826c to your computer and use it in GitHub Desktop.
m = {}
m.prettier_command = "npx prettier"
m.prettier_args = " "
function format_prettier()
local file = vis.win.file
local cmd = m.prettier_command .. m.prettier_args .. file.path
-- Range should default to selection if present:
local status, out, err = vis:pipe(file, { start = 0, finish = 0}, cmd)
vis:info(status)
if status ~= 0 then
-- vis:info(err)
else
file:delete(0, file.size)
file:insert(0, out)
end
end
vis:map(vis.modes.NORMAL, "=", format_prettier, "format_prettier")
vis.events.subscribe(vis.events.FILE_SAVE_PRE, function (file, path)
if not string.match(file.path, "%.js$") then return true end
local cmd = m.prettier_command .. m.prettier_args .. file.path
-- Range should default to selection if present:
local status, out, err = vis:pipe(file, { start = 0, finish = 0}, cmd)
if status ~= 0 then
vis:info(err)
return status
end
file:delete(0, file.size)
file:insert(0, out)
return true
end)
return m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment