aboutsummaryrefslogtreecommitdiff
path: root/plugins/tab_switcher.lua
diff options
context:
space:
mode:
authorGuldoman <giulio.lettieri@gmail.com>2022-12-28 20:36:57 +0100
committerGitHub <noreply@github.com>2022-12-28 15:36:57 -0400
commite77616d5e0fa34b7b558cb93d096e3c9dfb5fad8 (patch)
tree677a1d1b55ab64b84f99136aecb06f7ac040283f /plugins/tab_switcher.lua
parenta5e119a9dc9a0160211c4c54fdbe83ae5dabcc43 (diff)
downloadlite-xl-plugins-e77616d5e0fa34b7b558cb93d096e3c9dfb5fad8.tar.gz
lite-xl-plugins-e77616d5e0fa34b7b558cb93d096e3c9dfb5fad8.zip
Add `tab_switcher` (#173)
Diffstat (limited to 'plugins/tab_switcher.lua')
-rw-r--r--plugins/tab_switcher.lua60
1 files changed, 60 insertions, 0 deletions
diff --git a/plugins/tab_switcher.lua b/plugins/tab_switcher.lua
new file mode 100644
index 0000000..3d55eed
--- /dev/null
+++ b/plugins/tab_switcher.lua
@@ -0,0 +1,60 @@
+-- mod-version:3
+local core = require "core"
+local command = require "core.command"
+local keymap = require "core.keymap"
+local common = require "core.common"
+local DocView = require "core.docview"
+
+local tab_switcher = {}
+function tab_switcher.get_tab_list(base_node)
+ local raw_list = base_node:get_children()
+ local list = {}
+ local mt = {
+ -- fuzzy_match uses tostring to get the text to compare
+ __tostring = function(i) return i.text end
+ }
+ for _,v in pairs(raw_list) do
+ if v:is(DocView) then
+ table.insert(list, setmetatable({
+ text = v:get_name(),
+ view = v
+ }, mt))
+ end
+ end
+ return list
+end
+
+local function ask_selection(label, items)
+ if #items == 0 then
+ core.warn("No tabs available")
+ return
+ end
+ core.command_view:enter(label, {
+ submit = function(_, item)
+ local n = core.root_view.root_node:get_node_for_view(item.view)
+ if n then n:set_active_view(item.view) end
+ end,
+ suggest = function(text)
+ return common.fuzzy_match(items, text, true)
+ end,
+ validate = function(_, item)
+ return item
+ end
+ })
+end
+
+command.add(nil,{
+ ["tab-switcher:tab-list"] = function()
+ ask_selection("Switch to tab", tab_switcher.get_tab_list(core.root_view.root_node))
+ end,
+ ["tab-switcher:tab-list-current-split"] = function()
+ ask_selection("Switch to tab in current split", tab_switcher.get_tab_list(core.root_view:get_active_node()))
+ end
+})
+
+keymap.add({
+ ["alt+p"] = "tab-switcher:tab-list",
+ ["alt+shift+p"] = "tab-switcher:tab-list-current-split"
+})
+
+return tab_switcher