From 677f2d152a51d30eb1ba74af7380a09536a7ac78 Mon Sep 17 00:00:00 2001 From: ayamir Date: Sat, 29 Jun 2024 22:37:56 +0800 Subject: [PATCH] fix: set timeout for null-ls considering range format capability. this commit also revert the settings structure b/c we can refactor it in another PR. --- lua/core/settings.lua | 77 ++++++++++--------- lua/modules/configs/completion/formatting.lua | 43 +++++++---- lua/modules/configs/completion/null-ls.lua | 1 + 3 files changed, 68 insertions(+), 53 deletions(-) diff --git a/lua/core/settings.lua b/lua/core/settings.lua index f9174afdf..9f7271444 100644 --- a/lua/core/settings.lua +++ b/lua/core/settings.lua @@ -8,42 +8,47 @@ settings["use_ssh"] = true ---@type boolean settings["use_copilot"] = true --- entrance of format-related options -settings["format_opts"] = { - -- Set it to false if there is no need to format on save. - ---@type boolean - format_on_save = true, - -- Set it to false if the notification after formatting is annoying. - ---@type boolean - format_notify = true, - -- Set it to true if you prefer formatting ONLY the *changed lines* as defined by your version control system. - -- NOTE: This entry will only be respected if: - -- > The buffer to be formatted is under version control (Git or Mercurial); - -- > Any of the server attached to that buffer supports |DocumentRangeFormattingProvider| server capability. - -- Otherwise Neovim would fall back to format the whole buffer, and a warning will be issued. - ---@type boolean - format_modifications_only = false, - -- Set the format disabled directories here, files under these dirs won't be formatted on save. - --- NOTE: Directories may contain regular expressions (grammar: vim). |regexp| - --- NOTE: Directories are automatically normalized. |vim.fs.normalize()| - ---@type string[] - format_disabled_dirs = { - "~/format_disabled_dir", - }, - -- Set format timeout here - format_timeout = 1000, - -- Servers in this list will skip setting formatting capabilities if rhs is true. - ---@type table - server_formatting_block_list = { - lua_ls = true, - tsserver = true, - clangd = true, - }, - -- Filetypes in this list will skip lsp formatting if rhs is true. - ---@type table - formatter_block_list = { - lua = false, -- example - }, +-- Set it to false if there is no need to format on save. +---@type boolean +settings["format_on_save"] = true + +-- Set format timeout here (ms) +---@type number +settings["format_timeout"] = 1000 + +-- Set it to false if the notification after formatting is annoying. +---@type boolean +settings["format_notify"] = true + +-- Set it to true if you prefer formatting ONLY the *changed lines* as defined by your version control system. +-- NOTE: This entry will only be respected if: +-- > The buffer to be formatted is under version control (Git or Mercurial); +-- > Any of the server attached to that buffer supports |DocumentRangeFormattingProvider| server capability. +-- Otherwise Neovim would fall back to format the whole buffer, and a warning will be issued. +---@type boolean +settings["format_modifications_only"] = false + +-- Set the format disabled directories here, files under these dirs won't be formatted on save. +--- NOTE: Directories may contain regular expressions (grammar: vim). |regexp| +--- NOTE: Directories are automatically normalized. |vim.fs.normalize()| +---@type string[] +settings["format_disabled_dirs"] = { + -- Example + "~/format_disabled_dir", +} + +-- Filetypes in this list will skip lsp formatting if rhs is true. +---@type table +settings["formatter_block_list"] = { + lua = false, -- example +} + +-- Servers in this list will skip setting formatting capabilities if rhs is true. +---@type table +settings["server_formatting_block_list"] = { + lua_ls = true, + tsserver = true, + clangd = true, } -- Set it to false if you want to turn off LSP Inlay Hints diff --git a/lua/modules/configs/completion/formatting.lua b/lua/modules/configs/completion/formatting.lua index a35b6c25c..b8a35df20 100644 --- a/lua/modules/configs/completion/formatting.lua +++ b/lua/modules/configs/completion/formatting.lua @@ -1,19 +1,18 @@ local M = {} local settings = require("core.settings") -local format_opts = settings.format_opts -local disabled_workspaces = format_opts.format_disabled_dirs -local format_on_save = format_opts.format_on_save -local format_notify = format_opts.format_notify -local format_timeout = format_opts.format_timeout -local format_modifications_only = format_opts.format_modifications_only -local server_formatting_block_list = format_opts.server_formatting_block_list +local disabled_workspaces = settings.format_disabled_dirs +local format_on_save = settings.format_on_save +local format_notify = settings.format_notify +local format_modifications_only = settings.format_modifications_only +local server_formatting_block_list = settings.server_formatting_block_list +local format_timeout = settings.format_timeout vim.api.nvim_create_user_command("FormatToggle", function() M.toggle_format_on_save() end, {}) -local block_list = require("core.settings").format_opts.formatter_block_list +local block_list = settings.formatter_block_list vim.api.nvim_create_user_command("FormatterToggleFt", function(opts) if block_list[opts.args] == nil then vim.notify( @@ -158,18 +157,28 @@ function M.format(opts) { title = "LSP Formatter Warning" } ) return - elseif - format_modifications_only - and require("lsp-format-modifications").format_modifications(client, bufnr).success - then - if format_notify then + end + + if format_modifications_only then + if client.server_capabilities.documentRangeFormattingProvider then + if + format_notify + and require("lsp-format-modifications").format_modifications(client, bufnr).success + then + vim.notify( + string.format("[LSP] Format changed lines successfully with %s!", client.name), + vim.log.levels.INFO, + { title = "LSP Range Format Success" } + ) + return + end + else vim.notify( - string.format("[LSP] Format changed lines successfully with %s!", client.name), - vim.log.levels.INFO, - { title = "LSP Range Format Success" } + string.format("[LSP] Modification only formatting is not supported by formatter [%s]!", client.name), + vim.log.levels.WARN, + { title = "LSP Range Format Warning" } ) end - return end -- Fall back to format the whole buffer (even if partial formatting failed) diff --git a/lua/modules/configs/completion/null-ls.lua b/lua/modules/configs/completion/null-ls.lua index 9179da8a7..146ec37c0 100644 --- a/lua/modules/configs/completion/null-ls.lua +++ b/lua/modules/configs/completion/null-ls.lua @@ -42,6 +42,7 @@ return function() log_level = "warn", update_in_insert = false, sources = sources, + default_timeout = require("settings").format_timeout }) require("completion.mason-null-ls").setup()