Add the switch function and config options

This commit is contained in:
Folkert Kevelam 2024-10-12 21:43:49 +02:00
parent e706a75645
commit 3879ab1eed

View File

@ -1,27 +1,81 @@
local M = {} local M = {}
local Extension_Map = require"SpecSwitcher.ExtensionMap" local Extension_Map = require"SpecSwitcher.ExtensionMap"
local Path = require"SpecSwitcher.path"
local Config = { local _config = {
test = "hallo" descend_dir = {},
switch_shortcut = "<leader>n"
} }
local mapping = Extension_Map:new()
function open(filename) function open(filename)
vim.cmd("e " .. filename) vim.cmd("e " .. filename)
end end
function M.setup(opts) function M.Switch()
opts = opts or {} local current_dir = vim.api.nvim_buf_get_name(0)
for k,v in pairs(opts) do local file = nil
Config[k] = v 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
open(current_path)
return
end
end
end
print("No file found")
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 end
local mapping = Extension_Map:new() if options.descend_dirs ~= nil then
for _, dirs in ipairs(options.descend_dirs) do
table.insert(_config.descend_dir, dirs)
end
end
mapping:add("c", {"h", "H", "hh", "HH"}) if options.switch_shortcut ~= nil then
_config.switch_shortcut = options.switch_shortcut
end
mapping:print() vim.keymap.set('n', _config.switch_shortcut, M.Switch)
end end
return M return M