Skip to content

Commit

Permalink
style(renderer): Improved logic of renderer
Browse files Browse the repository at this point in the history
Removed unncessary codes from renderer. Added nil-check for config table
items.

Also added a second check for items that can have multiple configs(e.g.
Headings, list items, links etc.)
  • Loading branch information
OXY2DEV committed Jul 31, 2024
1 parent 4a93e15 commit 598bdad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 123 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# vim:nospell:
name: Release Please

on:
push:
Expand Down
10 changes: 4 additions & 6 deletions ftplugin/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ end

-- Check for requirements
if vim.fn.has("nvim-0.10") == 0 then
vim.print("[ markview.nvim ] : Thie plugin is only available on version 0.10.0 and higher!");
warn("[ markview.nvim ] : Thie plugin is only available on version 0.10.0 and higher!");
return;
elseif not parser_installed("markdown") then
vim.print("[ markview.nvim ] : Treesitter parser for 'markdown' wasn't found!");
warn("[ markview.nvim ] : Treesitter parser for 'markdown' wasn't found!");
return;
elseif not parser_installed("markdown_inline") then
vim.print("[ markview.nvim ] : Treesitter parser for 'markdown_inline' wasn't found!");
warn("[ markview.nvim ] : Treesitter parser for 'markdown_inline' wasn't found!");
return;
end

