From 89a441a0039a141fd784d0b40b6ac30bb1a8a163 Mon Sep 17 00:00:00 2001 From: numToStr Date: Wed, 23 Mar 2022 21:34:29 +0530 Subject: [PATCH 1/8] 1st draft --- a.jsx | 57 +++++++++++++++++++++++++++++++ a.md | 83 +++++++++++++++++++++++++++++++++++++++++++++ lua/Comment/ft.lua | 1 + lua/Comment/jsx.lua | 57 +++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 a.jsx create mode 100644 a.md create mode 100644 lua/Comment/jsx.lua diff --git a/a.jsx b/a.jsx new file mode 100644 index 00000000..193fc61d --- /dev/null +++ b/a.jsx @@ -0,0 +1,57 @@ +function Yo() { + return ( + <> +
hello
+ + ); +} + +const Yoo = () => { + return ( +
+
+

hello

+

{ + return ( +

+

IDK

+
+ ); + }, + }} + > + hello +

+
+
+ ); +}; + +class Yooo { + render() { + return ( + <> +
hello
+ + ); + } +} + +const Yoooo = () => ( + <> +
hello
+ +); + +const Yoooo = () => ( +
+
hello
+
+); + +function Yoooo() { + return
hello
; +} diff --git a/a.md b/a.md new file mode 100644 index 00000000..65e43ea2 --- /dev/null +++ b/a.md @@ -0,0 +1,83 @@ +# this is markdown + +```javascript +function Yo() { + return ( + <> +
hello
+ + ); +} +``` + +```javascript +const Yoo = () => { + return ( +
+
+

hello

+

{ + return ( +

+

IDK

+
+ ); + }, + }} + > + hello +

