Skip to content

Commit

Permalink
emmylua docs
Browse files Browse the repository at this point in the history
  • Loading branch information
numToStr committed Dec 24, 2022
1 parent a2271b9 commit 1936e1d
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions lua/Comment/jsx.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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, {}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')

Expand All @@ -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

0 comments on commit 1936e1d

Please sign in to comment.