Skip to content

Commit

Permalink
it's now working
Browse files Browse the repository at this point in the history
  • Loading branch information
numToStr committed May 26, 2022
1 parent 512e78b commit 5867947
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 55 deletions.
72 changes: 37 additions & 35 deletions a.jsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
const Yoo = () => {
return (
<div>
<section>
<p>hello</p>
<p
he="llo"
wor="ld"
attr={{
...window,
hello: () => {
return (
<section>
<p>IDK</p>
</section>
);
},
}}
>
hello
</p>
<p>{true ? "true" : "false"}</p>
<p>{true && "true"}</p>
<p>
{true && (
return (
<div>
<section>
<p>This is awesome</p>
<p>hello</p>
<p
he="llo"
wor="ld"
attr={{
...window,
hello: () => {
return (
<section>
<p>IDK</p>
<p>IDK</p>
<p>IDK</p>
</section>
);
},
}}
>
hello
</p>
<p>{true ? "true" : "false"}</p>
<p>{true && "true"}</p>
<p>
{true && (
<section>
<p>This is awesome</p>
</section>
)}
</p>
<div id="div">
{getUser({
name: "numToStr",
job: "making plugins",
})}
</div>
</section>
)}
</p>
<div id="div">
{getUser({
name: "numToStr",
job: "making plugins",
})}
</div>
</section>
</div>
);
);
};
//
// const Yoooo = () => (
Expand Down
91 changes: 71 additions & 20 deletions lua/Comment/jsx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ local J = {
}

local query = [[
; If somehow we can group all the attributes into one
(jsx_opening_element [(jsx_attribute) (comment)] @nojsx)
; If somehow we can group all the comments into one
(jsx_expression (comment)) @jsx
((jsx_expression (comment)) @jsx)
(jsx_expression
[(object) (call_expression)] @nojsx)
Expand All @@ -19,11 +17,59 @@ local query = [[
[(jsx_fragment) (jsx_element)] @jsx)
]]

local trees = {
typescriptreact = 'tsx',
javascriptreact = 'javascript',
}

---Checks whether parser's language matches the filetype that supports jsx syntax
---@param lang string
local function is_jsx(lang)
-- Name of the treesitter parsers that supports jsx syntax
return lang == 'tsx' or lang == 'javascript'
return lang == trees.typescriptreact or lang == trees.javascriptreact
end

-- This function is a workaround for `+` treesitter quantifier
-- which is currently not supported by neovim (wip: https://github.com/neovim/neovim/pull/15330)
-- because of this we can't query consecutive comment or attributes nodes,
-- and group them as single range, hence this function
---@param q table
---@param tree table
---@param parser table
---@param range CRange
---@return table
local function normalize(q, tree, parser, range)
local prev, sections, section = nil, {}, 0

for id, node in q:iter_captures(tree:root(), parser:source(), range.srow - 1, range.erow) do
if id ~= prev then
section = section + 1
end

local srow, _, erow = node:range()
local key = string.format('%s.%s', id, section)
if sections[key] == nil then
sections[key] = { id = id, range = { srow = srow, erow = erow } }
else
-- storing the smallest starting row and biggest ending row
local r = sections[key].range
if srow < r.srow then
sections[key].range.srow = srow
end
if erow > r.erow then
sections[key].range.erow = erow
end
end

prev = id
end

return sections
end

---Runs the query and returns the commentstring by checking the cursor range
---@param parser table
---@param range CRange
---@return string?
local function capture(parser, range)
local lang = parser:lang()

Expand All @@ -33,29 +79,34 @@ local function capture(parser, range)

local Q = vim.treesitter.query.parse_query(lang, query)

local lines, group
local id, lines = 0, nil

for _, tree in ipairs(parser:trees()) do
for id, node in Q:iter_captures(tree:root(), parser:source(), range.srow - 1, range.erow) do
local srow, _, erow = node:range()
-- print(Q.captures[id])
-- print(srow, range.srow - 1)
-- print(erow, range.erow - 1)
-- print(srow <= range.srow - 1 and erow >= range.erow - 1)
if srow <= range.srow - 1 and erow >= range.erow - 1 then
local region = erow - srow
if not lines or region < lines then
lines, group = region, Q.captures[id]
for _, section in pairs(normalize(Q, tree, parser, range)) do
if section.range.srow <= range.srow - 1 and section.range.erow >= range.erow - 1 then
local region = section.range.erow - section.range.srow
if lines == nil or region < lines then
id, lines = section.id, region
end
end
end
end

return group == 'jsx' and J.comment
return Q.captures[id] == 'jsx' and J.comment
end

---Calculates the `jsx` commentstring
---@param ctx Ctx
---@return string?
function J.calculate(ctx)
local ok, P = pcall(vim.treesitter.get_parser)
local buf = vim.api.nvim_get_current_buf()
local filetype = vim.api.nvim_buf_get_option(buf, 'filetype')

-- NOTE:
-- `get_parser` panics for `{type,java}scriptreact` filetype
-- bcz their parser's name is different from their filetype
-- Maybe report the issue to `nvim-treesitter` or core(?)
local ok, P = pcall(vim.treesitter.get_parser, buf, trees[filetype] or filetype)

if not ok then
return
Expand All @@ -71,15 +122,15 @@ function J.calculate(ctx)
-- This is for `markdown` which embeds multiple `tsx` blocks
for _, child in pairs(P:children()) do
if child:contains(rng) then
local captured = capture(child, ctx.range)
local _, captured = pcall(capture, child, ctx.range)
if captured then
return captured
end
end
end

-- This is for `tsx` itself
if P:contains(rng) then
-- This is for `tsx` itself
return capture(P, ctx.range)
end
end
Expand Down

0 comments on commit 5867947

Please sign in to comment.