Expand Down Expand Up @@ -205,8 +205,6 @@ vim.api.nvim_create_autocmd(events, {

local current_range = markview.renderer.get_content_range(partial_contents);

vim.print(_G.__markview_render_ranges[event.buf])
-- Don't draw new things
if _G.__markview_render_ranges[event.buf] and vim.deep_equal(_G.__markview_render_ranges[event.buf], current_range) then
markview.renderer.clear_content_range(event.buf, partial_contents)
return;
Expand All @@ -215,7 +213,7 @@ vim.api.nvim_create_autocmd(events, {
markview.renderer.clear_content_range(event.buf, partial_contents)
markview.renderer.clear_content_range(event.buf, prev_contents);

markview.renderer.render_in_range(event.buf, prev_contents, markview.configuration, draw_start, draw_stop);
markview.renderer.render_in_range(event.buf, prev_contents, markview.configuration);
markview.renderer.update_range(event.buf, current_range);
end));
end
Expand Down
122 changes: 29 additions & 93 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,15 @@ renderer.views = {};
---@param content any
---@param config markview.render_config.headings
renderer.render_headings = function (buffer, content, config)
if config.enable == false then
if not config or config.enable == false then
return;
end

---@type markview.render_config.headings.h
local conf = config["heading_" .. content.level] or {};
local shift = config.shift_width or vim.bo[buffer].shiftwidth;

-- Do not proceed if config doesn't exist for a heading
if not conf then
return;
end
Expand Down Expand Up @@ -677,8 +678,8 @@ renderer.render_headings_s = function (buffer, content, config)

---@type markview.render_config.headings.h
local conf = content.marker:match("=") and config["setext_1"] or config["setext_2"];
local shift = config.shift_width or vim.bo[buffer].shiftwidth;

-- Do not proceed if setext headings don't have configuraton
if not conf then
return;
end
Expand Down Expand Up @@ -738,7 +739,7 @@ end
---@param content any
---@param config_table markview.render_config.code_blocks
renderer.render_code_blocks = function (buffer, content, config_table)
if config_table == nil or config_table.enable == false then
if not config_table or config_table.enable == false then
return;
end

Expand Down Expand Up @@ -893,7 +894,7 @@ end
renderer.render_block_quotes = function (buffer, content, config_table)
local qt_config;

if config_table.enable == false then
if not config_table or config_table.enable == false then
return;
end

Expand All @@ -917,6 +918,11 @@ renderer.render_block_quotes = function (buffer, content, config_table)
qt_config = config_table.default;
end

-- Config for a block quote is not available
if not qt_config then
return;
end

if qt_config.custom_title == true and content.title ~= "" then
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start, {
virt_text_pos = "inline",
Expand Down Expand Up @@ -985,7 +991,7 @@ end
renderer.render_horizontal_rules = function (buffer, content, config_table)
local virt_text = {};

if config_table.enable == false then
if not config_table or config_table.enable == false then
return;
end

Expand Down Expand Up @@ -1039,7 +1045,7 @@ end
renderer.render_links = function (buffer, content, config_table)
local lnk_conf;

if config_table.enable == false then
if not config_table or config_table.enable == false then
return;
end

Expand All @@ -1051,6 +1057,11 @@ renderer.render_links = function (buffer, content, config_table)
lnk_conf = config_table.emails;
end

-- Do not render links with no config
if not lnk_conf then
return;
end

vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.link_type == "email_autolink" and content.col_start or content.col_start + 1, {
virt_text_pos = "inline",
virt_text = {
Expand Down Expand Up @@ -1082,7 +1093,7 @@ end
---@param content any
---@param config_table markview.render_config.inline_codes
renderer.render_inline_codes = function (buffer, content, config_table)
if config_table.enable == false then
if not config_table or config_table.enable == false then
return;
end

Expand Down Expand Up @@ -1111,7 +1122,7 @@ end
---@param content any
---@param config_table markview.render_config.list_items
renderer.render_lists = function (buffer, content, config_table)
if config_table.enable == false then
if not config_table or config_table.enable == false then
return;
end

Expand All @@ -1127,6 +1138,11 @@ renderer.render_lists = function (buffer, content, config_table)
ls_conf = config_table.marker_dot or {};
end

-- Do not render list types with no configuraton
if not ls_conf then
return;
end

local use_text = ls_conf.text or content.marker_symbol;

if ls_conf.add_padding == true then
Expand Down Expand Up @@ -1176,7 +1192,7 @@ end
---@param content any
---@param config_table markview.render_config.checkboxes
renderer.render_checkboxes = function (buffer, content, config_table)
if config_table.enable == false then
if not config_table or config_table.enable == false then
return;
end

Expand All @@ -1190,7 +1206,7 @@ renderer.render_checkboxes = function (buffer, content, config_table)
chk_config = config_table.pending;
end

if type(chk_config.text) ~= "string" then
if not chk_config or type(chk_config.text) ~= "string" then
return;
end

Expand All @@ -1212,7 +1228,7 @@ end
---@param content any
---@param user_config markview.config
renderer.render_tables = function (buffer, content, user_config)
if user_config.tables == nil or user_config.tables.enable == false then
if not user_config.tables or user_config.tables.enable == false then
return;
end

Expand All @@ -1229,57 +1245,7 @@ renderer.render_tables = function (buffer, content, user_config)
end
end




--- CursorMove listener
renderer.autocmd = nil;

renderer.create_autocmd = function (config_table)
if renderer.autocmd then
return;
end

local events = { "CursorMovedI" };

-- if config_table.modes and vim.list_contains(config_table.modes, "i") then
-- table.insert(events, "CursorMovedI");
-- end

renderer.autocmd = vim.api.nvim_create_autocmd(events, {
pattern = config_table.filetypes or "*.md", -- Currently only for markdown
callback = function (event)
local buffer = event.buf;
local mode = vim.api.nvim_get_mode().mode;

if not vim.list_contains(config_table.modes or {}, mode) then
return;
end

renderer.render_deleted_items(buffer, config_table);
renderer.removed_elements[buffer] = {};

if not vim.list_contains(config_table.special_modes or { "i" }, mode) then
return;
end

-- This is for testing purposes
local cursor = vim.api.nvim_win_get_cursor(0);
local comps = {};

for _, component in ipairs(_G.__markview_views[buffer] or {}) do
if (cursor[1] - 1) >= component.row_start and cursor[1] - 1 <= component.row_end then
table.insert(comps, component);
table.insert(renderer.removed_elements[buffer], component);
end
end

renderer.destroy(buffer)
end
})
end

renderer.render_in_range = function (buffer, partial_contents, config_table, from, to)
renderer.render_in_range = function (buffer, partial_contents, config_table)
for _, content in ipairs(partial_contents) do
local type = content.type;
local fold_closed = vim.fn.foldclosed(content.row_start + 1);
Expand All @@ -1288,11 +1254,6 @@ renderer.render_in_range = function (buffer, partial_contents, config_table, fro
goto extmark_skipped;
end

-- if content.row_start < from or content.row_end > to then
-- goto extmark_skipped;
-- end


if type == "heading_s" then
pcall(renderer.render_headings_s, buffer, content, config_table.headings);
elseif type == "heading" then
Expand Down Expand Up @@ -1330,6 +1291,7 @@ renderer.render = function (buffer, parsed_content, config_table, conceal_start,
_G.__markview_views[buffer] = parsed_content;
end

-- Prevents errors caused by buffer ranges being nil
if _G.__markview_render_ranges and _G.__markview_render_ranges[buffer] then
_G.__markview_render_ranges[buffer] = {};
end
Expand Down Expand Up @@ -1380,30 +1342,6 @@ renderer.clear = function (buffer)
vim.api.nvim_buf_clear_namespace(buffer, renderer.namespace, 0, -1)
end















renderer.update = function (buffer, parsed_content)
if not _G.__markview_views then
_G.__markview_views = {};
end

if parsed_content ~= nil then
_G.__markview_views[buffer] = parsed_content;
end
end

renderer.update_range = function (buffer, new_range)
if not _G.__markview_render_ranges then
_G.__markview_render_ranges = {};
Expand All @@ -1416,8 +1354,6 @@ renderer.update_range = function (buffer, new_range)
if new_range and not vim.deep_equal(_G.__markview_render_ranges[buffer], new_range) then
_G.__markview_render_ranges[buffer] = new_range;
end

-- error("nil range")
end

renderer.clear_content_range = function (buffer, parsed_content)
Expand Down
24 changes: 0 additions & 24 deletions test.md

This file was deleted.

0 comments on commit 598bdad

Please sign in to comment.