Skip to content

Commit

Permalink
fix: set timeout for null-ls considering range format capability.
Browse files Browse the repository at this point in the history
this commit also revert the settings structure b/c we can refactor it in
another PR.
  • Loading branch information
ayamir committed Jun 29, 2024
1 parent 9f2c242 commit 677f2d1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 53 deletions.
77 changes: 41 additions & 36 deletions lua/core/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, boolean>
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<string, boolean>
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<string, boolean>
settings["formatter_block_list"] = {
lua = false, -- example
}

-- Servers in this list will skip setting formatting capabilities if rhs is true.
---@type table<string, boolean>
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
Expand Down
43 changes: 26 additions & 17 deletions lua/modules/configs/completion/formatting.lua
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lua/modules/configs/completion/null-ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 677f2d1

Please sign in to comment.