diff --git a/lua/guard-collection/formatter.lua b/lua/guard-collection/formatter.lua index 51a8491..65ea523 100644 --- a/lua/guard-collection/formatter.lua +++ b/lua/guard-collection/formatter.lua @@ -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' }, diff --git a/test/formatter/helper.lua b/test/formatter/helper.lua index cbc3a46..28292fd 100644 --- a/test/formatter/helper.lua +++ b/test/formatter/helper.lua @@ -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) diff --git a/test/formatter/prettierd_spec.lua b/test/formatter/prettierd_spec.lua new file mode 100644 index 0000000..6a93ad0 --- /dev/null +++ b/test/formatter/prettierd_spec.lua @@ -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)