From 1c52cb4ac06f0b7484d3aa58075d48b0b4dc74df Mon Sep 17 00:00:00 2001 From: Charles Danesi Date: Sat, 4 Oct 2025 19:22:31 -0400 Subject: [PATCH] chore: improve Markdown and codeblock formatting - Update `prettier.lua` to handle injected code blocks with correct `lang_to_ft` and `lang_to_ext` mappings - Remove deprecated `markdown_inline` filetype; rely on injected formatting instead - Update `linting.lua` to match new configuration - Remove unused formatting and linting tools --- .config/nvim/lua/plugins/linting.lua | 47 ++++++---- .config/nvim/lua/plugins/prettier.lua | 118 +++++++++++++++++++++----- 2 files changed, 128 insertions(+), 37 deletions(-) diff --git a/.config/nvim/lua/plugins/linting.lua b/.config/nvim/lua/plugins/linting.lua index 7bfc288..4467686 100644 --- a/.config/nvim/lua/plugins/linting.lua +++ b/.config/nvim/lua/plugins/linting.lua @@ -6,20 +6,37 @@ return { local lint = require('lint') lint.linters_by_ft = { - -- markdown = { 'markdownlint' }, - -- html = { 'htmlhint' }, - -- json = { 'jsonlint' }, - -- sh = { 'shellharden', 'shellcheck' }, - -- bash = { 'shellharden', 'shellcheck' }, - -- javascript = { 'eslint_d', 'trivy' }, - -- typescript = { 'eslint_d' }, - -- python = { 'pylint', 'trivy' }, - -- ansible = { 'ansiblelint' }, - -- gitcommit = { 'gitlint' }, - -- docker = { 'trivy' }, - -- yaml = { 'trivy', 'yamllint' }, - -- editorconfig = { 'editorconfig-checker' }, - -- systemd = { 'systemdlint' }, + -- Lua + lua = {}, + + -- Shell / Bash / Zsh + sh = { 'shellcheck' }, + bash = { 'shellcheck' }, + + -- Markdown + markdown = { 'markdownlint' }, + -- markdown_inline = { 'markdownlint' }, + + -- Web / Frontend + html = { 'htmlhint' }, + css = { 'stylelint' }, + javascript = { 'eslint_d' }, + php = { 'trivy' }, + + -- YAML + yaml = { 'yamllint' }, + + -- JSON + json = { 'jsonlint', 'jq' }, + + -- Python + python = { 'pylint' }, + + -- Git commit messages + gitcommit = { 'gitlint' }, + + -- SQL + sql = { 'sqlfluff' }, } local lint_augroup = vim.api.nvim_create_augroup('lint', { clear = true }) @@ -31,4 +48,4 @@ return { end, }) end, -} \ No newline at end of file +} diff --git a/.config/nvim/lua/plugins/prettier.lua b/.config/nvim/lua/plugins/prettier.lua index 038201b..5cd868b 100644 --- a/.config/nvim/lua/plugins/prettier.lua +++ b/.config/nvim/lua/plugins/prettier.lua @@ -1,35 +1,109 @@ +--[[ vim.api.nvim_create_autocmd({ 'FocusLost', 'BufLeave' }, { + pattern = '*', + callback = function(args) + local buf = args.buf or vim.api.nvim_get_current_buf() + if vim.fn.mode() == 'n' then + vim.defer_fn(function() + if vim.api.nvim_buf_is_valid(buf) then require('conform').format({ bufnr = buf }) end + end, 100) + end + end, +}) ]] + return { 'stevearc/conform.nvim', - event = { 'BufReadPre', 'BufNewFile' }, + event = { 'BufWritePre' }, opts = { formatters_by_ft = { - sh = { 'beautysh', 'shellharden' }, - bash = { 'beautysh', 'shellharden' }, - zsh = { 'beautysh' }, - javascript = { 'prettier' }, - typescript = { 'prettier' }, - css = { 'prettier' }, + -- Lua + lua = { 'lsp_format', 'stylua' }, + + -- Shell / Bash / Zsh + sh = { 'beautysh' }, + bash = { 'beautysh' }, + -- zsh = { 'beautysh' }, -- beautysh pisses me off with zsh files + + -- Markdown + markdown = { 'prettier', 'injected' }, + -- markdown_inline = { 'prettier', 'injected' }, + + -- Web / Frontend html = { 'prettier' }, - json = { 'prettier' }, - yaml = { 'prettier' }, - markdown = { 'prettier', 'markdownlint' }, - graphql = { 'prettier' }, - liquid = { 'prettier' }, - lua = { 'stylua' }, - python = { 'black', 'sort' }, - }, - formatters = { - --[[ shfmt = { - prepend_args = { '-i', '6' }, - }, ]] + css = { 'prettier', 'stylelint' }, + javascript = { 'prettier', 'eslint_d' }, + php = { 'phpcbf' }, + + -- YAML + yaml = { 'prettier', 'yamlfmt' }, + + -- JSON / Config + json = { 'prettier', 'fixjson' }, + + -- Python + python = { 'black', 'isort' }, + + -- SQL + sql = { 'sqlfluff' }, }, + format_injected = true, default_format_opts = { lsp_format = 'fallback', }, - format_on_save = { + format_after_save = { lsp_format = 'fallback', - async = false, timeout_ms = 1000, }, }, -} \ No newline at end of file + + config = function(_, opts) + local conform = require('conform') + conform.setup(opts) + + -- Configure injected formatter + conform.formatters.injected = { + options = { + ignore_errors = false, + lang_to_ft = { + sh = 'sh', + bash = 'sh', + zsh = 'sh', + lua = 'lua', + javascript = 'javascript', + json = 'json', + yaml = 'yaml', + markdown = 'markdown', + python = 'python', + html = 'html', + css = 'css', + toml = 'toml', + sql = 'sql', + php = 'php', + }, + -- Map injected languages to the right formatter(s) + lang_to_ext = { + sh = 'sh', + bash = 'sh', + zsh = 'sh', + javascript = 'js', + markdown = 'md', + json = 'json', + yaml = 'yml', + python = 'py', + lua = 'lua', + html = 'html', + css = 'css', + toml = 'toml', + sql = 'sql', + php = 'php', + }, + + --[[ lang_to_formatters = { + sh = { 'beautysh' }, + bash = { 'beautysh'}, + javascript = {'prettier'}, + json = {'prettier'}, + }, ]] + }, + } + end, +}