Skip to content

Commit

Permalink
feat: support custom formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Aug 24, 2022
1 parent fca744c commit 51075e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
38 changes: 37 additions & 1 deletion lua/prettier/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
local options = require("prettier.options")
local null_ls = require("prettier.null-ls")

local M = {}
local M = {
__ = {},
}

function M.setup(user_options)
options.setup(user_options)
Expand All @@ -14,4 +16,38 @@ function M.format(method)
null_ls.format(method)
end

function M.create_formatter(opts)
local command = opts.command

M.__[command] = {
_fn = function(method)
if M.__[command].fn then
return M.__[command].fn(method)
end
end,
cmd = function(range)
if range > 0 then
M.__[command]._fn("textDocument/rangeFormatting")
else
M.__[command]._fn("textDocument/formatting")
end
end,
}

vim.schedule(function()
local format = null_ls.create_formatter({
bin = opts.bin,
bin_preference = opts.bin_preference,
cli_options = opts.cli_options,
["null-ls"] = opts["null-ls"],
})

M.__[command].fn = format

vim.cmd(string.format([[command! -range=%% %s :lua require("prettier").__["%s"].cmd(<range>)]], command, command))
end)

return M.__[command]._fn
end

return M
4 changes: 2 additions & 2 deletions lua/prettier/null-ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ local function create_generator(opts)
})
end

local function create_formatter(opts)
function M.create_formatter(opts)
if not ok then
return noop
end
Expand Down Expand Up @@ -161,7 +161,7 @@ local function create_formatter(opts)
end

function M.setup()
M.format = create_formatter({
M.format = M.create_formatter({
bin = options.get("bin"),
cli_options = options.get("cli_options"),
["null-ls"] = {
Expand Down

0 comments on commit 51075e9

Please sign in to comment.