aboutsummaryrefslogtreecommitdiff
path: root/plugins/tab_switcher.lua
blob: 3d55eede0fb9b99856dd298b8efa9e3f804fda7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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