From 8f30baec4483444d38ab4dc244f47d428a63647e Mon Sep 17 00:00:00 2001 From: ayamir Date: Mon, 1 Jul 2024 10:03:57 +0800 Subject: [PATCH] feat: make format timeout configurable (#1275) * feat&refactor: make format timeout configurable, refactor format-related options. * chore(settings): set `format_modifications_only` to `false` to prevent possible ci issues Signed-off-by: Charles Chiu * 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. * fixup: fix ci. * fix: remove redundant notification. * fixup: fix notify logic and CI. * fixup: merge judgements. Co-authored-by: jint_lzxy_ <50296129+Jint-lzxy@users.noreply.github.com> Signed-off-by: Mohu * fixup: remove redundant judge. * docs: more accurate comment note. Co-authored-by: jint_lzxy_ <50296129+Jint-lzxy@users.noreply.github.com> Signed-off-by: ayamir * fix: wrong require source. Co-authored-by: jint_lzxy_ <50296129+Jint-lzxy@users.noreply.github.com> Signed-off-by: ayamir --------- Signed-off-by: Charles Chiu Signed-off-by: Mohu Signed-off-by: ayamir Co-authored-by: Charles Chiu Co-authored-by: jint_lzxy_ <50296129+Jint-lzxy@users.noreply.github.com> --- lua/core/settings.lua | 40 ++++++++++--------- lua/modules/configs/completion/formatting.lua | 9 +++-- lua/modules/configs/completion/null-ls.lua | 1 + 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/lua/core/settings.lua b/lua/core/settings.lua index 3cd9c923b..125416008 100644 --- a/lua/core/settings.lua +++ b/lua/core/settings.lua @@ -8,14 +8,14 @@ settings["use_ssh"] = true ---@type boolean settings["use_copilot"] = true --- Set it to false if you want to turn off LSP Inlay Hints ----@type boolean -settings["lsp_inlayhints"] = true - -- Set it to false if there is no need to format on save. ---@type boolean settings["format_on_save"] = true +-- Set format timeout here (in ms). +---@type number +settings["format_timeout"] = 1000 + -- Set it to false if the notification after formatting is annoying. ---@type boolean settings["format_notify"] = true @@ -37,6 +37,24 @@ settings["format_disabled_dirs"] = { "~/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 +---@type boolean +settings["lsp_inlayhints"] = true + -- Set it to false if diagnostics virtual text is annoying. -- If disabled, you may browse lsp diagnostics using trouble.nvim (press `gt` to toggle it). ---@type boolean @@ -85,20 +103,6 @@ settings["background"] = "dark" ---@type string settings["external_browser"] = "chrome-cli open" --- 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 the language servers that will be installed during bootstrap here. -- check the below link for all the supported LSPs: -- https://github.com/neovim/nvim-lspconfig/tree/master/lua/lspconfig/server_configurations diff --git a/lua/modules/configs/completion/formatting.lua b/lua/modules/configs/completion/formatting.lua index 4634d0451..0368efef7 100644 --- a/lua/modules/configs/completion/formatting.lua +++ b/lua/modules/configs/completion/formatting.lua @@ -6,12 +6,13 @@ 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").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( @@ -35,7 +36,7 @@ vim.api.nvim_create_user_command("FormatterToggleFt", function(opts) end, { nargs = 1, complete = "filetype" }) function M.enable_format_on_save(is_configured) - local opts = { pattern = "*", timeout = 1000 } + local opts = { pattern = "*", timeout = format_timeout } vim.api.nvim_create_augroup("format_on_save", { clear = true }) vim.api.nvim_create_autocmd("BufWritePre", { group = "format_on_save", @@ -156,7 +157,9 @@ function M.format(opts) { title = "LSP Formatter Warning" } ) return - elseif + end + + if format_modifications_only and require("lsp-format-modifications").format_modifications(client, bufnr).success then diff --git a/lua/modules/configs/completion/null-ls.lua b/lua/modules/configs/completion/null-ls.lua index ff2efab17..72982d6ea 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("core.settings").format_timeout, }) require("completion.mason-null-ls").setup()