Skip to content

Commit fe4b0a5

Browse files
committed
wip refactor 2
1 parent 3089410 commit fe4b0a5

2 files changed

Lines changed: 32 additions & 36 deletions

File tree

lua/base/4-mappings.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,7 @@ function M.lsp_mappings(client, bufnr)
13371337
-- @param capability string The server capability to check for (example: "documentFormattingProvider").
13381338
-- @param filter? vim.lsp.get_clients.filter|nil A valid get_clients filter (see function docs).
13391339
-- @return boolean # `true` if any of the clients provide the capability.
1340-
local function supports_method(capability, filter)
1340+
local function supports_method(method, filter)
13411341
-- default filter: current buffer.
13421342
if not filter then filter = { bufnr = bufnr } end
13431343

@@ -1347,13 +1347,12 @@ function M.lsp_mappings(client, bufnr)
13471347
if not client.supports_method then return true end
13481348

13491349
-- if the client implement supports_method, respect its value.
1350-
if lsp_client.supports_method(capability) then return true end
1350+
if lsp_client.supports_method(method) then return true end
13511351
end
13521352
return false
13531353
end
13541354

13551355
local lsp_mappings = require("base.utils").get_mappings_template()
1356-
local lsp_default_opts = require("base.utils").apply_default_lsp_settings()
13571356

13581357
-- Diagnostics
13591358
lsp_mappings.n["<leader>ld"] =
@@ -1417,8 +1416,8 @@ function M.lsp_mappings(client, bufnr)
14171416
}
14181417

14191418
-- Formatting (keymapping)
1420-
local formatting = lsp_default_opts.formatting
1421-
local format_opts = lsp_default_opts.format_opts
1419+
local format_opts = require('base.utils').get_lsp_formatting_defaults()
1420+
14221421
lsp_mappings.n["<leader>lf"] = {
14231422
function()
14241423
vim.lsp.buf.format(format_opts)
@@ -1437,7 +1436,7 @@ function M.lsp_mappings(client, bufnr)
14371436
)
14381437

14391438
-- Autoformatting (autocmd)
1440-
local autoformat = formatting.format_on_save
1439+
local autoformat = format_opts
14411440
local filetype = vim.api.nvim_get_option_value("filetype", { buf = bufnr })
14421441

14431442
-- guard clauses
@@ -1544,7 +1543,7 @@ if is_autoformat_enabled and is_filetype_allowed and is_filetype_ignored then
15441543
}
15451544

15461545
-- Goto help
1547-
local lsp_hover_opts = lsp_default_opts.hover_opts
1546+
local lsp_hover_opts = require("base.utils").apply_lsp_diagnostic_defaults()
15481547
lsp_mappings.n["gh"] = {
15491548
function()
15501549
vim.lsp.buf.hover(lsp_hover_opts)

lua/base/utils/init.lua

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ function M.add_autocmds_to_buffer(augroup, bufnr, autocmds)
9595
end
9696
end
9797

98-
--- Apply default settings for diagnostics, formatting, and lsp capabilities.
99-
--- It only need to be executed once, normally on mason-lspconfig.
100-
--- @return nil
101-
M.apply_default_lsp_settings = function()
102-
-- Icons
103-
-- Apply the icons defined in ../icons/icons.lua
98+
--- This function define and apply the default NormalNvim diagnostic settings.
99+
---
100+
--- Feel free to edit this function (but you shouldn't need to).
101+
--- @return table # A table with hover_opts, or empty table {}.
102+
M.apply_lsp_diagnostic_defaults = function()
103+
-- Apply the icons defined in ../icons/icons.lu
104104
local signs = {
105105
{ name = "DiagnosticSignError", text = M.get_icon("DiagnosticError"), texthl = "DiagnosticSignError" },
106106
{ name = "DiagnosticSignWarn", text = M.get_icon("DiagnosticWarn"), texthl = "DiagnosticSignWarn" },
@@ -116,9 +116,6 @@ M.apply_default_lsp_settings = function()
116116
vim.fn.sign_define(sign.name, sign)
117117
end
118118

119-
-- Apply default lsp hover borders
120-
-- Applies the option lsp_round_borders_enabled from ../1-options.lua
121-
local lsp_hover_opts = vim.g.lsp_round_borders_enabled and { border = "rounded", silent = true } or {}
122119

123120
-- Set default diagnostics
124121
local default_diagnostics = {
@@ -145,9 +142,7 @@ M.apply_default_lsp_settings = function()
145142
},
146143
}
147144

148-
-- TODO: This is the only point where we are actually 'applying something' → Let's move it away.
149-
-- Apply default diagnostics
150-
-- Applies the option diagnostics_mode from ../1-options.lua
145+
-- Table of available options to be used in ../1-options.lua > vim.g.diagnostics_mode
151146
local diagnostics = {
152147
-- diagnostics off
153148
[0] = vim.tbl_deep_extend(
@@ -164,27 +159,29 @@ M.apply_default_lsp_settings = function()
164159
}
165160
vim.diagnostic.config(diagnostics[vim.g.diagnostics_mode])
166161

167-
-- Apply formatting settings
168-
local lsp_formatting = { format_on_save = { enabled = true }, disabled = {} }
169-
if type(lsp_formatting.format_on_save) == "boolean" then
170-
lsp_formatting.format_on_save = { enabled = lsp_formatting.format_on_save }
171-
end
172-
local lsp_format_opts = vim.deepcopy(lsp_formatting)
173-
lsp_format_opts.disabled = nil
174-
lsp_format_opts.format_on_save = nil
162+
-- Get the option lsp_round_borders_enabled from ../1-options.lua
163+
local lsp_hover_opts = vim.g.lsp_round_borders_enabled and { border = "rounded", silent = true } or {}
164+
165+
return lsp_hover_opts
166+
end
167+
168+
--- This function define the default NormalNvim formatting settings.
169+
--- It's suppossed to be called on the mappings file.
170+
---
171+
--- Feel free to edit this function (but you should't need to).
172+
--- @return table # A table with hover_opts, or empty table {}.
173+
M.get_lsp_formatting_defaults = function()
174+
-- Set formatting setting
175+
local lsp_format_opts = { format_on_save = { enabled = vim.g.autoformat_enabled or false }, disabled = {} }
176+
177+
-- Check if client is fully disabled or filtered by function
175178
lsp_format_opts.filter = function(client)
176-
local filter = lsp_formatting.filter
177-
local disabled = lsp_formatting.disabled or {}
178-
-- check if client is fully disabled or filtered by function
179+
local filter = lsp_format_opts.filter
180+
local disabled = lsp_format_opts.disabled
179181
return not (vim.tbl_contains(disabled, client.name) or (type(filter) == "function" and not filter(client)))
180182
end
181183

182-
local lsp_default_opts = {}
183-
lsp_default_opts.formatting = lsp_formatting
184-
lsp_default_opts.format_opts = lsp_format_opts
185-
lsp_default_opts.hover_opts = lsp_hover_opts
186-
187-
return lsp_default_opts
184+
return lsp_format_opts
188185
end
189186

190187
--- Applies the user lsp mappings to the lsp client.

0 commit comments

Comments
 (0)