Skip to content

Commit 8a2b2f3

Browse files
committed
chore: refactor config.lua
1 parent a841f73 commit 8a2b2f3

File tree

3 files changed

+36
-38
lines changed

3 files changed

+36
-38
lines changed

lua/Comment/api.lua

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local U = require('Comment.utils')
22
local Ex = require('Comment.extra')
33
local Op = require('Comment.opfunc')
4-
local Config = require('Comment.config'):new()
4+
local Config = require('Comment.config')
55
local A = vim.api
66

77
local C = {}
@@ -35,8 +35,7 @@ end
3535
---Toggle linewise-comment over multiple lines using `vim.v.count`
3636
---@param cfg? Config
3737
function C.toggle_linewise_count(cfg)
38-
local c = Config:get()
39-
Op.count(c.__count or vim.v.count, cfg or c, U.ctype.line)
38+
Op.count(Config.count or vim.v.count, cfg or Config:get(), U.ctype.line)
4039
end
4140

4241
---@private
@@ -71,8 +70,7 @@ end
7170
---Toggle blockwise-comment over multiple lines using `vim.v.count`
7271
---@param cfg? Config
7372
function C.toggle_blockwise_count(cfg)
74-
local c = Config:get()
75-
Op.count(c.__count or vim.v.count, cfg or c, U.ctype.block)
73+
Op.count(Config.count or vim.v.count, cfg or Config:get(), U.ctype.block)
7674
end
7775

