Skip to content

Commit

Permalink
feat: be more lazy when loading
Browse files Browse the repository at this point in the history
  • Loading branch information
jedrzejboczar committed Apr 10, 2024
1 parent bfef005 commit d4a071e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
19 changes: 12 additions & 7 deletions lua/possession.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'),
}
13 changes: 9 additions & 4 deletions lua/possession/commands.lua
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
36 changes: 34 additions & 2 deletions lua/possession/utils.lua
Original file line number Diff line number Diff line change
@@ -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 = { ... }
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit d4a071e

Please sign in to comment.