Skip to content

Commit 80dd5f1

Browse files
committed
fixes from stream
1 parent ed18b8a commit 80dd5f1

File tree

3 files changed

+65
-35
lines changed

3 files changed

+65
-35
lines changed

lua/Comment/ctx.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local ts = require('Comment.ts')
33
--- Comment context
44
---@class Ctx
55
---@field lang string: The name of the language where the cursor is
6-
---@field contained table: The containing node of where the cursor is
6+
---@field node table: The containing node of where the cursor is
77
---@field ctype CType
88
---@field cmode CMode
99
---@field cmotion CMotion
@@ -16,8 +16,9 @@ function Ctx:new(opts)
1616
assert(opts.cmotion, 'Must have a cmotion')
1717
assert(opts.ctype, 'Must have a ctype')
1818

19-
opts.lang = ts.get_lang()
20-
opts.contained = ts.get_containing_node()
19+
opts.lang = ts.get_lang(opts)
20+
opts.node = ts.get_node(opts)
21+
opts.node_type = opts.node and opts.node:type()
2122

2223
return setmetatable(opts, self)
2324
end

lua/Comment/ft.lua

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ local M = {
1010
latex = '%%s',
1111
}
1212

13+
local javascript_special_nodes = {
14+
comment = { M.cxx_l, M.cxx_b },
15+
jsx_attribute = { M.cxx_l, M.cxx_b },
16+
jsx_element = { '{/* %s */}', '{/* %s */}' },
17+
jsx_fragment = { '{/* %s */}', '{/* %s */}' },
18+
jsx_opening_element = { M.cxx_l, M.cxx_b },
19+
call_expression = { M.cxx_l, M.cxx_b },
20+
statement_block = { M.cxx_l, M.cxx_b },
21+
}
22+
1323
---Lang table that contains commentstring (linewise/blockwise) for mutliple filetypes
1424
---@type table { filetype = { linewise, blockwise } }
1525
local L = {
@@ -28,28 +38,8 @@ local L = {
2838
html = { M.html_b, M.html_b },
2939
idris = { M.dash, M.haskell_b },
3040
java = { M.cxx_l, M.cxx_b },
31-
javascript = {
32-
M.cxx_l,
33-
M.cxx_b,
34-
35-
jsx_fragment = { '{/* %s */}' },
36-
jsx_element = { '{/* %s */}' },
37-
jsx_attribute = { '// %s' },
38-
jsx_expression = { '// %s', '/*%s*/' },
39-
call_expression = { '// %s', '/*%s*/' },
40-
statement_block = { '// %s' },
41-
},
42-
javascriptreact = {
43-
M.cxx_l,
44-
M.cxx_b,
45-
46-
jsx_fragment = { '{/* %s */}' },
47-
jsx_element = { '{/* %s */}' },
48-
jsx_attribute = { '// %s' },
49-
jsx_expression = { '// %s', '/*%s*/' },
50-
call_expression = { '// %s', '/*%s*/' },
51-
statement_block = { '// %s' },
52-
},
41+
javascript = vim.tbl_deep_extend('keep', { M.cxx_l, M.cxx_b }, javascript_special_nodes),
42+
javascriptreact = vim.tbl_deep_extend('keep', { M.cxx_l, M.cxx_b }, javascript_special_nodes),
5343
julia = { M.hash, '#=%s=#' },
5444
lidris = { M.dash, M.haskell_b },
5545
lua = { M.dash, '--[[%s--]]' },
@@ -67,8 +57,8 @@ local L = {
6757
terraform = { M.hash, M.cxx_b },
6858
tex = { M.latex },
6959
toml = { M.hash },
70-
typescript = { M.cxx_l, M.cxx_b },
71-
typescriptreact = { M.cxx_l, M.cxx_b },
60+
typescript = vim.tbl_deep_extend('keep', { M.cxx_l, M.cxx_b }, javascript_special_nodes),
61+
typescriptreact = vim.tbl_deep_extend('keep', { M.cxx_l, M.cxx_b }, javascript_special_nodes),
7262
vim = { '"%s' },
7363
xml = { M.html_b, M.html_b },
7464
yaml = { M.hash },
@@ -91,16 +81,17 @@ return setmetatable({}, {
9181
end,
9282

9383
calculate = function(ctx)
84+
P(ctx)
9485
local lang = L[ctx.lang]
9586
if not lang then
9687
return
9788
end
9889

99-
if not ctx.contained then
90+
if not ctx.node then
10091
return lang[ctx.ctype] or lang[1]
10192
end
10293

103-
local config = lang[ctx.contained:type()]
94+
local config = lang[ctx.node:type()]
10495
if not config then
10596
return lang[ctx.ctype] or lang[1]
10697
end

lua/Comment/ts.lua

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
local ft = require('Comment.ft')
2+
local U = require('Comment.utils')
23

34
local ts = {}
45

5-
local get_current_tree = function(bufnr, range)
6+
local get_cursor_line_non_whitespace_col_location = function()
7+
local cursor = vim.api.nvim_win_get_cursor(0)
8+
local first_non_whitespace_col = vim.fn.match(vim.fn.getline('.'), '\\S')
9+
10+
return {
11+
cursor[1] - 1,
12+
first_non_whitespace_col,
13+
}
14+
end
15+
16+
--- Get the tree associated with the current comment
17+
---@param ctx Ctx
18+
local get_current_tree = function(ctx, bufnr, range)
619
if bufnr and not range then
720
error('If you pass bufnr, you must pass a range as well')
821
end
@@ -15,7 +28,20 @@ local get_current_tree = function(bufnr, range)
1528
end
1629

1730
if not range then
18-
local cursor = vim.api.nvim_win_get_cursor(0)
31+
local cursor
32+
if ctx.cmotion == U.cmotion.line then
33+
cursor = get_cursor_line_non_whitespace_col_location()
34+
elseif ctx.cmotion == U.cmotion.V then
35+
local region = { U.get_region('V') }
36+
cursor = { region[1] - 1, region[2] }
37+
elseif ctx.cmotion == U.cmotion.v then
38+
local region = { U.get_region('v') }
39+
cursor = { region[1] - 1, region[2] }
40+
else
41+
-- ctx.cmotion == U.cmotion.char
42+
cursor = vim.api.nvim_win_get_cursor(0)
43+
end
44+
1945
range = {
2046
cursor[1] - 1,
2147
cursor[2],
@@ -28,19 +54,24 @@ local get_current_tree = function(bufnr, range)
2854
end
2955

3056
--- Get the current language for bufnr and range
57+
---@param ctx Ctx: The current content
3158
---@param bufnr number: The bufnr
3259
---@param range table: See ranges for treesitter
33-
function ts.get_lang(bufnr, range)
34-
local current_tree = get_current_tree(bufnr, range)
60+
function ts.get_lang(ctx, bufnr, range)
61+
local current_tree = get_current_tree(ctx, bufnr, range)
3562
if not current_tree then
3663
return
3764
end
3865

3966
return current_tree:lang()
4067
end
4168

42-
function ts.get_containing_node(bufnr, in_range)
43-
local current_tree, range = get_current_tree(bufnr, in_range)
69+
--- Get corresponding node to the context
70+
---@param ctx Ctx: The current content
71+
---@param bufnr number: The buffer you are interested in. Generally nil
72+
---@param in_range table: The range you are interested in. Generally nil
73+
function ts.get_node(ctx, bufnr, in_range)
74+
local current_tree, range = get_current_tree(ctx, bufnr, in_range)
4475
if not current_tree then
4576
return
4677
end
@@ -52,6 +83,13 @@ function ts.get_containing_node(bufnr, in_range)
5283
return
5384
end
5485

86+
-- Short circuit if this ft doesn't have magical embedded languages
87+
-- inside of itself that randomly change just based on where you are
88+
-- in a syntax tree, instead of like, having the same rules everywhere.
89+
if vim.tbl_islist(config) then
90+
return nil
91+
end
92+
5593
local root = current_tree:trees()[1]:root()
5694
local contained = root:named_descendant_for_range(unpack(range))
5795

0 commit comments

Comments
 (0)