Skip to content

Commit

Permalink
feat: Added hybrid mode to the plugin
Browse files Browse the repository at this point in the history
fix: Improved rendering mechanics

fix: Replaced options with callbacks

fix: Tweaked how titles & headings are rendered
  • Loading branch information
OXY2DEV committed Aug 8, 2024
1 parent fa1efc6 commit 9de7d93
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 125 deletions.
142 changes: 114 additions & 28 deletions ftplugin/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,71 +36,157 @@ vim.api.nvim_create_autocmd({ "BufWinEnter" }, {
table.insert(helpview.attached_buffers, buffer);
end

-- On disable
if helpview.state.enable == false or helpview.state.buf_states[buffer] == false then
for _, window in ipairs(windows) do
if helpview.configuration.options and helpview.configuration.options.on_disable and pcall(helpview.configuration.options.on_disable, window, buffer) then
helpview.configuration.options.on_disable(window, buffer);
if helpview.configuration.callbacks and helpview.configuration.options.on_disable and pcall(helpview.configuration.callbacks.on_disable, window, buffer) then
helpview.configuration.callbacks.on_disable(window, buffer);
end
end

return;
-- On enable
else
for _, window in ipairs(windows) do
if helpview.configuration.options and helpview.configuration.options.on_enable and pcall(helpview.configuration.options.on_enable, window, buffer) then
helpview.configuration.options.on_enable(window, buffer);
if helpview.configuration.callbacks and helpview.configuration.callbacks.on_enable and pcall(helpview.configuration.callbacks.on_enable, window, buffer) then
helpview.configuration.callbacks.on_enable(window, buffer);
end
end

helpview.state.buf_states[buffer] = true;
end

local parsed_content = helpview.parser.init(buffer);
local cursor = vim.api.nvim_win_get_cursor(0);
local lines = vim.api.nvim_buf_line_count(event.buf);

-- helpview.column.cache(buffer, parsed_content);
helpview.renderer.clear(buffer)
helpview.renderer.render(buffer, parsed_content, helpview.configuration, helpview.get_buffer_info(buffer));
-- Don't render stuff others can't see
if lines > 1000 then
local before = math.max(0, cursor[1] - (helpview.configuration.parse_range or 100));
local after = math.min(vim.api.nvim_buf_line_count(event.buf), cursor[1] + (helpview.configuration.parse_range or 100));

local parse = helpview.parser.init(event.buf, before, after);

helpview.renderer.clear(event.buf);
helpview.renderer.render(event.buf, parse, helpview.configuration, helpview.get_buffer_info(event.buf));
else
local parse = helpview.parser.init(event.buf);

helpview.renderer.clear(event.buf);
helpview.renderer.render(event.buf, parse, helpview.configuration, helpview.get_buffer_info(event.buf));
end
end
});

