From 0c2b5abc9725588b4a575eb4fcb9034d2a86a2d4 Mon Sep 17 00:00:00 2001 From: rootiest Date: Sun, 3 Nov 2024 01:52:42 -0400 Subject: [PATCH] refactor: consolidate all data module references into a single data object --- lua/data/init.lua | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/lua/data/init.lua b/lua/data/init.lua index 3b4b7db..1f11499 100644 --- a/lua/data/init.lua +++ b/lua/data/init.lua @@ -1,37 +1,52 @@ ---@module "data" --- This module aggregates various data tables used throughout the configuration. -local M = {} +local data = {} -- Explicitly specify for LSP support -M.keys = require("data.keys") -M.types = require("data.types") -M.func = require("data.func") -M.cmd = require("data.cmd") -M.deps = require("data.deps") -M.dash = require("data.dash") +data.keys = require("data.keys") +data.types = require("data.types") +data.func = require("data.func") +data.cmd = require("data.cmd") +data.deps = require("data.deps") +data.dash = require("data.dash") +data.ft = require("data.ft") +data.events = require("data.events") +data.autocmd = require("data.autocmd") +data.cond = require("data.cond") --- Function to iterate over files in the directory -local function read_data_dir() - -- Getting the root path for the current module directory - local dir_path = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":h") - - -- Open the directory - local files = vim.fn.readdir(dir_path) +-- Generic function to iterate over files in a specified directory and load Lua modules +function data.load_modules_from_dir(directory, module_prefix) + -- Open the specified directory + local files = vim.fn.readdir(directory) for _, file in ipairs(files) do -- Skip init.lua if file ~= "init.lua" and file:match(".*%.lua$") then -- Get the module name without the .lua extension local module_name = file:sub(1, -5) + -- Construct the module path + local module_path = module_prefix .. "." .. module_name -- Load the module if not already explicitly set - if not M[module_name] then - M[module_name] = require("data." .. module_name) + if not _G[module_name] then + _G[module_name] = require(module_path) end end end end +-- Example usage +local function read_data_dir() + -- Getting the root path for the current module directory + local dir_path = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":h") + -- Load modules from the data directory + data.load_modules_from_dir(dir_path, "data") +end + -- Read the directory and load any additional modules read_data_dir() -return M +---@alias DataModule table + +---@type DataModule + +return data