Skip to content

Commit

Permalink
fix(typescript): only check for prettier dependency for JavaScript …
Browse files Browse the repository at this point in the history
…or TypeScript files
  • Loading branch information
mehalter committed May 14, 2024
1 parent 3f9e967 commit 5c186d9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lua/astrocommunity/pack/typescript/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ local function decode_json(filename)
return json
end

local format_filetypes = { "javascript", "javascriptreact", "typescript", "typescriptreact" }

local function check_json_key_exists(json, ...) return vim.tbl_get(json, ...) ~= nil end
local lsp_rooter, prettierrc_rooter
local has_prettier = function(bufnr)
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
if type(bufnr) ~= "number" then bufnr = vim.api.nvim_get_current_buf() end
local rooter = require "astrocore.rooter"
if not lsp_rooter then lsp_rooter = rooter.resolve "lsp" end
Expand Down Expand Up @@ -57,6 +58,10 @@ local has_prettier = function(bufnr)
return prettier_dependency or next(prettierrc_rooter(bufnr))
end

local null_ls_formatter = function(params)
if vim.tbl_contains(format_filetypes, params.filetype) then return has_prettier(params.bufnr) end
return true
end
local conform_formatter = function(bufnr) return has_prettier(bufnr) and { "prettierd" } or {} end

return {
Expand Down Expand Up @@ -88,22 +93,20 @@ return {
opts.handlers.prettierd = function(source_name, methods)
local null_ls = require "null-ls"
for _, method in ipairs(methods) do
null_ls.register(null_ls.builtins[method][source_name].with { runtime_condition = has_prettier })
null_ls.register(null_ls.builtins[method][source_name].with { runtime_condition = null_ls_formatter })
end
end
end,
},
{
"stevearc/conform.nvim",
optional = true,
opts = {
formatters_by_ft = {
typescriptreact = conform_formatter,
typescript = conform_formatter,
javascriptreact = conform_formatter,
javascript = conform_formatter,
},
},
opts = function(_, opts)
if not opts.formatters_by_ft then opts.formatters_by_ft = {} end
for _, filetype in ipairs(format_filetypes) do
opts.formatters_by_ft[filetype] = conform_formatter
end
end,
},
{
"jay-babu/mason-nvim-dap.nvim",
Expand Down

0 comments on commit 5c186d9

Please sign in to comment.