Skip to content

Commit 7a95d5f

Browse files
committed
refactor: Simplify debugprint() function
1 parent e74e365 commit 7a95d5f

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

lua/debugprint/init.lua

+26-32
Original file line numberDiff line numberDiff line change
@@ -239,35 +239,6 @@ M.debugprint_operatorfunc_motion = function()
239239
M.debugprint_operatorfunc_regular()
240240
end
241241

242-
---@param opts DebugprintFunctionOptionsInternal
243-
---@return nil
244-
M.debugprint_regular = function(opts)
245-
if opts.variable == true then
246-
local filetype_config = get_filetype_config()
247-
248-
if filetype_config then
249-
opts.variable_name = utils.get_variable_name(
250-
global_opts.ignore_treesitter or opts.ignore_treesitter or false,
251-
filetype_config
252-
)
253-
254-
if not opts.variable_name then
255-
return
256-
end
257-
end
258-
end
259-
260-
if opts.insert == true then
261-
return get_debugprint_line(opts)
262-
else
263-
cache_request = opts
264-
utils_operator.set_operatorfunc(
265-
"v:lua.require'debugprint'.debugprint_operatorfunc_regular"
266-
)
267-
return "g@l"
268-
end
269-
end
270-
271242
---@param opts? DebugprintFunctionOptions
272243
---@return string?
273244
M.debugprint = function(opts)
@@ -280,15 +251,38 @@ M.debugprint = function(opts)
280251
return
281252
end
282253

283-
if func_opts.motion == true then
254+
if func_opts.variable == true then
255+
local filetype_config = get_filetype_config()
256+
257+
if filetype_config then
258+
func_opts.variable_name = utils.get_variable_name(
259+
global_opts.ignore_treesitter
260+
or func_opts.ignore_treesitter
261+
or false,
262+
filetype_config
263+
)
264+
265+
if not func_opts.variable_name then
266+
return
267+
end
268+
end
269+
end
270+
271+
if func_opts.insert == true then
272+
cache_request = {}
273+
return get_debugprint_line(func_opts)
274+
elseif func_opts.motion == true then
284275
cache_request = func_opts
285276
utils_operator.set_operatorfunc(
286277
"v:lua.require'debugprint'.debugprint_operatorfunc_motion"
287278
)
288279
return "g@"
289280
else
290-
cache_request = {}
291-
return M.debugprint_regular(func_opts)
281+
cache_request = func_opts
282+
utils_operator.set_operatorfunc(
283+
"v:lua.require'debugprint'.debugprint_operatorfunc_regular"
284+
)
285+
return "g@l"
292286
end
293287
end
294288

lua/debugprint/options.lua

+7
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ local FUNCTION_OPTION_DEFAULTS = {
123123
above = false,
124124
variable = false,
125125
ignore_treesitter = false,
126+
motion = false,
127+
insert = false,
126128
}
127129

128130
---@param o DebugprintFunctionOptions
@@ -132,6 +134,8 @@ local validate_function_opts = function(o)
132134
above = { o.above, "boolean" },
133135
variable = { o.above, "boolean" },
134136
ignore_treesitter = { o.ignore_treesitter, "boolean" },
137+
motion = { o.motion, "boolean" },
138+
insert = { o.motion, "boolean" },
135139
})
136140
end
137141

@@ -198,6 +202,9 @@ M.get_and_validate_function_opts = function(opts)
198202

199203
validate_function_opts(func_opts)
200204

205+
assert(not (func_opts.motion and func_opts.insert))
206+
assert(not (func_opts.motion and func_opts.variable))
207+
201208
return func_opts
202209
end
203210

lua/debugprint/types.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@
6161
---@field variable? boolean
6262
---@field ignore_treesitter? boolean
6363
---@field insert? boolean
64+
---@field motion? boolean
6465

6566
---@class DebugprintFunctionOptionsInternal: DebugprintFunctionOptions
66-
---@field motion? boolean
6767
---@field variable_name? string
6868

6969
---@class DebugprintCommandOpts

0 commit comments

Comments
 (0)