From d3455ad8eee6a940f9fe8a5222d3339c7ab6b4f1 Mon Sep 17 00:00:00 2001 From: Walter Jenkins Date: Sat, 16 Aug 2025 05:17:28 -0500 Subject: [PATCH] fix issue with templ files --- .prettierrc.json | 13 +++++++++++ init.lua | 8 +++---- lua/plugins/conform.lua | 50 ++++++++++++++++++++++++++++++++--------- lua/plugins/lsp.lua | 12 +++++----- lua/plugins/none-ls.lua | 8 ++++++- 5 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 .prettierrc.json diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 00000000..8114bf3d --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,13 @@ +{ + "printWidth": 120, + "tabWidth": 2, + "useTabs": false, + "semi": true, + "singleQuote": false, + "quoteProps": "as-needed", + "trailingComma": "es5", + "bracketSpacing": true, + "bracketSameLine": false, + "arrowParens": "avoid", + "plugins": ["prettier-plugin-go-template"] +} diff --git a/init.lua b/init.lua index 1cfb0a54..e2213cec 100644 --- a/init.lua +++ b/init.lua @@ -98,10 +98,10 @@ end local session_file = '.session.vim' -- Check if the session file exists in the current directory -if file_exists(session_file) then - -- Source the session file - vim.cmd('source ' .. session_file) -end +-- if file_exists(session_file) then +-- -- Source the session file +-- vim.cmd('source ' .. session_file) +-- end -- The line beneath this is called `modeline`. See `:help modeline` -- vim: ts=2 sts=2 sw=2 et diff --git a/lua/plugins/conform.lua b/lua/plugins/conform.lua index a538f2c3..44db35ee 100644 --- a/lua/plugins/conform.lua +++ b/lua/plugins/conform.lua @@ -1,21 +1,49 @@ +-- lua/plugins/conform.lua return { { "stevearc/conform.nvim", + event = { "BufReadPre", "BufNewFile" }, config = function() - require("conform").setup({ - formatters_by_ft = { - -- Disable formatters for .templ files - templ = {}, + -- Resolve templ binary (Mason first, else PATH) + local mason_templ = vim.fn.stdpath("data") .. "/mason/bin/templ" + local templ_cmd = (vim.fn.executable(mason_templ) == 1) and mason_templ + or ((vim.fn.executable("templ") == 1) and "templ" or nil) - -- Add your other filetype configs here as needed - -- e.g., go = { "gofmt" }, + if not templ_cmd then + vim.notify("[conform.nvim] Could not find `templ` binary. Install via Mason or PATH.", vim.log.levels.WARN) + end + + require("conform").setup({ + formatters = { + templ_fmt = { + command = templ_cmd or "templ", + -- Read source from stdin, tell templ the filename for correct rules, + -- and write formatted result to stdout (no in-place writes). + args = { "fmt", "-stdin-filepath", "$FILENAME", "-stdout" }, + stdin = true, + }, }, - format_on_save = { - timeout_ms = 500, - lsp_fallback = true, + formatters_by_ft = { + templ = { "templ_fmt" }, -- ✅ only templ fmt for .templ + javascript = { "prettier" }, + typescript = { "prettier" }, + jsx = { "prettier" }, + tsx = { "prettier" }, + json = { "prettier" }, + yaml = { "prettier" }, + markdown = { "prettier" }, + html = { "prettier" }, + css = { "prettier" }, + lua = { "stylua" }, + go = { "gofmt" }, }, + format_on_save = function(bufnr) + if vim.bo[bufnr].filetype == "templ" then + return { timeout_ms = 2000, lsp_fallback = false } -- no LSP/Prettier fallback + end + return { timeout_ms = 1000, lsp_fallback = true } + end, }) end, - event = { "BufReadPre", "BufNewFile" }, - } + }, } diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 2a1b4f05..3af35dee 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -77,7 +77,7 @@ return { analyses = { unusedparams = true }, directoryFilters = { '-node_modules' }, templ = { - format = true, + format = false, -- DISABLED for debugging lint = true, }, }, @@ -94,11 +94,11 @@ return { }, eslint = {}, html = { filetypes = { 'html', 'twig', 'hbs' } }, - templ = { - cmd = { vim.fn.stdpath("data") .. "/mason/bin/templ", "lsp" }, - filetypes = { "templ" }, - root_dir = require("lspconfig").util.root_pattern("go.mod", ".git"), - }, + -- templ = { -- DISABLED for debugging + -- cmd = { vim.fn.stdpath("data") .. "/mason/bin/templ", "lsp" }, + -- filetypes = { "templ" }, + -- root_dir = require("lspconfig").util.root_pattern("go.mod", ".git"), + -- }, lua_ls = { settings = { Lua = { diff --git a/lua/plugins/none-ls.lua b/lua/plugins/none-ls.lua index fc4ee0ce..94f5a65f 100644 --- a/lua/plugins/none-ls.lua +++ b/lua/plugins/none-ls.lua @@ -27,7 +27,7 @@ return { local sources = { diagnostics.checkmake, - formatting.prettier.with { filetypes = { 'html', 'json', 'yaml', 'markdown' } }, + formatting.prettier.with { filetypes = { 'html', 'json', 'yaml', 'markdown' } }, -- removed 'templ' for debugging formatting.stylua, formatting.shfmt.with { args = { '-i', '4' } }, require('none-ls.formatting.ruff').with { extra_args = { '--extend-select', 'I' } }, @@ -41,6 +41,12 @@ return { -- you can reuse a shared lspconfig on_attach callback here on_attach = function(client, bufnr) if client.supports_method 'textDocument/formatting' then + -- Skip formatting for .templ files during debugging + local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype') + if filetype == 'templ' then + return + end + vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr } vim.api.nvim_create_autocmd('BufWritePre', { group = augroup,