Add the switch function and config options
This commit is contained in:
parent
e706a75645
commit
3879ab1eed
|
|
@ -1,27 +1,81 @@
|
|||
local M = {}
|
||||
|
||||
local Extension_Map = require"SpecSwitcher.ExtensionMap"
|
||||
local Path = require"SpecSwitcher.path"
|
||||
|
||||
local Config = {
|
||||
test = "hallo"
|
||||
local _config = {
|
||||
descend_dir = {},
|
||||
switch_shortcut = "<leader>n"
|
||||
}
|
||||
|
||||
local mapping = Extension_Map:new()
|
||||
|
||||
function open(filename)
|
||||
vim.cmd("e " .. filename)
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
opts = opts or {}
|
||||
function M.Switch()
|
||||
local current_dir = vim.api.nvim_buf_get_name(0)
|
||||
|
||||
for k,v in pairs(opts) do
|
||||
Config[k] = v
|
||||
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
|
||||
|
||||
local mapping = Extension_Map:new()
|
||||
for _, exts in ipairs(mapped_exts) do
|
||||
local current_path = current_dir .. filename .. "." .. exts
|
||||
|
||||
mapping:add("c", {"h", "H", "hh", "HH"})
|
||||
if vim.fn.filereadable(current_path) > 0 then
|
||||
open(current_path)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
print("No file found")
|
||||
end
|
||||
end
|
||||
|
||||
mapping:print()
|
||||
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
|
||||
|
||||
vim.keymap.set('n', _config.switch_shortcut, M.Switch)
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user