vim.api.nvim_create_autocmd({ "ModeChanged", "TextChanged", "WinResized" }, {
vim.api.nvim_create_autocmd({ "ModeChanged" }, {
group = help_augroup,
buffer = vim.api.nvim_get_current_buf(),

callback = function (event)
local mode = vim.api.nvim_get_mode().mode;
local buffer = event.buf;

if helpview.state.enable == false or helpview.state.buf_states[buffer] == false then
return;
elseif helpview.state.buf_states[buffer] ~= true then
helpview.state.buf_states[buffer] = true;
end

local mode = vim.api.nvim_get_mode().mode;
if helpview.configuration.callbacks and helpview.configuration.callbacks.on_mode_change then
for _, win in ipairs(helpview.get_attached_wins(buffer)) do
pcall(helpview.configuration.callbacks.on_mode_change, buffer, win, mode);
end
end

local modifiable = vim.bo[buffer].modifiable;
if not vim.list_contains(helpview.configuration.modes, mode) then
helpview.renderer.clear(buffer);
return;
end

local cursor = vim.api.nvim_win_get_cursor(0);
local lines = vim.api.nvim_buf_line_count(buffer);

local window_lines = vim.o.lines;
if lines > 1000 then
local before = math.max(0, cursor[1] - (helpview.configuration.parse_range or 100));
local after = math.min(lines, cursor[1] + (helpview.configuration.parse_range or 100));

local parse = helpview.parser.init(buffer, before, after);

helpview.renderer.clear(buffer);
helpview.renderer.render(buffer, parse, helpview.configuration, helpview.get_buffer_info(event.buf));
else
local parse = helpview.parser.init(buffer);

helpview.renderer.clear(buffer);
helpview.renderer.render(buffer, parse, helpview.configuration, helpview.get_buffer_info(event.buf));
end

if not helpview.configuration.hybrid_modes or not vim.list_contains(helpview.configuration.hybrid_modes, mode) then
return;
end
end
});

local events = {};

if vim.list_contains(helpview.configuration.modes, "n") or
vim.list_contains(helpview.configuration.modes, "v")
then
table.insert(events, "CursorMoved");
end

if vim.list_contains(helpview.configuration.modes, "i") then
table.insert(events, "CursorMovedI");
end

local timer = vim.uv.new_timer();

vim.api.nvim_create_autocmd(events, {
group = help_augroup,
buffer = vim.api.nvim_get_current_buf(),

callback = function (event)
timer:stop();
local mode = vim.api.nvim_get_mode().mode;
local buffer = event.buf;

if event.event ~= "WinResized" and modifiable == false or lines > (helpview.configuration.max_lines or 1000) then
if helpview.state.enable == false or helpview.state.buf_states[buffer] == false then
return;
end

if not vim.list_contains(helpview.configuration.modes, mode) then
return;
end

timer:start(100, 0, vim.schedule_wrap(function ()
local cursor = vim.api.nvim_win_get_cursor(0);
local lines = vim.api.nvim_buf_line_count(buffer);

if lines > 1000 then
local before = math.max(0, cursor[1] - (helpview.configuration.parse_range or 100));
local after = math.min(vim.api.nvim_buf_line_count(buffer), cursor[1] + (helpview.configuration.parse_range or 100));

local parse = helpview.parser.init(buffer, before, after);

if vim.islist(helpview.configuration.modes) and vim.list_contains(helpview.configuration.modes, mode) then
helpview.renderer.partial_clear(buffer, cursor[1] - (helpview.configuration.render_lines or window_lines), cursor[1] + (helpview.configuration.render_lines or window_lines));
helpview.renderer.partial_render(buffer, cursor[1] - (helpview.configuration.render_lines or window_lines), cursor[1] + (helpview.configuration.render_lines or window_lines), helpview.configuration, helpview.get_buffer_info(buffer));
helpview.renderer.clear(buffer);
helpview.renderer.render(buffer, parse, helpview.configuration, helpview.get_buffer_info(buffer));
else
helpview.renderer.partial_clear(buffer, cursor[1] - (helpview.configuration.render_lines or window_lines), cursor[1] + (helpview.configuration.render_lines or window_lines));
local parse = helpview.parser.init(buffer);

helpview.renderer.clear(buffer);
helpview.renderer.render(buffer, parse, helpview.configuration, helpview.get_buffer_info(buffer));
end
else
if vim.islist(helpview.configuration.modes) and vim.list_contains(helpview.configuration.modes, mode) then
local parsed_content = helpview.parser.init(buffer);

-- helpview.column.cache(buffer, parsed_content);
helpview.renderer.clear(buffer)
helpview.renderer.render(buffer, parsed_content, helpview.configuration, helpview.get_buffer_info(buffer));
else
helpview.renderer.clear(buffer)
if not helpview.configuration.hybrid_modes or not vim.list_contains(helpview.configuration.hybrid_modes, mode) then
return;
end
end

local under_cursor = helpview.parser.init(buffer, math.max(cursor[1] - 1, 0), cursor[1]);
local cl_start, cl_stop = helpview.renderer.get_content_range(under_cursor);

if not cl_start or not cl_stop then
return;
end

helpview.renderer.clear(buffer, cl_start, cl_stop);
end));
end
});

23 changes: 15 additions & 8 deletions lua/helpview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,20 @@ helpview.state = {

helpview.configuration = {
modes = { "n", "c" },
hybrid_modes = nil,
-- buf_ignore = { "help" },
callbacks = {
on_enable = nil,
on_disable = nil,

on_mode_change = nil
},

highlight_groups = {
{
output = function ()
local bg = helpview.colors.get_hl_value(0, "Normal", "bg");
local inline_fg = helpview.colors.get_hl_value(0, "TablineSel", "fg");
local inline_fg = helpview.colors.get_hl_value(0, "@markup.raw.vimdoc", "fg");
local tag_fg = helpview.colors.get_hl_value(0, "Title", "fg");
local option_fg = helpview.colors.get_hl_value(0, "Tag", "fg");
local taglink_fg = helpview.colors.get_hl_value(0, "Title", "fg");
Expand Down Expand Up @@ -194,7 +201,7 @@ helpview.configuration = {
{
output = function ()
local from = helpview.colors.get_hl_value(0, "Normal", "bg") or "#1e1e2e";
local to = helpview.colors.get_hl_value(0, "rainbow4", "fg");
local to = helpview.colors.get_hl_value(0, "@character", "fg") or helpview.colors.get_hl_value(0, "@comment.note", "fg");

return helpview.colors.create_gradient("Gradient", from, to, 10, "fg")
end
Expand All @@ -211,11 +218,11 @@ helpview.configuration = {
"", "", ""
},

hls = {
hl = {
"h1", "h1", "h1",
"h1", "h1", "h1",
"h1", "h1", "h1",
}
},
},
heading_2 = {
style = "decorated",
Expand All @@ -226,7 +233,7 @@ helpview.configuration = {
"", "", ""
},

hls = {
hl = {
"h2", "h2", "h2",
"h2", "h2", "h2",
"h2", "h2", "h2",
Expand All @@ -239,9 +246,9 @@ helpview.configuration = {
"", "", ""
},

hls = {
hl = {
"h3", "h3", "h3",
}
},
},
},
title = {
Expand All @@ -250,7 +257,7 @@ helpview.configuration = {
"", " ", "",
"", "", ""
},
hls = {
hl = {
"Special", "Special", "Special",
"Special", "Special", "Special",
"Special", "Special", "Special",
Expand Down
2 changes: 1 addition & 1 deletion lua/helpview/extras/column.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ column.draw = function (window, buffer)

local _o = "";

if vim.v.relnum == 0 and vim.v.virtnum == 0 then
if vim.v.relnum == 0 and vim.v.virtnum == 0 and vim.o.cursorline == true then
_o = "%#" .. "CursorLine" .. "#";
end

Expand Down
12 changes: 6 additions & 6 deletions lua/helpview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ parser.get_str_type = function (str)
end
end

parser.vimdoc = function (buffer, TStree)
parser.vimdoc = function (buffer, TStree, from, to)
local scanned_queies = vim.treesitter.query.parse("vimdoc", [[
([(h1)
(h2)
Expand Down Expand Up @@ -53,7 +53,7 @@ parser.vimdoc = function (buffer, TStree)
-- vim.print(capture_id)
-- end

for capture_id, capture_node, _, _ in scanned_queies:iter_captures(TStree:root()) do
for capture_id, capture_node, _, _ in scanned_queies:iter_captures(TStree:root(), buffer, from, to) do
local capture_name = scanned_queies.captures[capture_id];
local capture_text = vim.treesitter.get_node_text(capture_node, buffer);
local row_start, col_start, row_end, col_end = capture_node:range();
Expand Down Expand Up @@ -134,7 +134,7 @@ parser.vimdoc = function (buffer, TStree)
row_start = row_start,
col_start = col_start,

row_end = row_end,
row_end = row_end - 1,
col_end = col_end
})
end
Expand All @@ -154,7 +154,7 @@ parser.vimdoc = function (buffer, TStree)
row_start = row_start,
col_start = col_start,

row_end = row_end,
row_end = row_end - 1,
col_end = col_end
})
elseif capture_name == "may_be_hl" then
Expand Down Expand Up @@ -344,7 +344,7 @@ parser.vimdoc = function (buffer, TStree)
end
end

parser.init = function (buffer)
parser.init = function (buffer, from, to)
local root_parser = vim.treesitter.get_parser(buffer);
root_parser:parse();

Expand All @@ -354,7 +354,7 @@ parser.init = function (buffer)
local tree_language = language_tree:lang();

if tree_language == "vimdoc" then
parser.vimdoc(buffer, TStree);
parser.vimdoc(buffer, TStree, from, to);
end
end);

Expand Down
Loading

0 comments on commit 9de7d93

Please sign in to comment.