-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from nanozuki/tab-jumper
feat: magic jump
- Loading branch information
Showing
6 changed files
with
229 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
local tab_jumper = { | ||
line = nil, | ||
char_to_tabid = {}, ---@type table<string,number> | ||
tabid_to_char = {}, ---@type table<number,string> | ||
is_start = false, | ||
} | ||
|
||
local alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
|
||
---@param line TabbyLine | ||
function tab_jumper.pre_render(line) | ||
tab_jumper.line = line | ||
end | ||
|
||
function tab_jumper.reset() | ||
tab_jumper.char_to_tabid = {} | ||
tab_jumper.tabid_to_char = {} | ||
end | ||
|
||
function tab_jumper.build_indexes() | ||
if tab_jumper.line == nil then | ||
return | ||
end | ||
local tabs = tab_jumper.line.tabs().tabs | ||
|
||
-- use the first, not used char of tab's name | ||
for _, tab in ipairs(tabs) do | ||
local name = tab.name() | ||
for i = 1, #name do | ||
local char = name:sub(i, i):upper() | ||
if tab_jumper.char_to_tabid[char] == nil then | ||
tab_jumper.char_to_tabid[char] = tab.id | ||
tab_jumper.tabid_to_char[tostring(tab.id)] = char | ||
break | ||
end | ||
end | ||
end | ||
|
||
-- then, for the remain tabs, use the first, not used char of alphabet | ||
for _, tab in ipairs(tabs) do | ||
if tab_jumper.tabid_to_char[tostring(tab.id)] == nil then | ||
for i = 1, #alphabet do | ||
local char = alphabet:sub(i, i) | ||
if tab_jumper.char_to_tabid[char] == nil then | ||
tab_jumper.char_to_tabid[char] = tab.id | ||
tab_jumper.tabid_to_char[tostring(tab.id)] = char | ||
break | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
function tab_jumper.get_char(tabid) | ||
local char = tab_jumper.tabid_to_char[tostring(tabid)] or '??' | ||
return char | ||
end | ||
|
||
function tab_jumper.start() | ||
tab_jumper.build_indexes() | ||
tab_jumper.is_start = true | ||
|
||
vim.cmd.redrawtabline() | ||
local ok, tabid = pcall(function() | ||
local c = string.char(vim.fn.getchar()):upper() | ||
return tab_jumper.char_to_tabid[c] | ||
end) | ||
|
||
tab_jumper.is_start = false | ||
tab_jumper.reset() | ||
if ok and tabid then | ||
vim.api.nvim_set_current_tabpage(tabid) | ||
else | ||
vim.cmd.redrawtabline() | ||
end | ||
end | ||
|
||
return tab_jumper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.