From 017f36d2d2967ab3e5e07750354f566e38b6b0ce Mon Sep 17 00:00:00 2001 From: musjj <72612857+musjj@users.noreply.github.com> Date: Thu, 18 Aug 2022 19:45:00 +0700 Subject: [PATCH 1/3] Fix references to incorrect tables --- lua/nvterm/terminal.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvterm/terminal.lua b/lua/nvterm/terminal.lua index b48a5e6..f6809a4 100644 --- a/lua/nvterm/terminal.lua +++ b/lua/nvterm/terminal.lua @@ -7,7 +7,7 @@ local function get_last(list) if list then return not vim.tbl_isempty(list) and list[#list] or nil end - return terminals[#terminals] or nil + return terminals.list[#terminals.list] or nil end local function get_type(type, list) @@ -108,7 +108,7 @@ end nvterm.show = function(type) terminals = util.verify_terminals(terminals) - local term = type and get_type_last(type) or terminals.last + local term = type and get_type_last(type) or get_last() nvterm.show_term(term) end From 096ea0eace6a730f4dd50f8bc65b2e480dba34d4 Mon Sep 17 00:00:00 2001 From: musjj <72612857+musjj@users.noreply.github.com> Date: Thu, 18 Aug 2022 19:55:40 +0700 Subject: [PATCH 2/3] A more robust terminal list handling The state of the terminal list are often outdated by the time nvterm does things with it, so we should always verify it first. --- lua/nvterm/terminal.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lua/nvterm/terminal.lua b/lua/nvterm/terminal.lua index f6809a4..3052a12 100644 --- a/lua/nvterm/terminal.lua +++ b/lua/nvterm/terminal.lua @@ -7,10 +7,12 @@ local function get_last(list) if list then return not vim.tbl_isempty(list) and list[#list] or nil end + terminals = util.verify_terminals(terminals) return terminals.list[#terminals.list] or nil end local function get_type(type, list) + terminals = util.verify_terminals(terminals) list = list or terminals.list return vim.tbl_filter(function(t) return t.type == type @@ -18,6 +20,7 @@ local function get_type(type, list) end local function get_still_open() + terminals = util.verify_terminals(terminals) if not terminals.list then return {} end @@ -35,6 +38,7 @@ local function get_type_last(type) end local function get_term(key, value) + terminals = util.verify_terminals(terminals) -- assumed to be unique, will only return 1 term regardless return vim.tbl_filter(function(t) return t[key] == value @@ -42,6 +46,7 @@ local function get_term(key, value) end local create_term_window = function(type) + terminals = util.verify_terminals(terminals) local existing = terminals.list and #get_type(type, get_still_open()) > 0 util.execute_type_cmd(type, terminals, existing) vim.wo.relativenumber = false @@ -50,7 +55,6 @@ local create_term_window = function(type) end local ensure_and_send = function(cmd, type) - terminals = util.verify_terminals(terminals) local function select_term() if not type then return get_last_still_open() or nvterm.new "horizontal" @@ -107,7 +111,6 @@ nvterm.hide = function(type) end nvterm.show = function(type) - terminals = util.verify_terminals(terminals) local term = type and get_type_last(type) or get_last() nvterm.show_term(term) end @@ -120,6 +123,7 @@ nvterm.new = function(type) a.nvim_win_set_buf(win, buf) local job_id = vim.fn.termopen(terminals.shell or vim.o.shell) + terminals = util.verify_terminals(terminals) local id = #terminals.list + 1 local term = { id = id, win = win, buf = buf, open = true, type = type, job_id = job_id } terminals.list[id] = term @@ -128,7 +132,6 @@ nvterm.new = function(type) end nvterm.toggle = function(type) - terminals = util.verify_terminals(terminals) local term = get_type_last(type) if not term then @@ -170,6 +173,7 @@ nvterm.list_active_terms = function(property) end nvterm.list_terms = function() + terminals = util.verify_terminals(terminals) return terminals.list end From 53e1ec4bc3da6f0df4c2902b18620235094d589a Mon Sep 17 00:00:00 2001 From: musjj <72612857+musjj@users.noreply.github.com> Date: Sat, 20 Aug 2022 12:10:39 +0700 Subject: [PATCH 3/3] Slightly reduce unnecessary calls to `verify_terminals` --- lua/nvterm/terminal.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/nvterm/terminal.lua b/lua/nvterm/terminal.lua index 3052a12..e0119ad 100644 --- a/lua/nvterm/terminal.lua +++ b/lua/nvterm/terminal.lua @@ -12,8 +12,10 @@ local function get_last(list) end local function get_type(type, list) - terminals = util.verify_terminals(terminals) - list = list or terminals.list + if not list then + terminals = util.verify_terminals(terminals) + list = terminals.list + end return vim.tbl_filter(function(t) return t.type == type end, list) @@ -46,8 +48,7 @@ local function get_term(key, value) end local create_term_window = function(type) - terminals = util.verify_terminals(terminals) - local existing = terminals.list and #get_type(type, get_still_open()) > 0 + local existing = #get_type(type, get_still_open()) > 0 util.execute_type_cmd(type, terminals, existing) vim.wo.relativenumber = false vim.wo.number = false @@ -155,7 +156,6 @@ nvterm.toggle_all_terms = function() end end - nvterm.close_all_terms = function() for _, buf in ipairs(nvterm.list_active_terms "buf") do vim.cmd("bd! " .. tostring(buf))