+
+
+ ); +}; +``` + +```tsx +const Yoooo = () => ( + <> +
hello
+ +); +``` + +```tsx +const Yoooo = () => ( +
+
hello
+
+); +``` + +```tsx +function Yo() { + return
hello
; +} +``` + +```tsx +function Yo() { + return <>; +} +``` + +```tsx +class Yooo { + render() { + return ( + <> +
hello
+ + ); + } +} +``` + + + +```lua +local a = 123 +``` diff --git a/lua/Comment/ft.lua b/lua/Comment/ft.lua index 812eacd2..ac922e04 100644 --- a/lua/Comment/ft.lua +++ b/lua/Comment/ft.lua @@ -151,6 +151,7 @@ local L = setmetatable({ tmux = { M.hash }, toml = { M.hash }, twig = { M.twig, M.twig }, + tsx = { M.cxx_l, M.cxx_b }, -- Alias for `typescriptreact` bcz `tsx` is the parser's name typescript = { M.cxx_l, M.cxx_b }, typescriptreact = { M.cxx_l, M.cxx_b }, typst = { M.cxx_l, M.cxx_b }, diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua new file mode 100644 index 00000000..ff053542 --- /dev/null +++ b/lua/Comment/jsx.lua @@ -0,0 +1,57 @@ +local J = { + comment = '{/*%s*/}', +} + +local query = [[ + (parenthesized_expression + [(jsx_fragment) (jsx_element)] @jsx) + + (return_statement + [(jsx_fragment) (jsx_element)] @jsx) +]] + +local function is_jsx(lang) + -- Name of the treesitter parsers that supports jsx syntax + return lang == 'tsx' or lang == 'javascript' +end + +local function capture(child, range) + local lang = child:lang() + + if not is_jsx(lang) then + return + end + + local Q = vim.treesitter.query.parse_query(lang, query) + + for _, tree in ipairs(child:trees()) do + for _, node in Q:iter_captures(tree:root(), child:source()) do + local srow, _, erow = node:range() + -- Why subtracting 2? + -- 1. Lua indexes starts from 1 + -- 2. Removing the `return` keyword from the range + if srow <= range.srow - 2 and erow >= range.erow then + return J.comment + end + end + end +end + +function J.calculate(ctx) + local ok, P = pcall(vim.treesitter.get_parser) + + if not ok then + return + end + + for _, child in pairs(P:children()) do + local captured = capture(child, ctx.range) + if captured then + return captured + end + end + + return capture(P, ctx.range) +end + +return J From 5541a4ff0e192dec497079e4fb2434bd12e6a318 Mon Sep 17 00:00:00 2001 From: numToStr Date: Thu, 24 Mar 2022 13:20:46 +0530 Subject: [PATCH 2/8] 2st draft --- a.md | 2 ++ lua/Comment/jsx.lua | 41 +++++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/a.md b/a.md index 65e43ea2..e21d32d3 100644 --- a/a.md +++ b/a.md @@ -17,6 +17,8 @@ const Yoo = () => {

hello

{ diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua index ff053542..ed3b37c9 100644 --- a/lua/Comment/jsx.lua +++ b/lua/Comment/jsx.lua @@ -1,36 +1,41 @@ local J = { comment = '{/*%s*/}', + valid = { 'jsx_element', 'jsx_fragment', 'jsx_text', '<', '>' }, } -local query = [[ - (parenthesized_expression - [(jsx_fragment) (jsx_element)] @jsx) - - (return_statement - [(jsx_fragment) (jsx_element)] @jsx) -]] - -local function is_jsx(lang) +local function is_jsx_tree(lang) -- Name of the treesitter parsers that supports jsx syntax return lang == 'tsx' or lang == 'javascript' end +local function is_jsx_node(node) + if not node then + return false + end + return vim.tbl_contains(J.valid, node:type()) +end + local function capture(child, range) local lang = child:lang() - if not is_jsx(lang) then + local rng = { + range.srow - 1, + range.scol, + range.erow - 1, + range.ecol, + } + + if not (is_jsx_tree(lang) and child:contains(rng)) then return end - local Q = vim.treesitter.query.parse_query(lang, query) - for _, tree in ipairs(child:trees()) do - for _, node in Q:iter_captures(tree:root(), child:source()) do - local srow, _, erow = node:range() - -- Why subtracting 2? - -- 1. Lua indexes starts from 1 - -- 2. Removing the `return` keyword from the range - if srow <= range.srow - 2 and erow >= range.erow then + local root = tree:root() + local node = root:descendant_for_range(unpack(rng)) + local srow, _, erow = node:range() + if srow <= range.srow - 1 and erow >= range.erow - 1 then + local nxt, prev = node:next_sibling(), node:prev_sibling() + if is_jsx_node(prev) or is_jsx_node(node) or is_jsx_node(nxt) then return J.comment end end From 4de10dcaa04588f0c37957d281d1a7e06e3d7fc1 Mon Sep 17 00:00:00 2001 From: numToStr Date: Thu, 24 Mar 2022 19:09:13 +0530 Subject: [PATCH 3/8] 3rd draft --- a.jsx | 85 +++++++++++++++++++++++++------------------ a.md | 6 ++++ lua/Comment/jsx.lua | 87 +++++++++++++++++++++++++++++---------------- 3 files changed, 113 insertions(+), 65 deletions(-) diff --git a/a.jsx b/a.jsx index 193fc61d..eb499ad1 100644 --- a/a.jsx +++ b/a.jsx @@ -1,17 +1,11 @@ -function Yo() { - return ( - <> -

hello
- - ); -} - const Yoo = () => { return (

hello

{ @@ -25,33 +19,56 @@ const Yoo = () => { > hello

+

{true ? "true" : "false"}

+

{true && "true"}

+

+ {true && ( +

+

This is awesome

+
+ )} +

+
+ {getUser({ + name: "numToStr", + job: "making plugins", + })} +
); }; - -class Yooo { - render() { - return ( - <> -
hello
- - ); - } -} - -const Yoooo = () => ( - <> -
hello
- -); - -const Yoooo = () => ( -
-
hello
-
-); - -function Yoooo() { - return
hello
; -} +// +// const Yoooo = () => ( +//
+//
hello
+//
+// ); +// +// function Yo() { +// return ( +// <> +//
hello
+// +// ); +// } +// +// class Yooo { +// render() { +// return ( +// <> +//
hello
+// +// ); +// } +// } +// +// const Yoooo = () => ( +// <> +//
hello
+// +// ); +// +// function Yoooo() { +// return
hello
; +// } diff --git a/a.md b/a.md index e21d32d3..0ab3e28f 100644 --- a/a.md +++ b/a.md @@ -32,6 +32,12 @@ const Yoo = () => { > hello

+
+ {getUser({ + name: "numToStr", + job: "making plugins", + })} +
); diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua index ed3b37c9..9e608f9c 100644 --- a/lua/Comment/jsx.lua +++ b/lua/Comment/jsx.lua @@ -1,45 +1,57 @@ local J = { comment = '{/*%s*/}', - valid = { 'jsx_element', 'jsx_fragment', 'jsx_text', '<', '>' }, } -local function is_jsx_tree(lang) +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 + [(object) (call_expression)] @nojsx) + + (parenthesized_expression + [(jsx_fragment) (jsx_element)] @jsx) + + (return_statement + [(jsx_fragment) (jsx_element)] @jsx) +]] + +local function is_jsx(lang) -- Name of the treesitter parsers that supports jsx syntax return lang == 'tsx' or lang == 'javascript' end -local function is_jsx_node(node) - if not node then - return false - end - return vim.tbl_contains(J.valid, node:type()) -end - -local function capture(child, range) - local lang = child:lang() +local function capture(parser, range) + local lang = parser:lang() - local rng = { - range.srow - 1, - range.scol, - range.erow - 1, - range.ecol, - } - - if not (is_jsx_tree(lang) and child:contains(rng)) then + if not is_jsx(lang) then return end - for _, tree in ipairs(child:trees()) do - local root = tree:root() - local node = root:descendant_for_range(unpack(rng)) - local srow, _, erow = node:range() - if srow <= range.srow - 1 and erow >= range.erow - 1 then - local nxt, prev = node:next_sibling(), node:prev_sibling() - if is_jsx_node(prev) or is_jsx_node(node) or is_jsx_node(nxt) then - return J.comment + local Q = vim.treesitter.query.parse_query(lang, query) + + local lines, group + + 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] + end end end end + + return group == 'jsx' and J.comment end function J.calculate(ctx) @@ -49,14 +61,27 @@ function J.calculate(ctx) return end + local rng = { + ctx.range.srow - 1, + ctx.range.scol, + ctx.range.erow - 1, + ctx.range.ecol, + } + + -- This is for `markdown` which embeds multiple `tsx` blocks for _, child in pairs(P:children()) do - local captured = capture(child, ctx.range) - if captured then - return captured + if child:contains(rng) then + local captured = capture(child, ctx.range) + if captured then + return captured + end end end - return capture(P, ctx.range) + if P:contains(rng) then + -- This is for `tsx` itself + return capture(P, ctx.range) + end end return J From 5814d553c1d31b139b2365930402e01c3cde6f9e Mon Sep 17 00:00:00 2001 From: numToStr Date: Thu, 26 May 2022 10:50:17 +0530 Subject: [PATCH 4/8] it's now working --- a.jsx | 74 ------------------------------- a.md | 91 -------------------------------------- lua/Comment/jsx.lua | 103 ++++++++++++++++++++++++++++++++------------ 3 files changed, 75 insertions(+), 193 deletions(-) delete mode 100644 a.jsx delete mode 100644 a.md diff --git a/a.jsx b/a.jsx deleted file mode 100644 index eb499ad1..00000000 --- a/a.jsx +++ /dev/null @@ -1,74 +0,0 @@ -const Yoo = () => { - return ( -
-
-

hello

-

{ - return ( -

-

IDK

-
- ); - }, - }} - > - hello -

-

{true ? "true" : "false"}

-

{true && "true"}

-

- {true && ( -

-

This is awesome

-
- )} -

-
- {getUser({ - name: "numToStr", - job: "making plugins", - })} -
-
-
- ); -}; -// -// const Yoooo = () => ( -//
-//
hello
-//
-// ); -// -// function Yo() { -// return ( -// <> -//
hello
-// -// ); -// } -// -// class Yooo { -// render() { -// return ( -// <> -//
hello
-// -// ); -// } -// } -// -// const Yoooo = () => ( -// <> -//
hello
-// -// ); -// -// function Yoooo() { -// return
hello
; -// } diff --git a/a.md b/a.md deleted file mode 100644 index 0ab3e28f..00000000 --- a/a.md +++ /dev/null @@ -1,91 +0,0 @@ -# this is markdown - -```javascript -function Yo() { - return ( - <> -
hello
- - ); -} -``` - -```javascript -const Yoo = () => { - return ( -
-
-

hello

-

{ - return ( -

-

IDK

-
- ); - }, - }} - > - hello -

