Skip to content

Commit 2781aff

Browse files
committed
refactor!: remove autoformat from features table and only configure through formatting table
1 parent 23b36e8 commit 2781aff

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

lua/astrolsp/config.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
---@field cond boolean|function|string? condition for the mapping
1414

1515
---@class AstroLSPFeatureOpts
16-
---@field autoformat boolean? enable or disable auto formatting on start (boolean' default = true)
1716
---@field codelens boolean? enable/disable codelens refresh on start (boolean; default = true)
1817
---@field diagnostics_mode integer? diagnostic mode on start (0 = off, 1 = no signs/virtual text, 2 = no virtual text, 3 = off; default = 3)
1918
---@field inlay_hints boolean? enable/disable inlay hints on start (boolean; default = false)
@@ -38,7 +37,6 @@
3837
--
3938
---```lua
4039
---features = {
41-
--- autoformat = true,
4240
--- codelens = true,
4341
--- diagnostics_mode = 3,
4442
--- inlay_hints = false,
@@ -202,7 +200,6 @@
202200
---@type AstroLSPOpts
203201
local M = {
204202
features = {
205-
autoformat = true,
206203
codelens = true,
207204
diagnostics_mode = 3,
208205
inlay_hints = false,

lua/astrolsp/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ function M.setup(opts)
265265

266266
setup_diagnostics()
267267

268+
-- normalize format_on_save to table format
269+
if vim.tbl_get(M.config, "formatting", "format_on_save") == false then
270+
M.config.formatting.format_on_save = { enabled = false }
271+
end
272+
268273
--- Format options that are passed into the `vim.lsp.buf.format` (`:h vim.lsp.buf.format()`)
269274
---@type AstroLSPFormatOpts
270275
M.format_opts = vim.deepcopy(assert(M.config.formatting))

lua/astrolsp/toggles.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
---@class astrolsp.toggles
1010
local M = {}
1111

12-
local features = require("astrolsp").config.features --[[@as AstroLSPFeatureOpts]]
12+
local config = require("astrolsp").config
13+
local features = config.features --[[@as AstroLSPFeatureOpts]]
14+
local format_on_save = config.formatting.format_on_save --[[@as AstroLSPFormatOnSaveOpts]]
1315

1416
local function ui_notify(silent, ...) return not silent and vim.notify(...) end
1517
local function bool2str(bool) return bool and "on" or "off" end
1618

1719
--- Toggle auto format
1820
---@param silent? boolean if true then don't sent a notification
1921
function M.autoformat(silent)
20-
features.autoformat = not features.autoformat
21-
ui_notify(silent, ("Global autoformatting %s"):format(bool2str(features.autoformat)))
22+
format_on_save.enabled = not format_on_save.enabled
23+
ui_notify(silent, ("Global autoformatting %s"):format(bool2str(format_on_save.enabled)))
2224
end
2325

2426
--- Toggle buffer local auto format
@@ -27,7 +29,10 @@ end
2729
function M.buffer_autoformat(bufnr, silent)
2830
bufnr = bufnr or 0
2931
local old_val = vim.b[bufnr].autoformat
30-
if old_val == nil then old_val = features.autoformat end
32+
if old_val == nil then
33+
ui_notify(silent, "No LSP attached with auto formatting")
34+
return
35+
end
3136
vim.b[bufnr].autoformat = not old_val
3237
ui_notify(silent, ("Buffer autoformatting %s"):format(bool2str(vim.b[bufnr].autoformat)))
3338
end

0 commit comments

Comments
 (0)