Skip to content

Commit 227e537

Browse files
committed
little emmylua tweaks
1 parent e6405d9 commit 227e537

File tree

6 files changed

+70
-68
lines changed

6 files changed

+70
-68
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ The following object is provided as an argument to `pre_hook` and `post_hook` fu
424424

425425
---Range of the selection that needs to be commented
426426
---@class CommentRange
427-
---@field srow number Starting row
428-
---@field scol number Starting column
429-
---@field erow number Ending row
430-
---@field ecol number Ending column
427+
---@field srow integer Starting row
428+
---@field scol integer Starting column
429+
---@field erow integer Ending row
430+
---@field ecol integer Ending column
431431
```
432432

433433
`CommentType`, `CommentMode` and `CommentMotion` all of them are exported from the plugin's utils for reuse

lua/Comment/config.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
---@private
4545
---@class RootConfig
4646
---@field config CommentConfig
47-
---@field position number[] To be used to restore cursor position
48-
---@field count number Helps with dot-repeat support for count prefix
47+
---@field position integer[] To be used to restore cursor position
48+
---@field count integer Helps with dot-repeat support for count prefix
4949
local Config = {
5050
state = {},
5151
config = {
@@ -88,6 +88,7 @@ function Config:get()
8888
return self.config
8989
end
9090

91+
---@export ft
9192
return setmetatable(Config, {
9293
__index = function(this, k)
9394
return this.state[k]

lua/Comment/extra.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ local extra = {}
77

88
-- FIXME This prints `a` in i_CTRL-o
99
---Moves the cursor and enters INSERT mode
10-
---@param row number Starting row
11-
---@param col number Ending column
10+
---@param row integer Starting row
11+
---@param col integer Ending column
1212
local function move_n_insert(row, col)
1313
A.nvim_win_set_cursor(0, { row, col })
1414
A.nvim_feedkeys('a', 'ni', true)
1515
end
1616

17-
---@param count number Line index
18-
---@param ctype number See |CommentType|
17+
---@param count integer Line index
18+
---@param ctype integer
1919
---@param cfg CommentConfig
2020
local function ins_on_line(count, ctype, cfg)
2121
local row, col = unpack(A.nvim_win_get_cursor(0))
@@ -45,21 +45,21 @@ local function ins_on_line(count, ctype, cfg)
4545
end
4646

4747
---Add a comment below the current line and goes to INSERT mode
48-
---@param ctype number See |CommentType|
48+
---@param ctype integer See |comment.utils.ctype|
4949
---@param cfg CommentConfig
5050
function extra.insert_below(ctype, cfg)
5151
ins_on_line(0, ctype, cfg)
5252
end
5353

5454
---Add a comment above the current line and goes to INSERT mode
55-
---@param ctype number See |CommentType|
55+
---@param ctype integer See |comment.utils.ctype|
5656
---@param cfg CommentConfig
5757
function extra.insert_above(ctype, cfg)
5858
ins_on_line(-1, ctype, cfg)
5959
end
6060

6161
---Add a comment at the end of current line and goes to INSERT mode
62-
---@param ctype number See |CommentType|
62+
---@param ctype integer See |comment.utils.ctype|
6363
---@param cfg CommentConfig
6464
function extra.insert_eol(ctype, cfg)
6565
local srow, scol = unpack(A.nvim_win_get_cursor(0))

lua/Comment/ft.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ local M = {
1919
}
2020

2121
---Lang table that contains commentstring (linewise/blockwise) for mutliple filetypes
22-
---@type table { filetype = { linewise, blockwise } }
22+
---Structure = { filetype = { linewise, blockwise } }
23+
---@type table<string,string[]>
2324
local L = {
2425
arduino = { M.cxx_l, M.cxx_b },
2526
bash = { M.hash },
@@ -115,7 +116,7 @@ end
115116

116117
---Get a commentstring from the filtype list
117118
---@param lang CommentLang
118-
---@param ctype number See |CommentType|
119+
---@param ctype integer See |comment.utils.ctype|
119120
---@return string
120121
function ft.get(lang, ctype)
121122
local l = ft.lang(lang)
@@ -124,16 +125,16 @@ end
124125

125126
---Get the commentstring(s) from the filtype list
126127
---@param lang CommentLang
127-
---@return string
128+
---@return string[]
128129
function ft.lang(lang)
129130
return L[lang]
130131
end
131132

132133
---Get the tree in range by walking the whole tree recursively
133-
---NOTE: This ignores `comment` parser as this is useless
134+
---NOTE: This ignores `comment` parser as this is not needed
134135
---@param tree userdata Tree to be walked
135-
---@param range number[] Range to check for
136-
---@return userdata
136+
---@param range integer[] Range to check - {start_line, s_col, end_line, end_col}
137+
---@return userdata _ Returns a 'treesitter-languagetree'
137138
function ft.contains(tree, range)
138139
for lang, child in pairs(tree:children()) do
139140
if lang ~= 'comment' and child:contains(range) then
@@ -166,6 +167,7 @@ function ft.calculate(ctx)
166167
return ft.get(lang, ctx.ctype) or default
167168
end
168169

170+
---@export ft
169171
return setmetatable(ft, {
170172
__newindex = function(this, k, v)
171173
this.set(k, v)

lua/Comment/opfunc.lua

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ local A = vim.api
66

77
local Op = {}
88

9-
---@alias OpMode 'line'|'char'|'v'|'V' Vim operator-mode motions. Read `:h map-operator`
9+
---Vim operator-mode motions.
10+
---Read `:h :map-operator`
11+
---@alias OpMode
12+
---| 'line' # Vertical motion
13+
---| 'char' # Horizontal motion
14+
---| 'v' # Visual Block motion
15+
---| 'V' # Visual Line motion
1016

1117
---@class CommentCtx Comment context
12-
---@field ctype number CommentType
13-
---@field cmode number CommentMode
14-
---@field cmotion number CommentMotion
18+
---@field ctype integer See |comment.utils.ctype|
19+
---@field cmode integer See |comment.utils.cmode|
20+
---@field cmotion integer See |comment.utils.cmotion|
1521
---@field range CommentRange
1622

1723
---@class OpFnParams Operator-mode function parameters
1824
---@field cfg CommentConfig
19-
---@field cmode number See |comment.utils.cmode|
20-
---@field lines table List of lines
25+
---@field cmode integer See |comment.utils.cmode|
26+
---@field lines string[] List of lines
2127
---@field rcs string RHS of commentstring
2228
---@field lcs string LHS of commentstring
2329
---@field range CommentRange
@@ -26,28 +32,10 @@ local Op = {}
2632
---This function contains the core logic for comment/uncomment
2733
---@param opmode OpMode
2834
---@param cfg CommentConfig
29-
---@param cmode number CommentMode
30-
---@param ctype number CommentType
31-
---@param cmotion number CommentMotion
35+
---@param cmode integer See |comment.utils.cmode|
36+
---@param ctype integer See |comment.utils.ctype|
37+
---@param cmotion integer See |comment.utils.cmotion|
3238
function Op.opfunc(opmode, cfg, cmode, ctype, cmotion)
33-
-- comment/uncomment logic
34-
--
35-
-- 1. type == line
36-
-- * decide whether to comment or not, if all the lines are commented then uncomment otherwise comment
37-
-- * also, store the minimum indent from all the lines (exclude empty line)
38-
-- * if comment the line, use cstr LHS and also considering the min indent
39-
-- * if uncomment the line, remove cstr LHS from lines
40-
-- * update the lines
41-
-- 2. type == block
42-
-- * check if the first and last is commented or not with cstr LHS and RHS respectively.
43-
-- * if both lines commented
44-
-- - remove cstr LHS from the first line
45-
-- - remove cstr RHS to end of the last line
46-
-- * if both lines uncommented
47-
-- - add cstr LHS after the leading whitespace and before the first char of the first line
48-
-- - add cstr RHS to end of the last line
49-
-- * update the lines
50-
5139
cmotion = cmotion == U.cmotion._ and U.cmotion[opmode] or cmotion
5240

5341
local range = U.get_region(opmode)
@@ -103,7 +91,7 @@ end
10391

10492
---Line commenting
10593
---@param param OpFnParams
106-
---@return number
94+
---@return integer _ Returns a calculated comment mode
10795
function Op.linewise(param)
10896
local pattern = U.is_fn(param.cfg.ignore)
10997
local padding = U.is_fn(param.cfg.padding)
@@ -161,10 +149,10 @@ function Op.linewise(param)
161149
return cmode
162150
end
163151

164-
---Full/Partial/Current-line Block commenting
152+
---Full/Partial/Current-Line Block commenting
165153
---@param param OpFnParams
166154
---@param partial? boolean Comment the partial region (visual mode)
167-
---@return number
155+
---@return integer _ Returns a calculated comment mode
168156
function Op.blockwise(param, partial)
169157
local is_x = #param.lines == 1 -- current-line blockwise
170158
local lines = is_x and param.lines[1] or param.lines
@@ -198,11 +186,11 @@ function Op.blockwise(param, partial)
198186
return cmode
199187
end
200188

201-
---Toggle line comment with count i.e vim.v.count
202-
---Example: `10gl` will comment 10 lines
203-
---@param count number Number of lines
189+
---Line commenting with count i.e vim.v.count
190+
---Example: '10gl' will comment 10 lines
191+
---@param count integer Number of lines
204192
---@param cfg CommentConfig
205-
---@param ctype number CommentType
193+
---@param ctype integer See |comment.utils.ctype|
206194
function Op.count(count, cfg, ctype)
207195
local lines, range = U.get_count_lines(count)
208196

lua/Comment/utils.lua

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ U.ctype = {
3737
}
3838

3939
---@class CommentMotion Comment motion types
40-
---@field private _ number Compute from vim mode. See |OpMode|
41-
---@field line number Line motion (ie. `gc2j`)
42-
---@field char number Character/left-right motion (ie. `gc2j`)
43-
---@field block number Visual operator-pending motion
44-
---@field v number Visual motion
45-
---@field V number Visual-line motion
40+
---@field private _ integer Compute from vim mode. See |OpMode|
41+
---@field line integer Line motion (ie. 'gc2j')
42+
---@field char integer Character/left-right motion (ie. 'gc2w')
43+
---@field block integer Visual operator-pending motion
44+
---@field v integer Visual motion (ie. 'v3jgc')
45+
---@field V integer Visual-line motion (ie. 'V10kgc')
4646

4747
---An object containing comment motions
4848
---@type CommentMotion
@@ -55,13 +55,15 @@ U.cmotion = {
5555
V = 5,
5656
}
5757

58+
---@private
5859
---Check whether the line is empty
5960
---@param iter string|string[]
6061
---@return boolean
6162
function U.is_empty(iter)
6263
return #iter == 0
6364
end
6465

66+
---@private
6567
---Get the length of the indentation
6668
---@param str string
6769
---@return integer integer Length of indent chars
@@ -97,6 +99,7 @@ function U.is_fn(fn, ...)
9799
return fn
98100
end
99101

102+
---@private
100103
---Check if the given line is ignored or not with the given pattern
101104
---@param ln string Line to be ignored
102105
---@param pat string Lua regex
@@ -122,7 +125,7 @@ function U.get_region(opmode)
122125
end
123126

124127
---Get lines from the current position to the given count
125-
---@param count number
128+
---@param count integer Probably 'vim.v.count'
126129
---@return CommentLines
127130
---@return CommentRange
128131
function U.get_count_lines(count)
@@ -146,7 +149,7 @@ function U.get_lines(range)
146149
end
147150

148151
---Validates and unwraps the given commentstring
149-
---@param cstr string
152+
---@param cstr string See 'commentstring'
150153
---@return string string Left side of the commentstring
151154
---@return string string Right side of the commentstring
152155
function U.unwrap_cstr(cstr)
@@ -160,10 +163,10 @@ function U.unwrap_cstr(cstr)
160163
return vim.trim(left), vim.trim(right)
161164
end
162165

163-
---Unwraps the commentstring by taking it from the following places
164-
--- 1. `pre_hook` (optionally a string can be returned)
165-
--- 2. `ft.lua` (extra commentstring table in the plugin)
166-
--- 3. `commentstring` (already set or added in pre_hook)
166+
---Parses commentstring from the following places in the respective order
167+
--- 1. pre_hook - commentstring returned from the function
168+
--- 2. ft.lua - commentstring table bundled with the plugin
169+
--- 3. commentstring - Neovim's native. See 'commentstring'
167170
---@param cfg CommentConfig
168171
---@param ctx CommentCtx
169172
---@return string string Left side of the commentstring
@@ -179,9 +182,10 @@ function U.parse_cstr(cfg, ctx)
179182
return U.unwrap_cstr(cstr)
180183
end
181184

182-
---Returns a closure which is used to comment a line
183-
---If given {string[]} to the closure then it will do blockwise
184-
---else it will do linewise
185+
---Returns a closure which is used to do comments
186+
---
187+
---If given {string[]} to the closure then it will do blockwise comment
188+
---else linewise comment will be done with the given {string}
185189
---@param left string Left side of the commentstring
186190
---@param right string Right side of the commentstring
187191
---@param padding boolean Is padding enabled?
@@ -243,6 +247,9 @@ function U.commenter(left, right, padding, scol, ecol)
243247
end
244248

245249
---Returns a closure which is used to uncomment a line
250+
---
251+
---If given {string[]} to the closure then it will block uncomment
252+
---else linewise uncomment will be done with the given {string}
246253
---@param left string Left side of the commentstring
247254
---@param right string Right side of the commentstring
248255
---@param padding boolean Is padding enabled?
@@ -299,6 +306,10 @@ function U.uncommenter(left, right, padding, scol, ecol)
299306
end
300307

301308
---Check if the given string is commented or not
309+
---
310+
---If given {string[]} to the closure, it will check the first and last line
311+
---with LHS and RHS of commentstring respectively else it will check the given
312+
---line with LHS and RHS (if given) of the commenstring
302313
---@param left string Left side of the commentstring
303314
---@param right string Right side of the commentstring
304315
---@param padding boolean Is padding enabled?

0 commit comments

Comments
 (0)