From db213743f769df28e370cbd49a73e147756612c8 Mon Sep 17 00:00:00 2001 From: jgmdev Date: Sun, 20 Jun 2021 13:56:38 -0400 Subject: [navigate] Make it work on master and fixed elements removal issue. --- plugins/navigate.lua | 60 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 16 deletions(-) (limited to 'plugins') diff --git a/plugins/navigate.lua b/plugins/navigate.lua index 2bf4396..dd985b5 100644 --- a/plugins/navigate.lua +++ b/plugins/navigate.lua @@ -3,8 +3,8 @@ local core = require "core" local common = require "core.common" local command = require "core.command" -local config = require "core.config" local keymap = require "core.keymap" +local Doc = require "core.doc" local DocView = require "core.docview" local navigate = { @@ -23,15 +23,37 @@ local function get_active_view() return nil end +-- Solution to safely remove elements from array table: +-- found at https://stackoverflow.com/a/53038524 +local function array_remove(t, fnKeep) + local j, n = 1, #t; + + for i=1, n do + if (fnKeep(t, i, j)) then + if (i ~= j) then + t[j] = t[i]; + t[i] = nil; + end + j = j + 1; + else + t[i] = nil; + end + end + + return t; +end + local function add(doc) -- Make new navigation point last in list if navigate.index > 0 and navigate.index < #navigate.list then - local list_len = #navigate.list - for index=navigate.index+1, list_len, 1 do - if navigate.list[index] then - table.remove(navigate.list, index) + local remove_start = navigate.index + 1 + local remove_end = #navigate.list + array_remove(navigate.list, function(_, i) + if i >= remove_start and i <= remove_end then + return false end - end + return true + end) end local line, col = doc:get_selection() @@ -106,17 +128,23 @@ core.add_thread(function() end end) -core.add_close_hook(function(doc) - local filename = doc.filename - local list = {table.unpack(navigate.list)} - for index, position in ipairs(list) do - if position.filename == filename then - if navigate.list[index] then - table.remove(navigate.list, index) - end +-- +-- Patching +-- +local doc_on_close = Doc.on_close + +function Doc:on_close() + local filename = self.filename + -- remove all positions referencing closed file + array_remove(navigate.list, function(t, i) + if t[i].filename == filename then + return false end - end -end) + return true + end) + + doc_on_close(self) +end -- -- Commands -- cgit v1.2.3