95 lines
2.2 KiB
Lua
95 lines
2.2 KiB
Lua
local M = {}
|
|
|
|
local Extension_Map = require"SpecSwitcher.ExtensionMap"
|
|
local Path = require"SpecSwitcher.path"
|
|
|
|
local _config = {
|
|
descend_dir = {},
|
|
open_in_new_tab = true
|
|
}
|
|
|
|
local mapping = Extension_Map:new()
|
|
|
|
local function open(filename)
|
|
vim.cmd("e " .. filename)
|
|
end
|
|
|
|
local function open_in_tab(filename)
|
|
vim.cmd("tabe " .. filename)
|
|
end
|
|
|
|
function M.Switch(open_in_new_tab)
|
|
local current_dir = vim.api.nvim_buf_get_name(0)
|
|
|
|
local file = nil
|
|
local dir = nil
|
|
local extension = nil
|
|
|
|
local extension_list = mapping:list_exts()
|
|
|
|
extension = Path.Get_Ext(current_dir, extension_list)
|
|
|
|
if extension == nil then
|
|
print("No mapping found")
|
|
else
|
|
file = Path.Get_File(current_dir)
|
|
dir = Path.Walk_Up(current_dir)
|
|
|
|
print(mapping:print())
|
|
local mapped_exts = mapping[extension]
|
|
local walker_func = Path.Generate_Dir_Walk(dir, _config.descend_dir)
|
|
|
|
local filename = Path.Get_Filename(file)
|
|
|
|
while true do
|
|
local current_dir = walker_func()
|
|
if current_dir == nil then
|
|
break
|
|
end
|
|
|
|
for _, exts in ipairs(mapped_exts) do
|
|
local current_path = current_dir .. filename .. "." .. exts
|
|
|
|
if vim.fn.filereadable(current_path) > 0 then
|
|
if open_in_new_tab == true then
|
|
open_in_tab(current_path)
|
|
else
|
|
open(current_path)
|
|
end
|
|
return
|
|
end
|
|
end
|
|
end
|
|
print("No file found")
|
|
end
|
|
end
|
|
|
|
function M.Switch_Open(open_in_new_tab)
|
|
local tab = open_in_new_tab
|
|
return function()
|
|
M.Switch(tab)
|
|
end
|
|
end
|
|
|
|
function M.setup(opts)
|
|
local options = opts or {}
|
|
|
|
if options.extensions ~= nil then
|
|
for _, exts in ipairs(options.extensions) do
|
|
mapping:add(exts[1], exts[2])
|
|
end
|
|
end
|
|
|
|
if options.descend_dirs ~= nil then
|
|
for _, dirs in ipairs(options.descend_dirs) do
|
|
table.insert(_config.descend_dir, dirs)
|
|
end
|
|
end
|
|
|
|
if options.switch_shortcut ~= nil then
|
|
_config.switch_shortcut = options.switch_shortcut
|
|
end
|
|
end
|
|
|
|
return M
|