Skip to content

Commit

Permalink
fix: Checkboxes now carry information regarding their list item
Browse files Browse the repository at this point in the history
This might slightly impact performance!
  • Loading branch information
OXY2DEV committed Sep 28, 2024
1 parent 9923633 commit c7385c9
Showing 1 changed file with 74 additions and 38 deletions.
112 changes: 74 additions & 38 deletions lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ parser.parsed_content = {};
---@param buffer number
---@param TStree any
parser.md = function (buffer, TStree, from, to)
if not parser_installed("markdown") then
return;
end

--- "__inside_code_block" is still experimental
---@diagnostic disable
if not parser.cached_conf or
Expand Down Expand Up @@ -459,6 +463,7 @@ parser.md = function (buffer, TStree, from, to)
-- The node ends on the next line after the block quote
-- We will not count it

line_width = col_start + 1,
col_start = col_start,
col_end = largest_len
})
Expand Down Expand Up @@ -662,23 +667,45 @@ parser.md = function (buffer, TStree, from, to)
col_end = c_end
})
elseif capture_name == "checkbox_off" then
local list_item;

for _, extmark in ipairs(parser.parsed_content) do
if extmark.type == "list_item" and extmark.row_start == row_start then
list_item = extmark;
break;
end
end

table.insert(parser.parsed_content, {
node = capture_node,
type = "checkbox",
state = "incomplete",

list_item = list_item,

row_start = row_start,
row_end = row_end,

col_start = col_start,
col_end = col_end
})
elseif capture_name == "checkbox_on" then
local list_item;

for _, extmark in ipairs(parser.parsed_content) do
if extmark.type == "list_item" and extmark.row_start == row_start then
list_item = extmark;
break;
end
end

table.insert(parser.parsed_content, {
node = capture_node,
type = "checkbox",
state = "complete",

list_item = list_item,

row_start = row_start,
row_end = row_end,

Expand All @@ -694,6 +721,10 @@ end
---@param buffer number
---@param TStree any
parser.md_inline = function (buffer, TStree, from, to)
if not parser_installed("markdown_inline") then
return;
end

--- "__inside_code_block" is still experimental
---@diagnostic disable
if not parser.cached_conf or
Expand Down Expand Up @@ -759,6 +790,8 @@ parser.md_inline = function (buffer, TStree, from, to)
type = "checkbox",
state = capture_text:match("%[(.)%]"),

list_item = extmark,

row_start = row_start,
row_end = row_end,

Expand Down Expand Up @@ -927,19 +960,29 @@ parser.md_inline = function (buffer, TStree, from, to)
end

parser.html = function (buffer, TStree, from, to)
if not parser_installed("html") then
return;
end

--- "__inside_code_block" is still experimental
---@diagnostic disable
if not parser.cached_conf or
parser.cached_conf.__inside_code_block ~= true
then
---@diagnostic enable
local root = TStree:root();
local root_r_start, _, root_r_end, _ = root:range();
for _, tbl in ipairs(parser.parsed_content) do
if not tbl.type == "code_block" then
goto skip;
end

for _, range in ipairs(parser.avoid_ranges) do
if root_r_start >= range[1] and root_r_end <= range[2] then
local root = TStree:root();
local root_r_start, _, _, _ = root:range();

if root_r_start >= tbl.row_start and root_r_start <= tbl.row_end then
return;
end

::skip::
end
end

Expand Down Expand Up @@ -991,6 +1034,28 @@ parser.latex = function (buffer, TStree, from, to)
return;
end

--- "__inside_code_block" is still experimental
---@diagnostic disable
if not parser.cached_conf or
parser.cached_conf.__inside_code_block ~= true
then
---@diagnostic enable
for _, tbl in ipairs(parser.parsed_content) do
if not tbl.type == "code_block" then
goto skip;
end

local root = TStree:root();
local root_r_start, _, _, _ = root:range();

if root_r_start >= tbl.row_start and root_r_start <= tbl.row_end then
return;
end

::skip::
end
end

local scanned_queies = vim.treesitter.query.parse("latex", [[
((curly_group) @bracket)
Expand Down Expand Up @@ -1228,39 +1293,10 @@ end
--- Parsed data is stored as a "view" in renderer.lua
---
---@param buffer number
parser.init = function (buffer, config_table)
local root_parser = vim.treesitter.get_parser(buffer);
root_parser:parse(true);

if config_table then
parser.cached_conf = config_table;
end

-- Clear the previous contents
parser.parsed_content = {};

root_parser:for_each_tree(function (TStree, language_tree)
local tree_language = language_tree:lang();

if tree_language == "markdown" then
parser.md(buffer, TStree)
elseif tree_language == "markdown_inline" then
parser.md_inline(buffer, TStree);
elseif tree_language == "html" then
parser.html(buffer, TStree);
elseif tree_language == "latex" then
parser.latex(buffer, TStree);
end
end)

return parser.parsed_content;
end

parser.parse_range = function (buffer, config_table, from, to)
if not from or not to then
return {};
end

---@param config_table markview.configuration
---@param from integer?
---@param to integer?
parser.init = function (buffer, config_table, from, to)
local root_parser = vim.treesitter.get_parser(buffer);
root_parser:parse(true);

Expand All @@ -1275,7 +1311,7 @@ parser.parse_range = function (buffer, config_table, from, to)
local tree_language = language_tree:lang();

if tree_language == "markdown" then
parser.md(buffer, TStree, from, to);
parser.md(buffer, TStree, from, to)
elseif tree_language == "markdown_inline" then
parser.md_inline(buffer, TStree, from, to);
elseif tree_language == "html" then
Expand Down

0 comments on commit c7385c9

Please sign in to comment.