Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support nested (injected) code blocks #138

Merged
merged 1 commit into from
Apr 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions lua/Comment/ft.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,32 +117,41 @@ function ft.lang(lang)
return L[lang]
end

---Get the commenstring by walking the tree recursively
---NOTE: This ignores `comment` parser as this is useless
---@param tree userdata Tree to be walked
---@param range number[] Range to check for
---@return userdata
function ft.contains(tree, range)
for lang, child in pairs(tree:children()) do
if lang ~= 'comment' and child:contains(range) then
return ft.contains(child, range)
end
end

return tree
end

---Calculate commentstring w/ the power of treesitter
---@param ctx Ctx
---@return string
function ft.calculate(ctx)
local buf = A.nvim_get_current_buf()
local ok, langtree = pcall(vim.treesitter.get_parser, buf)
local ok, parser = pcall(vim.treesitter.get_parser, buf)
local default = ft.get(A.nvim_buf_get_option(buf, 'filetype'), ctx.ctype)

if not ok then
return default
end

local range = {
local lang = ft.contains(parser, {
ctx.range.srow - 1,
ctx.range.scol,
ctx.range.erow - 1,
ctx.range.ecol,
}

for lang, tree in pairs(langtree:children()) do
if tree:contains(range) then
return ft.get(lang, ctx.ctype) or default
end
end
}):lang()

return default
return ft.get(lang, ctx.ctype) or default
end

return setmetatable(ft, {
Expand Down