From 567917bfc7bf8f58072c53818a1e85de9c0b6bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C5=BDdanov?= Date: Sat, 17 May 2025 12:27:27 +0300 Subject: [PATCH 1/8] feat(pack): add prettier pack --- lua/astrocommunity/pack/prettier/README.md | 11 ++ lua/astrocommunity/pack/prettier/init.lua | 140 +++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 lua/astrocommunity/pack/prettier/README.md create mode 100644 lua/astrocommunity/pack/prettier/init.lua diff --git a/lua/astrocommunity/pack/prettier/README.md b/lua/astrocommunity/pack/prettier/README.md new file mode 100644 index 000000000..2c74f7033 --- /dev/null +++ b/lua/astrocommunity/pack/prettier/README.md @@ -0,0 +1,11 @@ +# Prettier + +[Prettier](https://prettier.io/) is an opinionated code formatter with support for: + +JavaScript · TypeScript · Flow · JSX · JSON +CSS · SCSS · Less +HTML · Vue · Angular +GraphQL · Markdown · YAML +[Your favorite language?](https://prettier.io/docs/plugins) + +It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary. diff --git a/lua/astrocommunity/pack/prettier/init.lua b/lua/astrocommunity/pack/prettier/init.lua new file mode 100644 index 000000000..5b917c51f --- /dev/null +++ b/lua/astrocommunity/pack/prettier/init.lua @@ -0,0 +1,140 @@ +local format_filetypes = { + "javascript", + "javascriptreact", + "typescript", + "typescriptreact", +} + +local lsp_rooter_cache, prettierrc_rooter_cache + +local function get_lsp_rooter() + if not lsp_rooter_cache then + lsp_rooter_cache = require("astrocore.rooter").resolve("lsp", { + ignore = { + servers = function(client) + return not vim.tbl_contains({ + "eslint", + "ts_ls", + "typescript-tools", + "volar", + "vtsls", + }, client.name) + end, + }, + }) + end + return lsp_rooter_cache +end + +local function get_prettierrc_rooter() + if not prettierrc_rooter_cache then + prettierrc_rooter_cache = require("astrocore.rooter").resolve { + ".prettierrc", + ".prettierrc.cjs", + ".prettierrc.js", + ".prettierrc.json", + ".prettierrc.json5", + ".prettierrc.mjs", + ".prettierrc.toml", + ".prettierrc.yaml", + ".prettierrc.yml", + "prettier.config.cjs", + "prettier.config.js", + "prettier.config.mjs", + } + end + return prettierrc_rooter_cache +end + +local function decode_json(filename) + local file = io.open(filename, "r") + if not file then return false end + local content = file:read "*all" + file:close() + local ok, json = pcall(vim.fn.json_decode, content) + if not ok or type(json) ~= "table" then return false end + return json +end + +local function check_json_key_exists(json, ...) return vim.tbl_get(json, ...) ~= nil end + +local function has_prettier(bufnr) + if type(bufnr) ~= "number" then bufnr = vim.api.nvim_get_current_buf() end + + local prettier_dependency = false + + local lsp_rooter = get_lsp_rooter() + local search_roots = require("astrocore").list_insert_unique(lsp_rooter(bufnr) or {}, { vim.fn.getcwd() }) + + for _, root in ipairs(search_roots) do + local package_json_path = root .. "/package.json" + if vim.fn.filereadable(package_json_path) == 1 then + local package_json = decode_json(package_json_path) + if + package_json + and ( + check_json_key_exists(package_json, "dependencies", "prettier") + or check_json_key_exists(package_json, "devDependencies", "prettier") + ) + then + prettier_dependency = true + break + end + end + end + + local prettierrc_rooter = get_prettierrc_rooter() + + return prettier_dependency or (prettierrc_rooter(bufnr) and next(prettierrc_rooter(bufnr)) ~= nil) +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 + +---@type LazySpec +return { + { + "jay-babu/mason-null-ls.nvim", + optional = true, + opts = function(_, opts) + opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettierd" }) + if not opts.handlers then opts.handlers = {} end + + 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 = null_ls_formatter }) + end + end + end, + }, + { + "stevearc/conform.nvim", + optional = true, + 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, + }, + { + "WhoIsSethDaniel/mason-tool-installer.nvim", + optional = true, + opts = function(_, opts) + opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettierd" }) + end, + }, + { + "echasnovski/mini.icons", + optional = true, + opts = function(_, opts) + if not opts.file then opts.file = {} end + opts.file[".prettierrc"] = { glyph = "", hl = "MiniIconsPurple" } + end, + }, +} From a33100b8907e6c77c48c4fa8ae4fa435c79d440f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C5=BDdanov?= Date: Sun, 18 May 2025 10:05:32 +0300 Subject: [PATCH 2/8] add additional filetypes and config files --- lua/astrocommunity/pack/prettier/init.lua | 59 +++++++++++++++-------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/lua/astrocommunity/pack/prettier/init.lua b/lua/astrocommunity/pack/prettier/init.lua index 5b917c51f..2613cee3f 100644 --- a/lua/astrocommunity/pack/prettier/init.lua +++ b/lua/astrocommunity/pack/prettier/init.lua @@ -1,8 +1,21 @@ local format_filetypes = { + "css", + "graphql", + "handlebars", + "html", "javascript", "javascriptreact", + "json", + "json5", + "jsonc", + "less", + "markdown", + "markdown.mdx", + "scss", "typescript", "typescriptreact", + "vue", + "yaml", } local lsp_rooter_cache, prettierrc_rooter_cache @@ -26,23 +39,28 @@ local function get_lsp_rooter() return lsp_rooter_cache end +local prettier_files = { + ".prettierrc", + ".prettierrc.json", + ".prettierrc.json5", + ".prettierrc.yaml", + ".prettierrc.yml", + ".prettierrc.toml", + ".prettierrc.cjs", + ".prettierrc.js", + ".prettierrc.mjs", + ".prettierrc.ts", + ".prettierrc.mts", + ".prettierrc.cts", + "prettier.config.js", + "prettier.config.cjs", + "prettier.config.mjs", + "prettier.config.ts", + "prettier.config.mts", +} + local function get_prettierrc_rooter() - if not prettierrc_rooter_cache then - prettierrc_rooter_cache = require("astrocore.rooter").resolve { - ".prettierrc", - ".prettierrc.cjs", - ".prettierrc.js", - ".prettierrc.json", - ".prettierrc.json5", - ".prettierrc.mjs", - ".prettierrc.toml", - ".prettierrc.yaml", - ".prettierrc.yml", - "prettier.config.cjs", - "prettier.config.js", - "prettier.config.mjs", - } - end + if not prettierrc_rooter_cache then prettierrc_rooter_cache = require("astrocore.rooter").resolve(prettier_files) end return prettierrc_rooter_cache end @@ -83,9 +101,10 @@ local function has_prettier(bufnr) end end - local prettierrc_rooter = get_prettierrc_rooter() + if prettier_dependency then return true end - return prettier_dependency or (prettierrc_rooter(bufnr) and next(prettierrc_rooter(bufnr)) ~= nil) + local prettierrc_root = get_prettierrc_rooter()(bufnr) + return prettierrc_root and next(prettierrc_root) ~= nil end local null_ls_formatter = function(params) @@ -134,7 +153,9 @@ return { optional = true, opts = function(_, opts) if not opts.file then opts.file = {} end - opts.file[".prettierrc"] = { glyph = "", hl = "MiniIconsPurple" } + for _, filename in ipairs(prettier_files) do + opts.file[filename] = { glyph = "", hl = "MiniIconsPurple" } + end end, }, } From b78bc827ec6a69bf03638c804229bfcfda9db18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C5=BDdanov?= Date: Sun, 18 May 2025 12:29:20 +0300 Subject: [PATCH 3/8] use prettier instead of prettierd --- lua/astrocommunity/pack/prettier/init.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/astrocommunity/pack/prettier/init.lua b/lua/astrocommunity/pack/prettier/init.lua index 2613cee3f..42744153e 100644 --- a/lua/astrocommunity/pack/prettier/init.lua +++ b/lua/astrocommunity/pack/prettier/init.lua @@ -112,7 +112,7 @@ local null_ls_formatter = function(params) return true end -local conform_formatter = function(bufnr) return has_prettier(bufnr) and { "prettierd" } or {} end +local conform_formatter = function(bufnr) return has_prettier(bufnr) and { "prettier" } or {} end ---@type LazySpec return { @@ -120,10 +120,10 @@ return { "jay-babu/mason-null-ls.nvim", optional = true, opts = function(_, opts) - opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettierd" }) + opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettier" }) if not opts.handlers then opts.handlers = {} end - opts.handlers.prettierd = function(source_name, methods) + opts.handlers.prettier = 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 = null_ls_formatter }) @@ -145,7 +145,7 @@ return { "WhoIsSethDaniel/mason-tool-installer.nvim", optional = true, opts = function(_, opts) - opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettierd" }) + opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettier" }) end, }, { From b008083a0f5587af0588c88f8d14df0482b647b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C5=BDdanov?= Date: Tue, 20 May 2025 06:48:54 +0300 Subject: [PATCH 4/8] Revert using prettier instead of prettierd It would be better to do this in a separate PR --- lua/astrocommunity/pack/prettier/init.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/astrocommunity/pack/prettier/init.lua b/lua/astrocommunity/pack/prettier/init.lua index 42744153e..2613cee3f 100644 --- a/lua/astrocommunity/pack/prettier/init.lua +++ b/lua/astrocommunity/pack/prettier/init.lua @@ -112,7 +112,7 @@ local null_ls_formatter = function(params) return true end -local conform_formatter = function(bufnr) return has_prettier(bufnr) and { "prettier" } or {} end +local conform_formatter = function(bufnr) return has_prettier(bufnr) and { "prettierd" } or {} end ---@type LazySpec return { @@ -120,10 +120,10 @@ return { "jay-babu/mason-null-ls.nvim", optional = true, opts = function(_, opts) - opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettier" }) + opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettierd" }) if not opts.handlers then opts.handlers = {} end - opts.handlers.prettier = function(source_name, methods) + 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 = null_ls_formatter }) @@ -145,7 +145,7 @@ return { "WhoIsSethDaniel/mason-tool-installer.nvim", optional = true, opts = function(_, opts) - opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettier" }) + opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettierd" }) end, }, { From 3696dcb864d2c43cac8e15fa857f07279af8a5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C5=BDdanov?= Date: Tue, 1 Jul 2025 06:21:24 +0300 Subject: [PATCH 5/8] inline variables --- lua/astrocommunity/pack/prettier/init.lua | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lua/astrocommunity/pack/prettier/init.lua b/lua/astrocommunity/pack/prettier/init.lua index 2613cee3f..fd0983cd0 100644 --- a/lua/astrocommunity/pack/prettier/init.lua +++ b/lua/astrocommunity/pack/prettier/init.lua @@ -107,13 +107,6 @@ local function has_prettier(bufnr) return prettierrc_root and next(prettierrc_root) ~= nil 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 - ---@type LazySpec return { { @@ -126,7 +119,12 @@ 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 = null_ls_formatter }) + null_ls.register(null_ls.builtins[method][source_name].with { + runtime_condition = function(params) + if vim.tbl_contains(format_filetypes, params.filetype) then return has_prettier(params.bufnr) end + return true + end, + }) end end end, @@ -137,7 +135,7 @@ return { 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 + opts.formatters_by_ft[filetype] = function(bufnr) return has_prettier(bufnr) and { "prettierd" } or {} end end end, }, From c524bff26c02a5ef2ce7e588701204678fa3d6a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C5=BDdanov?= Date: Tue, 1 Jul 2025 06:24:12 +0300 Subject: [PATCH 6/8] remove cache variables --- lua/astrocommunity/pack/prettier/init.lua | 36 +++++++++-------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/lua/astrocommunity/pack/prettier/init.lua b/lua/astrocommunity/pack/prettier/init.lua index fd0983cd0..b148eddfc 100644 --- a/lua/astrocommunity/pack/prettier/init.lua +++ b/lua/astrocommunity/pack/prettier/init.lua @@ -18,25 +18,20 @@ local format_filetypes = { "yaml", } -local lsp_rooter_cache, prettierrc_rooter_cache - local function get_lsp_rooter() - if not lsp_rooter_cache then - lsp_rooter_cache = require("astrocore.rooter").resolve("lsp", { - ignore = { - servers = function(client) - return not vim.tbl_contains({ - "eslint", - "ts_ls", - "typescript-tools", - "volar", - "vtsls", - }, client.name) - end, - }, - }) - end - return lsp_rooter_cache + return require("astrocore.rooter").resolve("lsp", { + ignore = { + servers = function(client) + return not vim.tbl_contains({ + "eslint", + "ts_ls", + "typescript-tools", + "volar", + "vtsls", + }, client.name) + end, + }, + }) end local prettier_files = { @@ -59,10 +54,7 @@ local prettier_files = { "prettier.config.mts", } -local function get_prettierrc_rooter() - if not prettierrc_rooter_cache then prettierrc_rooter_cache = require("astrocore.rooter").resolve(prettier_files) end - return prettierrc_rooter_cache -end +local function get_prettierrc_rooter() return require("astrocore.rooter").resolve(prettier_files) end local function decode_json(filename) local file = io.open(filename, "r") From 83e5d1550d3e32665c4d3ab91abbd618041c81e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C5=BDdanov?= Date: Tue, 1 Jul 2025 06:36:34 +0300 Subject: [PATCH 7/8] early return if prettier found --- lua/astrocommunity/pack/prettier/init.lua | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lua/astrocommunity/pack/prettier/init.lua b/lua/astrocommunity/pack/prettier/init.lua index b148eddfc..7d8241d55 100644 --- a/lua/astrocommunity/pack/prettier/init.lua +++ b/lua/astrocommunity/pack/prettier/init.lua @@ -71,8 +71,6 @@ local function check_json_key_exists(json, ...) return vim.tbl_get(json, ...) ~= local function has_prettier(bufnr) if type(bufnr) ~= "number" then bufnr = vim.api.nvim_get_current_buf() end - local prettier_dependency = false - local lsp_rooter = get_lsp_rooter() local search_roots = require("astrocore").list_insert_unique(lsp_rooter(bufnr) or {}, { vim.fn.getcwd() }) @@ -87,14 +85,11 @@ local function has_prettier(bufnr) or check_json_key_exists(package_json, "devDependencies", "prettier") ) then - prettier_dependency = true - break + return true end end end - if prettier_dependency then return true end - local prettierrc_root = get_prettierrc_rooter()(bufnr) return prettierrc_root and next(prettierrc_root) ~= nil end From 7c13e1c4c515e8eb58dc0bb5be3c31263d43eed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C5=BDdanov?= Date: Fri, 11 Jul 2025 13:55:10 +0300 Subject: [PATCH 8/8] simplify config --- lua/astrocommunity/pack/prettier/init.lua | 153 ++++++---------------- 1 file changed, 41 insertions(+), 112 deletions(-) diff --git a/lua/astrocommunity/pack/prettier/init.lua b/lua/astrocommunity/pack/prettier/init.lua index 7d8241d55..5f07403d0 100644 --- a/lua/astrocommunity/pack/prettier/init.lua +++ b/lua/astrocommunity/pack/prettier/init.lua @@ -1,99 +1,3 @@ -local format_filetypes = { - "css", - "graphql", - "handlebars", - "html", - "javascript", - "javascriptreact", - "json", - "json5", - "jsonc", - "less", - "markdown", - "markdown.mdx", - "scss", - "typescript", - "typescriptreact", - "vue", - "yaml", -} - -local function get_lsp_rooter() - return require("astrocore.rooter").resolve("lsp", { - ignore = { - servers = function(client) - return not vim.tbl_contains({ - "eslint", - "ts_ls", - "typescript-tools", - "volar", - "vtsls", - }, client.name) - end, - }, - }) -end - -local prettier_files = { - ".prettierrc", - ".prettierrc.json", - ".prettierrc.json5", - ".prettierrc.yaml", - ".prettierrc.yml", - ".prettierrc.toml", - ".prettierrc.cjs", - ".prettierrc.js", - ".prettierrc.mjs", - ".prettierrc.ts", - ".prettierrc.mts", - ".prettierrc.cts", - "prettier.config.js", - "prettier.config.cjs", - "prettier.config.mjs", - "prettier.config.ts", - "prettier.config.mts", -} - -local function get_prettierrc_rooter() return require("astrocore.rooter").resolve(prettier_files) end - -local function decode_json(filename) - local file = io.open(filename, "r") - if not file then return false end - local content = file:read "*all" - file:close() - local ok, json = pcall(vim.fn.json_decode, content) - if not ok or type(json) ~= "table" then return false end - return json -end - -local function check_json_key_exists(json, ...) return vim.tbl_get(json, ...) ~= nil end - -local function has_prettier(bufnr) - if type(bufnr) ~= "number" then bufnr = vim.api.nvim_get_current_buf() end - - local lsp_rooter = get_lsp_rooter() - local search_roots = require("astrocore").list_insert_unique(lsp_rooter(bufnr) or {}, { vim.fn.getcwd() }) - - for _, root in ipairs(search_roots) do - local package_json_path = root .. "/package.json" - if vim.fn.filereadable(package_json_path) == 1 then - local package_json = decode_json(package_json_path) - if - package_json - and ( - check_json_key_exists(package_json, "dependencies", "prettier") - or check_json_key_exists(package_json, "devDependencies", "prettier") - ) - then - return true - end - end - end - - local prettierrc_root = get_prettierrc_rooter()(bufnr) - return prettierrc_root and next(prettierrc_root) ~= nil -end - ---@type LazySpec return { { @@ -101,19 +5,6 @@ return { optional = true, opts = function(_, opts) opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed or {}, { "prettierd" }) - if not opts.handlers then opts.handlers = {} end - - 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 = function(params) - if vim.tbl_contains(format_filetypes, params.filetype) then return has_prettier(params.bufnr) end - return true - end, - }) - end - end end, }, { @@ -121,8 +12,28 @@ return { optional = true, 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] = function(bufnr) return has_prettier(bufnr) and { "prettierd" } or {} end + for _, filetype in ipairs { + "javascript", + "javascriptreact", + "typescript", + "typescriptreact", + "vue", + "css", + "scss", + "less", + "html", + "json", + "jsonc", + "yaml", + "markdown", + "markdown.mdx", + "graphql", + "handlebars", + "svelte", + "astro", + "htmlangular", + } do + opts.formatters_by_ft[filetype] = { "prettierd" } end end, }, @@ -138,7 +49,25 @@ return { optional = true, opts = function(_, opts) if not opts.file then opts.file = {} end - for _, filename in ipairs(prettier_files) do + for _, filename in ipairs { + ".prettierrc", + ".prettierrc.cjs", + ".prettierrc.cts", + ".prettierrc.js", + ".prettierrc.json", + ".prettierrc.json5", + ".prettierrc.mjs", + ".prettierrc.mts", + ".prettierrc.toml", + ".prettierrc.ts", + ".prettierrc.yaml", + ".prettierrc.yml", + "prettier.config.cjs", + "prettier.config.js", + "prettier.config.mjs", + "prettier.config.mts", + "prettier.config.ts", + } do opts.file[filename] = { glyph = "", hl = "MiniIconsPurple" } end end,