Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve lazy-loading #68

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/papis.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*papis.txt* For NVIM v0.8.0 Last change: 2024 June 09
*papis.txt* For NVIM v0.8.0 Last change: 2024 June 12

==============================================================================
Table of Contents *papis-table-of-contents*
Expand Down
4 changes: 1 addition & 3 deletions lua/papis/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ local commands = {
reinit_papis_py_config = {
name = "PapisReInitConfig",
command = function()
local testing_session = config["enable_modules"]["testing"]
local papis_py_conf_new = config:get_papis_py_conf(testing_session)
config:compare_papis_py_conf(papis_py_conf_new)
config:update_papis_py_conf()
end,
opts = { desc = "Papis: import configuration from Papis" },
},
Expand Down
50 changes: 25 additions & 25 deletions lua/papis/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,10 @@ local default_config = {
},
}

local M = vim.deepcopy(default_config)

---Queries papis to get info-name and dir settings.
---Queries Papis to get various options.
---@param testing_session boolean #If true, will use testing papis conf
---@return table #A table { info_name = val, dir = val }
function M:get_papis_py_conf(testing_session)
local function get_papis_py_conf(testing_session)
local papis_conf_keys = { "info-name", "notes-name", "dir" }
local papis_py_conf_new = {}
local testing_conf_path = ""
Expand All @@ -172,15 +170,18 @@ function M:get_papis_py_conf(testing_session)
return papis_py_conf_new
end

local M = vim.deepcopy(default_config)

---Compares and updates Queries papis to get info-name and dir settings. It is very slow and shouldn't be used
---if possible.
---@param papis_py_conf_new table #A table with new (read from Papis) config entries
function M:compare_papis_py_conf(papis_py_conf_new)
function M:update_papis_py_conf()
local db = require("papis.sqlite-wrapper")
if not db then
return
end

local is_testing_session = self["enable_modules"]["testing"]
local papis_py_conf_new = get_papis_py_conf(is_testing_session)
local papis_py_conf_old = db.config:get()[1]
papis_py_conf_old["id"] = nil

Expand All @@ -194,6 +195,24 @@ function M:compare_papis_py_conf(papis_py_conf_new)
end
end

---Sets up Papis configuration values if not already done.
function M:setup_papis_py_conf()
local db = require("papis.sqlite-wrapper")
if not db then
return
end
local log = require("papis.log")

-- get config from Papis if not already in db
if not db.config:is_setup() then
log.info("Papis.nvim configuration not setup, importing values from Papis now")
local testing_session = self["enable_modules"]["testing"]
local papis_py_conf_new = get_papis_py_conf(testing_session)
db.config:drop()
db.config:update({ id = 1 }, papis_py_conf_new)
end
end

---Updates the default configuration with user supplied options and gets conf from Papis
---@param opts table #Same format as default_config and contains user config
function M:update(opts)
Expand All @@ -219,25 +238,6 @@ function M:update(opts)
for k, v in pairs(newconf) do
self[k] = v
end

local db = require("papis.sqlite-wrapper")
if not db then
return
end
local log = require("papis.log")
if not log then
return
end

-- get config from Papis if not already in db
if not db.config:is_setup() then
log.new(self["log"] or log.get_default_config(), true)
log.info("Papis.nvim configuration not setup, importing values from Papis now")
local testing_session = self["enable_modules"]["testing"]
local papis_py_conf_new = self:get_papis_py_conf(testing_session)
db.config:drop()
db.config:update({ id = 1 }, papis_py_conf_new)
end
end

return M
22 changes: 11 additions & 11 deletions lua/papis/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,26 @@ end

local M = {}

---This function is run when neovim starts. It sets up the `PapisStart` command and autocmd
---to allow lazy-loading of papis.nvim.
---This function is run when neovim starts and sets up papis.nvim.
---@param opts table #User configuration
function M.setup(opts)
-- update config with user config
config:update(opts)

-- create autocmd that starts papis.nvim for configured filetypes
make_start_autocmd()
end

---This function starts all of papis.nvim.
function M.start()
log = require("papis.log")
log.new(config["log"] or log.get_default_config(), true)
log.debug("_________________________STARTING PAPIS.NVIM_________________________")

log.debug("_________________________SETTING UP PAPIS.NVIM_________________________")
-- ensure that config options from Papis (python app) are setup
config:setup_papis_py_conf()

-- checking for dependencies
local dependencies = { "papis", config["yq_bin"] }
for _, dependency in ipairs(dependencies) do
if vim.fn.executable(dependency) == 0 then
Expand All @@ -45,14 +53,6 @@ function M.setup(opts)
end
end

log.debug("Creating autocmds to lazily load papis.nvim")
make_start_autocmd()
end

---This function starts all of papis.nvim.
function M.start()
log.debug("Starting papis.nvim")

-- require what's necessary within `M.start()` instead of globally to allow lazy-loading
local db = require("papis.sqlite-wrapper")
if not db then
Expand Down