-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ec2579
commit b05f332
Showing
4 changed files
with
139 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
local u = require("tabscope.utils") | ||
|
||
-- Returns a table that stores and tracks tab local buffers. | ||
local function new() | ||
local m = {} | ||
m._buffers_by_tab = {} | ||
|
||
m._is_buffer_trackable = function(id) | ||
if id == nil or id < 1 then | ||
return false | ||
end | ||
|
||
if not vim.api.nvim_buf_is_valid(id) then | ||
return false | ||
end | ||
|
||
for _, buffers in pairs(m._buffers_by_tab) do | ||
for buffer, _ in pairs(buffers) do | ||
if buffer == id then | ||
return true | ||
end | ||
end | ||
end | ||
|
||
local listed = vim.bo[id].buflisted | ||
return listed | ||
end | ||
|
||
m._try_to_track_current_buffer = function() | ||
local buffer = vim.api.nvim_get_current_buf() | ||
if not m._is_buffer_trackable(buffer) then | ||
return | ||
end | ||
|
||
local current_tab_buffers = m.tab_get_buffers(0) | ||
current_tab_buffers[buffer] = true | ||
end | ||
|
||
m._try_to_untrack_abuf = function() | ||
local buffer = tonumber(vim.fn.expand("<abuf>")) | ||
if type(buffer) ~= "number" then | ||
return | ||
end | ||
|
||
for _, tab_buffers in ipairs(m._buffers_by_tab) do | ||
tab_buffers[buffer] = nil | ||
end | ||
end | ||
|
||
m.tab_get_buffers = function(tab) | ||
if not tab or tab < 1 then | ||
tab = vim.api.nvim_get_current_tabpage() | ||
end | ||
|
||
if m._buffers_by_tab[tab] == nil then | ||
m._buffers_by_tab[tab] = {} | ||
end | ||
|
||
return m._buffers_by_tab[tab] | ||
end | ||
|
||
m.ignore_events = false | ||
|
||
u.set_autocmd("BufEnter", function() | ||
if not m.ignore_events then | ||
m._try_to_track_current_buffer() | ||
end | ||
end) | ||
|
||
u.set_autocmd("BufDelete", function() | ||
if not m.ignore_events then | ||
m._try_to_untrack_abuf() | ||
end | ||
end) | ||
|
||
return m | ||
end | ||
|
||
return { | ||
new = new, | ||
} |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
local M = {} | ||
|
||
M.setup = function(_) | ||
print("hello new plugin") | ||
local buffer_manager = require("tabscope.buffer-manager").new() | ||
M.tab_manager = require("tabscope.tab-manager").new(buffer_manager) | ||
end | ||
|
||
return M |
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,41 @@ | ||
local u = require("tabscope.utils") | ||
|
||
-- Returns a table that manages tab switches. | ||
local function new(buffer_manager) | ||
local m = {} | ||
m.buffer_manager = buffer_manager | ||
|
||
m.try_to_switch_tab = function() | ||
local previous_tab = m.previous_tab | ||
m.previous_tab = nil | ||
if previous_tab == nil then | ||
return | ||
end | ||
|
||
local previous_buffers = m.buffer_manager.tab_get_buffers(previous_tab) | ||
local current_buffers = m.buffer_manager.tab_get_buffers(0) | ||
|
||
m.buffer_manager.ignore_events = true | ||
|
||
for buffer, _ in pairs(previous_buffers) do | ||
vim.bo[buffer].buflisted = false | ||
end | ||
|
||
for buffer, _ in pairs(current_buffers) do | ||
vim.bo[buffer].buflisted = true | ||
end | ||
|
||
m.buffer_manager.ignore_events = false | ||
end | ||
|
||
u.set_autocmd("BufEnter", m.try_to_switch_tab) | ||
u.set_autocmd("TabLeave", function() | ||
m.previous_tab = vim.api.nvim_get_current_tabpage() | ||
end) | ||
|
||
return m | ||
end | ||
|
||
return { | ||
new = new, | ||
} |
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,15 @@ | ||
local M = {} | ||
|
||
-- Creates auto command | ||
M.set_autocmd = function(event, callback) | ||
if not M._group then | ||
M._group = vim.api.nvim_create_augroup("TabScopeNvim", {}) | ||
end | ||
|
||
vim.api.nvim_create_autocmd(event, { | ||
group = M._group, | ||
callback = callback, | ||
}) | ||
end | ||
|
||
return M |