Skip to content

Commit

Permalink
feat&refactor: make format timeout configurable, refactor format-related
Browse files Browse the repository at this point in the history
options.
  • Loading branch information
ayamir committed Jun 2, 2024
1 parent cee6354 commit 7858852
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
77 changes: 38 additions & 39 deletions lua/core/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,48 @@ 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 = true,
-- 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 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 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",
}

-- 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
Expand Down Expand Up @@ -85,20 +98,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<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 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
Expand Down
16 changes: 9 additions & 7 deletions lua/modules/configs/completion/formatting.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
local M = {}

local settings = require("core.settings")
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_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

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 = require("core.settings").format_opts.formatter_block_list
vim.api.nvim_create_user_command("FormatterToggleFt", function(opts)
if block_list[opts.args] == nil then
vim.notify(
Expand All @@ -35,7 +37,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",
Expand Down

0 comments on commit 7858852

Please sign in to comment.