7e90c32

Branches > main > 7e90c32 > files

Nvim - Format on write via LSP

Grahp, 2026-10-25

Commit: 7e90c32d53ed916d0035a8594be8e6dfc6093382
Parent: ada9461

3 changed files:

nvim/init.lua modified (view file)

@@ -14,6 +14,7 @@ vim.schedule(function()   require("keybindings")   require("lsp")+  require("format")   require("lazy_ui") end) 

nvim/lua/format.lua modified (view file)

@@ -1,12 +1,19 @@ local ac = vim.api.nvim_create_autocmd local ag = vim.api.nvim_create_augroup -local function format(cmd)-  vim.schedule(function() vim.cmd("silent !" .. cmd .. " %") end)+--- Format with the LSP before writing (clojure-lsp uses cljfmt under the hood)+--- Disable with `:let b:format_on_write = v:false` (or g: for everything)+local function enabled(bufnr)+  local b = vim.b[bufnr].format_on_write+  if b ~= nil then return b end+  return vim.g.format_on_write ~= false end  local format_group = ag("Format", {clear = true})-ac("BufWritePost",-   {callback = function() format("cljfmt fix") end,-    group = format_group,-    pattern = "*.clj"})+ac("BufWritePre",+   {callback = function(args)+      if not enabled(args.buf) then return end+      if #vim.lsp.get_clients({bufnr = args.buf, method = "textDocument/formatting"}) == 0 then return end+      vim.lsp.buf.format({bufnr = args.buf, timeout_ms = 2000})+    end,+    group = format_group})

nvim/lua/lsp.lua modified (view file)

@@ -43,7 +43,8 @@  config({name = "lua-language-server",         filetypes = {"lua"},-        settings = {Lua = {diagnostics = {globals = {"vim"}}}},+        --- Its formatter fights my style, so don't format lua on write+        settings = {Lua = {diagnostics = {globals = {"vim"}}, format = {enable = false}}},         root_markers = {".luarc.json", ".luarc.jsonc", ".luacheckrc", ".stylua.toml", "stylua.toml", "selene.toml", "selene.yml", ".git"}})  config({name = "jdtls",