-
- {getUser({ - name: "numToStr", - job: "making plugins", - })} -
-
-
- ); -}; -``` - -```tsx -const Yoooo = () => ( - <> -
hello
- -); -``` - -```tsx -const Yoooo = () => ( -
-
hello
-
-); -``` - -```tsx -function Yo() { - return
hello
; -} -``` - -```tsx -function Yo() { - return <>; -} -``` - -```tsx -class Yooo { - render() { - return ( - <> -
hello
- - ); - } -} -``` - - - -```lua -local a = 123 -``` diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua index 9e608f9c..f911576e 100644 --- a/lua/Comment/jsx.lua +++ b/lua/Comment/jsx.lua @@ -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) @@ -19,11 +17,60 @@ 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 +---@return boolean 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, section, sections = 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 boolean local function capture(parser, range) local lang = parser:lang() @@ -33,35 +80,40 @@ 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 + 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 not lines or region < lines then - lines, group = region, Q.captures[id] + id, lines = section.id, region end end end end - return group == 'jsx' and J.comment + return Q.captures[id] == 'jsx' 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, tree = pcall(vim.treesitter.get_parser, buf, trees[filetype] or filetype) if not ok then return end - local rng = { + local range = { ctx.range.srow - 1, ctx.range.scol, ctx.range.erow - 1, @@ -69,19 +121,14 @@ 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) - if captured then - return captured - end + for _, child in pairs(tree:children()) do + if child:contains(range) and capture(child, ctx.range) then + return J.comment end end - if P:contains(rng) then - -- This is for `tsx` itself - return capture(P, ctx.range) - end + -- This is for `tsx` itself + return (tree:contains(range) and capture(tree, ctx.range)) and J.comment end return J From e73021db2ee92152c3d34016e745d417b53eec93 Mon Sep 17 00:00:00 2001 From: numToStr Date: Thu, 2 Jun 2022 15:06:22 +0530 Subject: [PATCH 5/8] fix: self closing element --- lua/Comment/jsx.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua index f911576e..a5e9b981 100644 --- a/lua/Comment/jsx.lua +++ b/lua/Comment/jsx.lua @@ -5,6 +5,8 @@ local J = { local query = [[ (jsx_opening_element [(jsx_attribute) (comment)] @nojsx) + (jsx_self_closing_element [(jsx_attribute) (comment)] @nojsx) + ((jsx_expression (comment)) @jsx) (jsx_expression From bf3a996c056b23f7b958d50162bb21dc844106c7 Mon Sep 17 00:00:00 2001 From: numToStr Date: Thu, 16 Jun 2022 14:14:06 +0530 Subject: [PATCH 6/8] fix: invalid comment when element and attr is on the same line --- lua/Comment/jsx.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua index a5e9b981..d6569978 100644 --- a/lua/Comment/jsx.lua +++ b/lua/Comment/jsx.lua @@ -82,19 +82,26 @@ local function capture(parser, range) local Q = vim.treesitter.query.parse_query(lang, query) - local id, lines = 0, nil + local id, lnum, lines = 0, nil, nil for _, tree in ipairs(parser:trees()) do 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 not lines or region < lines then - id, lines = section.id, region + id, lnum, lines = section.id, section.range.srow, region end end end end + -- NOTE: + -- This is for the case when the opening element and attributes are on the same line, + -- so to prevent invalid comment, we can check if the line looks like an element + if lnum ~= nil and string.match(vim.fn.getline(lnum + 1), '%s+<%w+%s.*>$') then + return true + end + return Q.captures[id] == 'jsx' end From f32dff8c9adabe535925dfc9aff022b83d6a4b66 Mon Sep 17 00:00:00 2001 From: numToStr Date: Sat, 20 Aug 2022 14:20:27 +0530 Subject: [PATCH 7/8] emmylua docs --- lua/Comment/jsx.lua | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua index d6569978..1b5fbea1 100644 --- a/lua/Comment/jsx.lua +++ b/lua/Comment/jsx.lua @@ -1,8 +1,15 @@ -local J = { - comment = '{/*%s*/}', -} +---@mod comment.jsx JSX Integration +---@brief [[ +---This module provides the jsx/tsx comment integration via `pre_hook`. The default +---treesitter integration doesn't provides tsx/jsx support as the syntax is weird +---enough to deserve its own module. Besides that not everyone is using jsx/tsx +---so it's better make it an ad-hoc integration. +---@brief ]] + +local jsx = {} local query = [[ + ;; query (jsx_opening_element [(jsx_attribute) (comment)] @nojsx) (jsx_self_closing_element [(jsx_attribute) (comment)] @nojsx) @@ -38,7 +45,7 @@ end ---@param q table ---@param tree table ---@param parser table ----@param range CRange +---@param range CommentRange ---@return table local function normalize(q, tree, parser, range) local prev, section, sections = nil, 0, {} @@ -71,13 +78,13 @@ end ---Runs the query and returns the commentstring by checking the cursor range ---@param parser table ----@param range CRange +---@param range CommentRange ---@return boolean local function capture(parser, range) local lang = parser:lang() if not is_jsx(lang) then - return + return false end local Q = vim.treesitter.query.parse_query(lang, query) @@ -105,10 +112,20 @@ local function capture(parser, range) return Q.captures[id] == 'jsx' end ----Calculates the `jsx` commentstring ----@param ctx Ctx ----@return string? -function J.calculate(ctx) +---Static |commentstring| for jsx/tsx +---@type string +jsx.commentstring = '{/*%s*/}' + +---Calculates the `jsx/tsx` commentstring using |treesitter| +---@param ctx CommentCtx +---@return string? _ jsx/tsx commenstring +---@see comment.utils.CommentCtx +---@usage [[ +---require('Comment').setup({ +--- pre_hook = require('Comment.jsx').calculate +---}) +---@usage ]] +function jsx.calculate(ctx) local buf = vim.api.nvim_get_current_buf() local filetype = vim.api.nvim_buf_get_option(buf, 'filetype') @@ -132,12 +149,12 @@ function J.calculate(ctx) -- This is for `markdown` which embeds multiple `tsx` blocks for _, child in pairs(tree:children()) do if child:contains(range) and capture(child, ctx.range) then - return J.comment + return jsx.commentstring end end -- This is for `tsx` itself - return (tree:contains(range) and capture(tree, ctx.range)) and J.comment + return (tree:contains(range) and capture(tree, ctx.range)) and jsx.commentstring or nil end -return J +return jsx From 94f7a2919b725d7f766f83608201c42d3ce3e12e Mon Sep 17 00:00:00 2001 From: numToStr Date: Wed, 12 Apr 2023 14:17:21 +0530 Subject: [PATCH 8/8] 0.9 updates --- lua/Comment/jsx.lua | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/lua/Comment/jsx.lua b/lua/Comment/jsx.lua index 1b5fbea1..b0d2a3b8 100644 --- a/lua/Comment/jsx.lua +++ b/lua/Comment/jsx.lua @@ -26,16 +26,11 @@ 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 ---@return boolean local function is_jsx(lang) - return lang == trees.typescriptreact or lang == trees.javascriptreact + return lang == 'tsx' or lang == 'javascript' end -- This function is a workaround for `+` treesitter quantifier @@ -87,7 +82,7 @@ local function capture(parser, range) return false end - local Q = vim.treesitter.query.parse_query(lang, query) + local Q = vim.treesitter.query.parse(lang, query) local id, lnum, lines = 0, nil, nil @@ -126,14 +121,7 @@ jsx.commentstring = '{/*%s*/}' ---}) ---@usage ]] function jsx.calculate(ctx) - 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, tree = pcall(vim.treesitter.get_parser, buf, trees[filetype] or filetype) + local ok, tree = pcall(vim.treesitter.get_parser, vim.api.nvim_get_current_buf()) if not ok then return