Skip to content

Commit

Permalink
rename document formatting...
Browse files Browse the repository at this point in the history
apply this branch after this PR is merged
neovim/nvim-lspconfig#1838
  • Loading branch information
ajitid committed Apr 26, 2022
1 parent ec0282d commit b749866
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions nvim/.config/nvim/lua/mine/lsp/lsp-and-completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function diagnostic_keymaps()
end

function format_on_save(client)
if client.resolved_capabilities.document_formatting then
if client.server_capabilities.documentFormattingProvider then
vim.keymap.set("n", "<leader>f=", vim.lsp.buf.formatting_sync, {buffer=0})

vim.api.nvim_command [[augroup Format]]
Expand All @@ -114,19 +114,30 @@ function format_on_save(client)
vim.api.nvim_command [[augroup END]]
end

if client.resolved_capabilities.document_range_formatting then
if client.server_capabilities.documentRangeFormattingProvider then
vim.keymap.set("v", "<leader>=", vim.lsp.buf.range_formatting, {buffer=0})
end
end

function disable_formatting(client)
client.server_capabilities.documentFormattingProvider = false
client.server_capabilities.documentRangeFormattingProvider = false
-- TODO resolved_capabilities would be removed, see https://www.reddit.com/r/neovim/comments/u5si2w/breaking_changes_inbound_next_few_weeks_for/
-- before removing the following lines from the code
-- do ensure that sample code in dotfiles repo's da7fcc455fd6e9d89b7b79be8b19216d32aaf055 commit message
-- formats without issue
client.resolved_capabilities.document_formatting = false
client.resolved_capabilities.document_range_formatting = false
end

require'lspconfig'.gopls.setup{
capabilities = capabilities,
on_attach = function(client, bufnr)
basic_keymaps()
diagnostic_keymaps()
format_on_save(client)
require("aerial").on_attach(client, bufnr)
client.resolved_capabilities.document_formatting = false
disable_formatting(client)
end,
}

Expand Down Expand Up @@ -201,11 +212,11 @@ lsp_installer.on_server_ready(function(server)
vim.keymap.set("n", "<leader>fr", typescript_rename_file_command, {buffer=0})

-- needed, otherwise on save nvim would ask whether to save using null-ls (prettier) or tsserver
client.resolved_capabilities.document_formatting = false
disable_formatting(client)
-- this, if I remember correctly is to improve perf. I'm keeping it commented for now:
-- client.config.flags.allow_incremental_sync = true
elseif server.name == "jsonls" then
client.resolved_capabilities.document_formatting = false
disable_formatting(client)
else
format_on_save(client)
end
Expand Down Expand Up @@ -322,6 +333,7 @@ local sources = {

local config = {
on_attach = function(client, bufnr)
-- don't use `disable_formatting`, it is discouraged, see https://github.com/jose-elias-alvarez/null-ls.nvim/issues/778#issuecomment-1103053724
format_on_save(client)
diagnostic_keymaps()
end,
Expand Down

0 comments on commit b749866

Please sign in to comment.