Skip to content

Commit

Permalink
feat: adds prettierd
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoshihou514 committed Jun 6, 2024
1 parent 25707ef commit f452486
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
26 changes: 26 additions & 0 deletions lua/guard-collection/formatter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ M.prettier = {
stdin = true,
}

M.prettierd = {
fn = function(buf, range)
if vim.fn.has('nvim-0.10') ~= 1 then
vim.notify('[guard-collection]: prettierd uses vim.system introduced in nvim 0.10', 4)
return
end
local srow = range and range['start'][1] or 0
local erow = range and range['end'][1] or -1
local handle = vim.system(
{ 'prettierd', vim.api.nvim_buf_get_name(buf) },
{
stdin = true,
},
vim.schedule_wrap(function(result)
if result.code ~= 0 then
return
end
vim.api.nvim_buf_set_lines(buf, srow, erow, false, vim.split(result.stdout, '\r?\n'))
vim.cmd('silent! noautocmd write!')
end)
)
handle:write(table.concat(vim.api.nvim_buf_get_lines(buf, srow, erow, false), '\n'))
handle:write(nil)
end,
}

M.rubocop = {
cmd = 'bundle',
args = { 'exec', 'rubocop', '-A', '-f', 'quiet', '--stderr', '--stdin' },
Expand Down
2 changes: 1 addition & 1 deletion test/formatter/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local api = vim.api

function M.test_with(ft, input)
local cmd = require('guard.filetype')(ft).formatter[1].cmd
assert(vim.fn.executable(cmd) == 1)
assert(not cmd or vim.fn.executable(cmd) == 1)
local bufnr = api.nvim_create_buf(true, false)
vim.bo[bufnr].filetype = ft
api.nvim_set_current_buf(bufnr)
Expand Down
19 changes: 19 additions & 0 deletions test/formatter/prettierd_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
describe('prettierd', function()
it('can format', function()
local ft = require('guard.filetype')
ft('javascript'):fmt('prettierd')
require('guard').setup()

local formatted = require('test.formatter.helper').test_with('javascript', {
[[ const randomNumber = Math.floor(]],
[[ Math.random() * 10]],
[[ ) + 1]],
[[alert(randomNumber)]],
})
assert.are.same({
[[const randomNumber = Math.floor(Math.random() * 10) + 1;]],
[[alert(randomNumber);]],
[[]],
}, formatted)
end)
end)

0 comments on commit f452486

Please sign in to comment.