Merge pull request #378 from adamharrison/close-other-tabs

Added in close others, and refactored close all.
This commit is contained in:
Adam 2021-08-12 09:53:04 -04:00 committed by GitHub
commit f3e750ccb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 11 deletions

View File

@ -155,7 +155,7 @@ command.add(nil, {
core.error("Cannot open folder %q", text) core.error("Cannot open folder %q", text)
return return
end end
core.confirm_close_all(core.open_folder_project, text) core.confirm_close_docs(core.docs, core.open_folder_project, text)
end, suggest_directory) end, suggest_directory)
end, end,

View File

@ -21,9 +21,15 @@ local t = {
end, end,
["root:close-all"] = function() ["root:close-all"] = function()
core.confirm_close_all(core.root_view.close_all_docviews, core.root_view) core.confirm_close_docs(core.docs, core.root_view.close_all_docviews, core.root_view)
end, end,
["root:close-all-others"] = function()
local active_doc, docs = core.active_view and core.active_view.doc, {}
for i, v in ipairs(core.docs) do if v ~= active_doc then table.insert(docs, v) end end
core.confirm_close_docs(docs, core.root_view.close_all_docviews, core.root_view, true)
end,
["root:switch-to-previous-tab"] = function() ["root:switch-to-previous-tab"] = function()
local node = core.root_view:get_active_node() local node = core.root_view:get_active_node()
local idx = node:get_view_idx(core.active_view) local idx = node:get_view_idx(core.active_view)

View File

@ -564,10 +564,10 @@ function core.init()
end end
function core.confirm_close_all(close_fn, ...) function core.confirm_close_docs(docs, close_fn, ...)
local dirty_count = 0 local dirty_count = 0
local dirty_name local dirty_name
for _, doc in ipairs(core.docs) do for _, doc in ipairs(docs or core.docs) do
if doc:is_dirty() then if doc:is_dirty() then
dirty_count = dirty_count + 1 dirty_count = dirty_count + 1
dirty_name = doc:get_name() dirty_name = doc:get_name()
@ -627,7 +627,7 @@ local function quit_with_function(quit_fn, force)
save_session() save_session()
quit_fn() quit_fn()
else else
core.confirm_close_all(quit_with_function, quit_fn, true) core.confirm_close_docs(core.docs, quit_with_function, quit_fn, true)
end end
end end

View File

@ -594,12 +594,13 @@ function Node:is_empty()
end end
function Node:close_all_docviews() function Node:close_all_docviews(keep_active)
if self.type == "leaf" then if self.type == "leaf" then
local i = 1 local i = 1
while i <= #self.views do while i <= #self.views do
local view = self.views[i] local view = self.views[i]
if view:is(DocView) and not view:is(CommandView) then if view:is(DocView) and not view:is(CommandView) and
(not keep_active or view ~= self.active_view) then
table.remove(self.views, i) table.remove(self.views, i)
else else
i = i + 1 i = i + 1
@ -609,8 +610,8 @@ function Node:close_all_docviews()
self:add_view(EmptyView()) self:add_view(EmptyView())
end end
else else
self.a:close_all_docviews() self.a:close_all_docviews(keep_active)
self.b:close_all_docviews() self.b:close_all_docviews(keep_active)
if self.a:is_empty() and not self.a.is_primary_node then if self.a:is_empty() and not self.a.is_primary_node then
self:consume(self.b) self:consume(self.b)
elseif self.b:is_empty() and not self.b.is_primary_node then elseif self.b:is_empty() and not self.b.is_primary_node then
@ -736,8 +737,8 @@ function RootView:open_doc(doc)
end end
function RootView:close_all_docviews() function RootView:close_all_docviews(keep_active)
self.root_node:close_all_docviews() self.root_node:close_all_docviews(keep_active)
end end