Skip to content

Commit

Permalink
fix(gx): Fixed a bug causing gx to not work with links.
Browse files Browse the repository at this point in the history
Also fixed incorrect node number in parser for links.

Closes #97
  • Loading branch information
OXY2DEV committed Aug 10, 2024
1 parent edcb29d commit 6d0bcce
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
14 changes: 4 additions & 10 deletions ftplugin/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ vim.api.nvim_create_autocmd({ "BufWinEnter" }, {
-- This needs all of the buffer to be parsed
local keymap_content = markview.parser.init(buffer, markview.configuration);


markview.keymaps.init(buffer, keymap_content, markview.configuration);
for _, window in ipairs(windows) do
if markview.configuration.callbacks and markview.configuration.callbacks.on_enable then
pcall(markview.configuration.callbacks.on_enable, buffer, window);
end

markview.keymaps.init(buffer, window, keymap_content, markview.configuration);
end
end
});
Expand Down Expand Up @@ -160,9 +160,7 @@ vim.api.nvim_create_autocmd({ "ModeChanged", "TextChanged" }, {
markview.renderer.render(buffer, parsed_content, markview.configuration)
end

for _, window in ipairs(windows) do
markview.keymaps.init(buffer, window, parsed_content, markview.configuration);
end
markview.keymaps.init(buffer, parsed_content, markview.configuration);

if not markview.configuration.hybrid_modes or not vim.list_contains(markview.configuration.hybrid_modes, mode) then
return;
Expand Down Expand Up @@ -245,11 +243,7 @@ vim.api.nvim_create_autocmd(events, {
end

if parsed_content and #parsed_content > 0 then
local windows = utils.find_attached_wins(event.buf);

for _, window in ipairs(windows) do
markview.keymaps.init(buffer, window, parsed_content, markview.configuration);
end
markview.keymaps.init(buffer, parsed_content, markview.configuration);
end

local cursor = vim.api.nvim_win_get_cursor(0);
Expand Down
13 changes: 7 additions & 6 deletions lua/markview/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ local keymaps = {};
keymaps.views = {};
keymaps.on_bufs = {};

keymaps.createKeymap = function (buffer, window)
local buf_links = keymaps.views[buffer];

keymaps.createKeymap = function (buffer)
vim.api.nvim_buf_set_keymap(buffer, "n", "gx", "", {
desc = "gx patch for Markview.nvim",
callback = function ()
local cursor = vim.api.nvim_win_get_cursor(window);
local buf_links = keymaps.views[buffer];
local cursor = vim.api.nvim_win_get_cursor(0);

for _, link in ipairs(buf_links) do
if link.row_start + 1 ~= cursor[1] then
Expand All @@ -36,19 +35,21 @@ keymaps.createKeymap = function (buffer, window)
})
end

keymaps.init = function (buffer, window, parsed_content, config_table)
keymaps.init = function (buffer, parsed_content, config_table)
if parsed_content ~= nil then
keymaps.views[buffer] = {};
end

for _, content in ipairs(parsed_content) do
if content.type == "link" then
table.insert(keymaps.views[buffer], content);
elseif content.type == "image" then
table.insert(keymaps.views[buffer], content);
end
end

if not vim.list_contains(keymaps.on_bufs, buffer) then
keymaps.createKeymap(buffer, window);
keymaps.createKeymap(buffer);

table.insert(keymaps.on_bufs, buffer)
end
Expand Down
2 changes: 1 addition & 1 deletion lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ parser.md_inline = function (buffer, TStree, from, to)
link_text = vim.treesitter.get_node_text(capture_node:named_child(0), buffer);
end

if capture_node:named_child(1) and (capture_node:named_child(q):type() == "link_destination" or capture_node:named_child(q):type() == "link_label") then
if capture_node:named_child(1) and (capture_node:named_child(1):type() == "link_destination" or capture_node:named_child(1):type() == "link_label") then
link_address = vim.treesitter.get_node_text(capture_node:named_child(1), buffer);
end

Expand Down

0 comments on commit 6d0bcce

Please sign in to comment.