Skip to content

Commit 34a76a6

Browse files
committed
Refactor conform config
- Change `format_on_save` to a blacklist - Use stylua's lsp for formatting instead of its CLI See nvim-lua/kickstart.nvim#1991
1 parent 172d461 commit 34a76a6

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

dot_config/nvim/init.lua

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ require('lazy').setup({
398398
-- Special Lua Config, as recommended by neovim help docs
399399
lua_ls = {
400400
on_init = function(client)
401+
client.server_capabilities.documentFormattingProvider = false -- Disable formatting (formatting is done by stylua)
402+
401403
if client.workspace_folders then
402404
local path = client.workspace_folders[1].name
403405
if path ~= vim.fn.stdpath 'config' and (vim.uv.fs_stat(path .. '/.luarc.json') or vim.uv.fs_stat(path .. '/.luarc.jsonc')) then return end
@@ -420,8 +422,11 @@ require('lazy').setup({
420422
},
421423
})
422424
end,
425+
---@type lspconfig.settings.lua_ls
423426
settings = {
424-
Lua = {},
427+
Lua = {
428+
format = { enable = false }, -- Disable formatting (formatting is done by stylua)
429+
},
425430
},
426431
},
427432

@@ -472,7 +477,7 @@ require('lazy').setup({
472477
keys = {
473478
{
474479
'<leader>f',
475-
function() require('conform').format { async = true, lsp_format = 'fallback' } end,
480+
function() require('conform').format { async = true } end,
476481
mode = '',
477482
desc = '[F]ormat buffer',
478483
},
@@ -482,21 +487,23 @@ require('lazy').setup({
482487
opts = {
483488
notify_on_error = false,
484489
format_on_save = function(bufnr)
485-
-- Disable "format_on_save lsp_fallback" for languages that don't
486-
-- have a well standardized coding style. You can add additional
487-
-- languages here or re-enable it for the disabled ones.
488-
local disable_filetypes = { c = true, cpp = true }
489-
if disable_filetypes[vim.bo[bufnr].filetype] then
490-
return nil
490+
-- You can specify filetypes to autoformat on save here:
491+
local enabled_filetypes = {
492+
lua = true,
493+
python = true,
494+
}
495+
if enabled_filetypes[vim.bo[bufnr].filetype] then
496+
return { timeout_ms = 500 }
491497
else
492-
return {
493-
timeout_ms = 500,
494-
lsp_format = 'fallback',
495-
}
498+
return nil
496499
end
497500
end,
501+
default_format_opts = {
502+
lsp_format = 'fallback', -- Use external formatters if configured below, otherwise use LSP formatting. Set to `false` to disable LSP formatting entirely.
503+
},
504+
-- You can also specify external formatters in here.
498505
formatters_by_ft = {
499-
lua = { 'stylua' },
506+
-- rust = { 'rustfmt' },
500507
sh = { 'shfmt' },
501508
-- Conform can also run multiple formatters sequentially
502509
-- python = { "isort", "black" },

0 commit comments

Comments
 (0)