7876
---@private
@@ -224,10 +222,9 @@ C.locked = setmetatable({}, {
224222
---NOTE: We are using `config` to store the position as it is a kinda global
225223
---@param cb string Name of the API function to call
226224
function C.call(cb)
227-
local cfg = Config:get()
228225
A.nvim_set_option('operatorfunc', ("v:lua.require'Comment.api'.locked.%s"):format(cb))
229-
cfg.__pos = cfg.sticky and A.nvim_win_get_cursor(0)
230-
cfg.__count = vim.v.count
226+
Config.position = Config:get().sticky and A.nvim_win_get_cursor(0)
227+
Config.count = vim.v.count
231228
end
232229

233230
---Configures the whole plugin

lua/Comment/config.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@
4343
---@field extra ExtraMapping
4444
---@field pre_hook fun(ctx: Ctx):string Function to be called before comment/uncomment
4545
---@field post_hook fun(ctx:Ctx) Function to be called after comment/uncomment
46-
---@field __pos number[] To be used to restore cursor position
47-
---@field __count number Helps with dot-repeat support for count prefix
4846

4947
---@class RootConfig
5048
---@field config Config
51-
local Config = {}
52-
53-
function Config.default()
54-
return {
49+
---@field position number[] To be used to restore cursor position
50+
---@field count number Helps with dot-repeat support for count prefix
51+
local Config = {
52+
state = {},
53+
config = {
5554
padding = true,
5655
sticky = true,
5756
mappings = {
@@ -72,14 +71,8 @@ function Config.default()
7271
below = 'gco',
7372
eol = 'gcA',
7473
},
75-
}
76-
end
77-
78-
---Creates a new config instance
79-
---@return RootConfig
80-
function Config:new()
81-
return setmetatable({ config = self.default() }, { __index = self })
82-
end
74+
},
75+
}
8376

8477
---Update the config
8578
---@param cfg Config
@@ -97,4 +90,11 @@ function Config:get()
9790
return self.config
9891
end
9992

100-
return Config
93+
return setmetatable(Config, {
94+
__index = function(this, k)
95+
return this.state[k]
96+
end,
97+
__newindex = function(this, k, v)
98+
this.state[k] = v
99+
end,
100+
})

lua/Comment/opfunc.lua

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
local U = require('Comment.utils')
2+
local Config = require('Comment.config')
23
local A = vim.api
34

4-
local O = {}
5+
local Op = {}
56

67
---@alias VMode '"line"'|'"char"'|'"v"'|'"V"' Vim Mode. Read `:h map-operator`
78

@@ -27,7 +28,7 @@ local O = {}
2728
---@param cmode CMode
2829
---@param ctype CType
2930
---@param cmotion CMotion
30-
function O.opfunc(vmode, cfg, cmode, ctype, cmotion)
31+
function Op.opfunc(vmode, cfg, cmode, ctype, cmotion)
3132
-- comment/uncomment logic
3233
--
3334
-- 1. type == line
@@ -75,21 +76,21 @@ function O.opfunc(vmode, cfg, cmode, ctype, cmotion)
7576
}
7677

7778
if block_x then
78-
ctx.cmode = O.blockwise_x(params)
79+
ctx.cmode = Op.blockwise_x(params)
7980
elseif ctype == U.ctype.block and not same_line then
80-
ctx.cmode = O.blockwise(params, partial_block)
81+
ctx.cmode = Op.blockwise(params, partial_block)
8182
else
82-
ctx.cmode = O.linewise(params)
83+
ctx.cmode = Op.linewise(params)
8384
end
8485

8586
-- We only need to restore cursor if both sticky and position are available
8687
-- As this function is also called for visual mapping where we are not storing the position
8788
--
8889
-- And I found out that if someone presses `gc` but doesn't provide operators and
8990
-- does visual comments then cursor jumps to previous stored position. Thus the check for visual modes
90-
if cfg.sticky and cfg.__pos and cmotion ~= U.cmotion.v and cmotion ~= U.cmotion.V then
91-
A.nvim_win_set_cursor(0, cfg.__pos)
92-
cfg.__pos = nil
91+
if cfg.sticky and Config.position and cmotion ~= U.cmotion.v and cmotion ~= U.cmotion.V then
92+
A.nvim_win_set_cursor(0, Config.position)
93+
Config.position = nil
9394
end
9495

9596
U.is_fn(cfg.post_hook, ctx)
@@ -98,7 +99,7 @@ end
9899
---Linewise commenting
99100
---@param p OpFnParams
100101
---@return integer CMode
101-
function O.linewise(p)
102+
function Op.linewise(p)
102103
local lcs_esc, rcs_esc = U.escape(p.lcs), U.escape(p.rcs)
103104
local pattern = U.is_fn(p.cfg.ignore)
104105
local padding, pp = U.get_padding(p.cfg.padding)
@@ -161,7 +162,7 @@ end
161162
---@param p OpFnParams
162163
---@param partial boolean Whether to do a partial or full comment
163164
---@return integer CMode
164-
function O.blockwise(p, partial)
165+
function Op.blockwise(p, partial)
165166
-- Block wise, only when there are more than 1 lines
166167
local sln, eln = p.lines[1], p.lines[#p.lines]
167168
local lcs_esc, rcs_esc = U.escape(p.lcs), U.escape(p.rcs)
@@ -210,7 +211,7 @@ end
210211
---Blockwise (left-right/x-axis motion) commenting
211212
---@param p OpFnParams
212213
---@return integer CMode
213-
function O.blockwise_x(p)
214+
function Op.blockwise_x(p)
214215
local line = p.lines[1]
215216
local first = line:sub(0, p.range.scol)
216217
local mid = line:sub(p.range.scol + 1, p.range.ecol + 1)
@@ -243,7 +244,7 @@ end
243244
---@param count integer Number of lines
244245
---@param cfg Config
245246
---@param ctype CType
246-
function O.count(count, cfg, ctype)
247+
function Op.count(count, cfg, ctype)
247248
local lines, range = U.get_count_lines(count)
248249

249250
---@type Ctx
@@ -266,12 +267,12 @@ function O.count(count, cfg, ctype)
266267
}
267268

268269
if ctype == U.ctype.block then
269-
ctx.cmode = O.blockwise(params)
270+
ctx.cmode = Op.blockwise(params)
270271
else
271-
ctx.cmode = O.linewise(params)
272+
ctx.cmode = Op.linewise(params)
272273
end
273274

274275
U.is_fn(cfg.post_hook, ctx)
275276
end
276277

277-
return O
278+
return Op

0 commit comments

Comments
 (0)