Skip to content

Commit 761f63f

Browse files
authored
chore!: refactoring (#179)
### Added - `utils.commenter()` - `utils.uncommenter()` ### Changed - `utils.{unwrap_cstr,parse_cstr}()` now returns `string, string` instead of `string|bool, string|bool` - Merged `Op.blockwise_x()` with `Op.blockwise()` - Removed `utils.get_padding()`, `utils.move_n_insert()` and `utils.grab_indent()` - Removed `utils.comment_str()` (Use `utils.commenter()`) - Removed `utils.uncomment_str()` (Use `utils.uncommenter()`) - Changed `utils.is_commented()` function signature. Resolve #177
1 parent 7c49fb2 commit 761f63f

File tree

7 files changed

+341
-294
lines changed

7 files changed

+341
-294
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ lua require('Comment').setup()
4343

4444
First you need to call the `setup()` method to create the default mappings.
4545

46-
> NOTE: If you are facing **Keybindings are mapped but they are not working** issue then please try [this](https://github.com/numToStr/Comment.nvim/issues/115#issuecomment-1032290098)
46+
> **Note** - If you are facing **Keybindings are mapped but they are not working** issue then please try [this](https://github.com/numToStr/Comment.nvim/issues/115#issuecomment-1032290098)
4747
4848
- Lua
4949

@@ -243,6 +243,8 @@ This plugin has native **treesitter** support for calculating `commentstring` wh
243243

244244
For advance use cases, use [nvim-ts-context-commentstring](https://github.com/JoosepAlviste/nvim-ts-context-commentstring). See [`pre_hook`](#pre-hook) section for the integration.
245245

246+
> **Note** - This plugin does not depend on [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) however it is recommended in order to easily install tree-sitter parsers.
247+
246248
<a id="hooks"></a>
247249

248250
### 🎣 Hooks
@@ -355,6 +357,8 @@ vim.api.nvim_command('set commentstring=//%s')
355357

356358
> Run `:h commentstring` for more help
357359
360+
<a id="ft-lua"></a>
361+
358362
2. You can also use this plugin interface to store both line and block commentstring for the filetype. You can treat this as a more powerful version of the `commentstring`
359363

360364
```lua
@@ -394,7 +398,7 @@ Although, `Comment.nvim` supports neovim's `commentstring` but unfortunately it
394398

395399
- [`pre_hook`](#hooks) - If a string is returned from this method then it will be used for commenting.
396400

397-
- [`ft_table`](#languages) - If the current filetype is found in the table, then the string there will be used.
401+
- [`ft.lua`](#ft-lua) - If the current filetype is found in the table, then the string there will be used.
398402

399403
- `commentstring` - Neovim's native commentstring for the filetype
400404

@@ -420,10 +424,10 @@ The following object is provided as an argument to `pre_hook` and `post_hook` fu
420424

421425
---Range of the selection that needs to be commented
422426
---@class CommentRange
423-
---@field srow number Starting row
424-
---@field scol number Starting column
425-
---@field erow number Ending row
426-
---@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
427431
```
428432

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

lua/Comment/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ end
197197
---@param opmode OpMode
198198
---@param cfg? CommentConfig
199199
function api.uncomment_current_blockwise_op(opmode, cfg)
200-
Op.opfunc(opmode, cfg, U.cmode.uncomment, U.ctype.block, U.cmotion.line)
200+
Op.opfunc(opmode, cfg or Config:get(), U.cmode.uncomment, U.ctype.block, U.cmotion.line)
201201
end
202202

203203
--==========================================

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: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ local A = vim.api
55

66
local extra = {}
77

8-
---@param count number Line index
9-
---@param ctype CommentType
8+
-- FIXME This prints `a` in i_CTRL-o
9+
---Moves the cursor and enters INSERT mode
10+
---@param row integer Starting row
11+
---@param col integer Ending column
12+
local function move_n_insert(row, col)
13+
A.nvim_win_set_cursor(0, { row, col })
14+
A.nvim_feedkeys('a', 'ni', true)
15+
end
16+
17+
---@param count integer Line index
18+
---@param ctype integer
1019
---@param cfg CommentConfig
1120
local function ins_on_line(count, ctype, cfg)
1221
local row, col = unpack(A.nvim_win_get_cursor(0))
@@ -18,40 +27,39 @@ local function ins_on_line(count, ctype, cfg)
1827
ctype = ctype,
1928
range = { srow = row, scol = col, erow = row, ecol = col },
2029
}
21-
local lcs, rcs = U.parse_cstr(cfg, ctx)
2230

31+
local srow = row + count
32+
local lcs, rcs = U.parse_cstr(cfg, ctx)
2333
local line = A.nvim_get_current_line()
24-
local indent = U.grab_indent(line)
25-
local padding = U.get_padding(cfg.padding)
34+
local indent = U.indent_len(line)
35+
local padding = U.get_pad(cfg.padding)
2636

2737
-- We need RHS of cstr, if we are doing block comments or if RHS exists
2838
-- because even in line comment RHS do exists for some filetypes like jsx_element, ocaml
29-
local if_rcs = (ctype == U.ctype.block or rcs) and padding .. rcs or ''
39+
local if_rcs = U.is_empty(rcs) and rcs or padding .. rcs
3040

31-
local srow = row + count
32-
local ll = indent .. lcs .. padding
33-
A.nvim_buf_set_lines(0, srow, srow, false, { ll .. if_rcs })
34-
local erow, ecol = srow + 1, #ll - 1
35-
U.move_n_insert(erow, ecol)
41+
A.nvim_buf_set_lines(0, srow, srow, false, { table.concat({ string.rep(' ', indent), lcs, padding, if_rcs }) })
42+
43+
move_n_insert(srow + 1, indent + #lcs + #padding - 1)
3644
U.is_fn(cfg.post_hook, ctx)
3745
end
3846

3947
---Add a comment below the current line and goes to INSERT mode
40-
---@param ctype CommentType
48+
---@param ctype integer See |comment.utils.ctype|
4149
---@param cfg CommentConfig
4250
function extra.insert_below(ctype, cfg)
4351
ins_on_line(0, ctype, cfg)
4452
end
4553

4654
---Add a comment above the current line and goes to INSERT mode
47-
---@param ctype CommentType
55+
---@param ctype integer See |comment.utils.ctype|
4856
---@param cfg CommentConfig
4957
function extra.insert_above(ctype, cfg)
5058
ins_on_line(-1, ctype, cfg)
5159
end
5260

5361
---Add a comment at the end of current line and goes to INSERT mode
54-
---@param ctype CommentType
62+
---@param ctype integer See |comment.utils.ctype|
5563
---@param cfg CommentConfig
5664
function extra.insert_eol(ctype, cfg)
5765
local srow, scol = unpack(A.nvim_win_get_cursor(0))
@@ -66,16 +74,16 @@ function extra.insert_eol(ctype, cfg)
6674
local lcs, rcs = U.parse_cstr(cfg, ctx)
6775

6876
local line = A.nvim_get_current_line()
69-
local padding = U.get_padding(cfg.padding)
77+
local padding = U.get_pad(cfg.padding)
7078

7179
-- We need RHS of cstr, if we are doing block comments or if RHS exists
7280
-- because even in line comment RHS do exists for some filetypes like jsx_element, ocaml
73-
local if_rcs = rcs and padding .. rcs or ''
81+
local if_rcs = U.is_empty(rcs) and rcs or padding .. rcs
7482

7583
local ecol
7684
if U.is_empty(line) then
7785
-- If line is empty, start comment at the correct indentation level
78-
A.nvim_buf_set_lines(0, srow - 1, srow, false, { lcs .. padding .. if_rcs })
86+
A.nvim_set_current_line(lcs .. padding .. if_rcs)
7987
A.nvim_command('normal! ==')
8088
ecol = #A.nvim_get_current_line() - #if_rcs - 1
8189
else
@@ -84,12 +92,11 @@ function extra.insert_eol(ctype, cfg)
8492
-- 2. Other than that, I am assuming that the users wants a space b/w the end of line and start of the comment
8593
local space = vim.bo.filetype == 'python' and ' ' or ' '
8694
local ll = line .. space .. lcs .. padding
87-
95+
A.nvim_set_current_line(ll .. if_rcs)
8896
ecol = #ll - 1
89-
A.nvim_buf_set_lines(0, srow - 1, srow, false, { ll .. if_rcs })
9097
end
9198

92-
U.move_n_insert(srow, ecol)
99+
move_n_insert(srow, ecol)
93100
U.is_fn(cfg.post_hook, ctx)
94101
end
95102

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 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)

0 commit comments

Comments
 (0)