diff --git a/lua/SpecSwitcher/init.lua b/lua/SpecSwitcher/init.lua index a7382b4..abecbb7 100644 --- a/lua/SpecSwitcher/init.lua +++ b/lua/SpecSwitcher/init.lua @@ -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 = "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 + + 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 - 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 return M