Skip to content

Commit b685f69

Browse files
author
jghauser
committed
feat: improve lazy-loading
1 parent 5463587 commit b685f69

File tree

3 files changed

+37
-39
lines changed

3 files changed

+37
-39
lines changed

lua/papis/commands.lua

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ local commands = {
2020
reinit_papis_py_config = {
2121
name = "PapisReInitConfig",
2222
command = function()
23-
local testing_session = config["enable_modules"]["testing"]
24-
local papis_py_conf_new = config:get_papis_py_conf(testing_session)
25-
config:compare_papis_py_conf(papis_py_conf_new)
23+
config:update_papis_py_conf()
2624
end,
2725
opts = { desc = "Papis: import configuration from Papis" },
2826
},

lua/papis/config.lua

+25-25
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,10 @@ local default_config = {
143143
},
144144
}
145145

146-
local M = vim.deepcopy(default_config)
147-
148-
---Queries papis to get info-name and dir settings.
146+
---Queries Papis to get various options.
149147
---@param testing_session boolean #If true, will use testing papis conf
150148
---@return table #A table { info_name = val, dir = val }
151-
function M:get_papis_py_conf(testing_session)
149+
local function get_papis_py_conf(testing_session)
152150
local papis_conf_keys = { "info-name", "notes-name", "dir" }
153151
local papis_py_conf_new = {}
154152
local testing_conf_path = ""
@@ -172,15 +170,18 @@ function M:get_papis_py_conf(testing_session)
172170
return papis_py_conf_new
173171
end
174172

173+
local M = vim.deepcopy(default_config)
174+
175175
---Compares and updates Queries papis to get info-name and dir settings. It is very slow and shouldn't be used
176176
---if possible.
177-
---@param papis_py_conf_new table #A table with new (read from Papis) config entries
178-
function M:compare_papis_py_conf(papis_py_conf_new)
177+
function M:update_papis_py_conf()
179178
local db = require("papis.sqlite-wrapper")
180179
if not db then
181180
return
182181
end
183182

183+
local is_testing_session = self["enable_modules"]["testing"]
184+
local papis_py_conf_new = get_papis_py_conf(is_testing_session)
184185
local papis_py_conf_old = db.config:get()[1]
185186
papis_py_conf_old["id"] = nil
186187

@@ -194,6 +195,24 @@ function M:compare_papis_py_conf(papis_py_conf_new)
194195
end
195196
end
196197

198+
---Sets up Papis configuration values if not already done.
199+
function M:setup_papis_py_conf()
200+
local db = require("papis.sqlite-wrapper")
201+
if not db then
202+
return
203+
end
204+
local log = require("papis.log")
205+
206+
-- get config from Papis if not already in db
207+
if not db.config:is_setup() then
208+
log.info("Papis.nvim configuration not setup, importing values from Papis now")
209+
local testing_session = self["enable_modules"]["testing"]
210+
local papis_py_conf_new = get_papis_py_conf(testing_session)
211+
db.config:drop()
212+
db.config:update({ id = 1 }, papis_py_conf_new)
213+
end
214+
end
215+
197216
---Updates the default configuration with user supplied options and gets conf from Papis
198217
---@param opts table #Same format as default_config and contains user config
199218
function M:update(opts)
@@ -219,25 +238,6 @@ function M:update(opts)
219238
for k, v in pairs(newconf) do
220239
self[k] = v
221240
end
222-
223-
local db = require("papis.sqlite-wrapper")
224-
if not db then
225-
return
226-
end
227-
local log = require("papis.log")
228-
if not log then
229-
return
230-
end
231-
232-
-- get config from Papis if not already in db
233-
if not db.config:is_setup() then
234-
log.new(self["log"] or log.get_default_config(), true)
235-
log.info("Papis.nvim configuration not setup, importing values from Papis now")
236-
local testing_session = self["enable_modules"]["testing"]
237-
local papis_py_conf_new = self:get_papis_py_conf(testing_session)
238-
db.config:drop()
239-
db.config:update({ id = 1 }, papis_py_conf_new)
240-
end
241241
end
242242

243243
return M

lua/papis/init.lua

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,26 @@ end
2323

2424
local M = {}
2525

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

32+
-- create autocmd that starts papis.nvim for configured filetypes
33+
make_start_autocmd()
34+
end
35+
36+
---This function starts all of papis.nvim.
37+
function M.start()
3338
log = require("papis.log")
3439
log.new(config["log"] or log.get_default_config(), true)
40+
log.debug("_________________________STARTING PAPIS.NVIM_________________________")
3541

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

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

48-
log.debug("Creating autocmds to lazily load papis.nvim")
49-
make_start_autocmd()
50-
end
51-
52-
---This function starts all of papis.nvim.
53-
function M.start()
54-
log.debug("Starting papis.nvim")
55-
5656
-- require what's necessary within `M.start()` instead of globally to allow lazy-loading
5757
local db = require("papis.sqlite-wrapper")
5858
if not db then

0 commit comments

Comments
 (0)