From d4a071e26ba49d147c6ceaa7fe209d6c6e5d10fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Boczar?= Date: Wed, 10 Apr 2024 17:31:17 +0200 Subject: [PATCH] feat: be more lazy when loading --- lua/possession.lua | 19 ++++++++++++------- lua/possession/commands.lua | 13 +++++++++---- lua/possession/utils.lua | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/lua/possession.lua b/lua/possession.lua index ba0845f..1ef542e 100644 --- a/lua/possession.lua +++ b/lua/possession.lua @@ -1,5 +1,4 @@ local config = require('possession.config') -local session = require('possession.session') local function cmd(name, args_desc, opts, cb) local desc = name .. ' ' .. args_desc @@ -57,12 +56,18 @@ local function setup(opts) end end +local function lazy(mod, func) + return function(...) + return require(mod)[func](...) + end +end + return { setup = setup, - save = session.save, - load = session.load, - delete = session.delete, - show = session.show, - list = session.list, - last = session.last, + save = lazy('possession.session', 'save'), + load = lazy('possession.session', 'load'), + delete = lazy('possession.session', 'delete'), + show = lazy('possession.session', 'show'), + list = lazy('possession.session', 'list'), + last = lazy('possession.session', 'last'), } diff --git a/lua/possession/commands.lua b/lua/possession/commands.lua index d2011cf..05ca924 100644 --- a/lua/possession/commands.lua +++ b/lua/possession/commands.lua @@ -1,11 +1,16 @@ local M = {} -local session = require('possession.session') -local display = require('possession.display') -local paths = require('possession.paths') -local migrate = require('possession.migrate') local utils = require('possession.utils') +---@module "possession.session" +local session = utils.lazy_mod('possession.session') +---@module "possession.display" +local display = utils.lazy_mod('possession.display') +---@module "possession.paths" +local paths = utils.lazy_mod('possession.paths') +---@module "possession.migrate" +local migrate = utils.lazy_mod('possession.migrate') + local function complete_list(candidates, opts) opts = vim.tbl_extend('force', { sort = true, diff --git a/lua/possession/utils.lua b/lua/possession/utils.lua index 3f559f2..3b72b93 100644 --- a/lua/possession/utils.lua +++ b/lua/possession/utils.lua @@ -1,10 +1,9 @@ local M = {} -local Path = require('plenary.path') local config = require('possession.config') -local logging = require('possession.logging') function M.debug(...) + local logging = require('possession.logging') if config.debug then local log_fn = config.silent and logging.to_file or logging.to_all local args = { ... } @@ -17,15 +16,18 @@ function M.debug(...) end function M.info(...) + local logging = require('possession.logging') local log_fn = config.silent and logging.to_file or logging.to_all log_fn(string.format(...), vim.log.levels.INFO) end function M.warn(...) + local logging = require('possession.logging') logging.to_all(string.format(...), vim.log.levels.WARN) end function M.error(...) + local logging = require('possession.logging') logging.to_all(string.format(...), vim.log.levels.ERROR) end @@ -266,6 +268,8 @@ end ---@param opts? { force?: boolean, normalize?: boolean } defaults to force=false, normalize=true ---@return string function M.relative_path(path, rel_to, opts) + local Path = require('plenary.path') + opts = vim.tbl_extend('force', { force = false, normalize = true, @@ -366,4 +370,32 @@ function M.try(fn, catch, finally) end end +---@param keys string[] +local function lazy_index(mod, keys) + return setmetatable({}, { + __call = function(_, ...) + local fn = vim.tbl_get(mod(), unpack(keys)) + return fn(...) + end, + __index = function(_, key) + return lazy_index(mod, vim.list_extend(vim.list_slice(keys), { key })) + end, + }) +end + +---@param path string +function M.lazy_mod(path) + local load = function() + return require(path) + end + local mod + mod = setmetatable({ path = path }, { + __call = load, + __index = function(_, key) + return lazy_index(mod, { key }) + end, + }) + return mod +end + return M