From 6d0bcce3a74b10bcc79c04c5944bfc000381be49 Mon Sep 17 00:00:00 2001 From: Shawon Date: Sat, 10 Aug 2024 07:44:51 +0600 Subject: [PATCH] fix(gx): Fixed a bug causing gx to not work with links. Also fixed incorrect node number in parser for links. Closes #97 --- ftplugin/markdown.lua | 14 ++++---------- lua/markview/keymaps.lua | 13 +++++++------ lua/markview/parser.lua | 2 +- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/ftplugin/markdown.lua b/ftplugin/markdown.lua index 830c5db..cc90e50 100644 --- a/ftplugin/markdown.lua +++ b/ftplugin/markdown.lua @@ -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 }); @@ -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; @@ -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); diff --git a/lua/markview/keymaps.lua b/lua/markview/keymaps.lua index 3eaead4..119f014 100644 --- a/lua/markview/keymaps.lua +++ b/lua/markview/keymaps.lua @@ -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 @@ -36,7 +35,7 @@ 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 @@ -44,11 +43,13 @@ keymaps.init = function (buffer, window, parsed_content, config_table) 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 diff --git a/lua/markview/parser.lua b/lua/markview/parser.lua index dba5fe3..819452e 100644 --- a/lua/markview/parser.lua +++ b/lua/markview/parser.lua @@ -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