Skip to content

Commit 3014915

Browse files
authored
Merge pull request #76 from jghauser/small-refactors
refactor: various small refactors
2 parents cd37ad4 + 08237dc commit 3014915

File tree

11 files changed

+124
-99
lines changed

11 files changed

+124
-99
lines changed

lua/papis/completion/data.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ local function make_completion_items()
3131
local completion_items = {}
3232
local tags = get_all_tags()
3333
for _, tag in ipairs(tags) do
34-
table.insert(completion_items, {
35-
word = tag, -- what is inserted?
36-
label = tag, -- what is visible in the completion popup
37-
})
34+
completion_items[#completion_items + 1] = {
35+
word = tag,
36+
label = tag,
37+
}
3838
end
3939
return completion_items
4040
end

lua/papis/completion/source.lua

+19-17
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,32 @@ if not db then
1616
end
1717
local tag_delimiter
1818

19+
-- Mapping table for tag delimiters
20+
local tag_delimiters = {
21+
tbl = "- ",
22+
[","] = ", ",
23+
[";"] = "; ",
24+
[" "] = " ",
25+
}
26+
1927
---Gets tag_delimiter for the tag_format
2028
---@return string|nil #The delimiter between tags given the format
2129
local function get_tag_delimiter()
2230
local tag_format = db.state:get_value({ id = 1 }, "tag_format")
23-
if tag_format == "tbl" then
24-
tag_delimiter = "- "
25-
elseif tag_format == "," then
26-
tag_delimiter = ", "
27-
elseif tag_format == ";" then
28-
tag_delimiter = "; "
29-
elseif tag_format == " " then
30-
tag_delimiter = tag_format
31-
end
31+
-- Use the mapping table to get the tag_delimiter
32+
tag_delimiter = tag_delimiters[tag_format]
3233
return tag_delimiter
3334
end
3435

36+
local parse_query = ts.query.parse(
37+
"yaml",
38+
[[
39+
(block_mapping_pair
40+
key: (flow_node) @name (#eq? @name "tags")
41+
) @capture
42+
]]
43+
)
44+
3545
local M = {}
3646

3747
---Creates a new cmp source
@@ -64,14 +74,6 @@ function M:is_available()
6474
local parser = ts.get_parser(0, "yaml")
6575
local root = parser:parse()[1]:root()
6676
local start_row, _, _, end_row, _, _ = unpack(ts.get_range(root))
67-
local parse_query = ts.query.parse(
68-
"yaml",
69-
[[
70-
(block_mapping_pair
71-
key: (flow_node) @name (#eq? @name "tags")
72-
) @capture
73-
]]
74-
)
7577
local cur_row, _ = unpack(api.nvim_win_get_cursor(0))
7678
-- check all captured nodes
7779
for id, node, _ in parse_query:iter_captures(root, 0, start_row, end_row) do

lua/papis/formatter/init.lua

+15-11
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@ local log = require("papis.log")
1010
local create_autocmd = vim.api.nvim_create_autocmd
1111
local create_augroup = vim.api.nvim_create_augroup
1212

13+
local augroup = create_augroup("papisFormatter", { clear = true })
14+
local autocmd = {
15+
pattern = nil,
16+
callback = nil,
17+
group = augroup,
18+
once = true,
19+
desc = "Papis: format a newly created note",
20+
}
21+
1322
local M = {}
1423

1524
function M.create_autocmd(pattern, callback, entry)
16-
local papisFormatter = create_augroup("papisFormatter", { clear = true })
17-
create_autocmd("BufEnter", {
18-
pattern = pattern,
19-
callback = function()
20-
log.debug("Running formatter callback...")
21-
callback(entry)
22-
end,
23-
group = papisFormatter,
24-
once = true,
25-
desc = "Papis: format a newly created note",
26-
})
25+
autocmd.pattern = pattern
26+
autocmd.callback = function()
27+
log.debug("Running formatter callback...")
28+
callback(entry)
29+
end
30+
create_autocmd("BufEnter", autocmd)
2731
end
2832

2933
return M

lua/papis/fs-watcher.lua

+21-14
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ local fs_watching_stopped = false
3434
---@param on_error function #Function to run on error
3535
local function do_watch(path, on_event, on_error)
3636
local handle = uv.new_fs_event()
37+
if not handle then
38+
return
39+
end
3740
local unwatch_cb = function()
38-
uv.fs_event_stop(handle)
41+
handle:stop()
3942
end
4043
local event_cb = function(err, filename)
4144
if err then
@@ -44,8 +47,8 @@ local function do_watch(path, on_event, on_error)
4447
on_event(filename, unwatch_cb)
4548
end
4649
end
47-
uv.fs_event_start(handle, tostring(path), {}, event_cb)
48-
table.insert(handles, handle)
50+
handle:start(tostring(path), {}, event_cb)
51+
handles[#handles + 1] = handle
4952
end
5053

5154
---Gets all directories in the library_dir
@@ -54,7 +57,7 @@ local function get_library_dirs()
5457
local library_dir = Path(db.config:get_value({ id = 1 }, "dir"))
5558
local library_dirs = {}
5659
for path in library_dir:fs_iterdir() do
57-
table.insert(library_dirs, path)
60+
library_dirs[#library_dirs + 1] = path
5861
end
5962
return library_dirs
6063
end
@@ -79,14 +82,16 @@ local function init_fs_watcher(dir_to_watch, is_library_root)
7982
log.debug("Filesystem event in the library root directory")
8083
entry_dir = Path(dir_to_watch, filename)
8184
info_path = entry_dir / info_name
85+
local entry_dir_str = tostring(entry_dir)
86+
local info_path_str = tostring(info_path)
8287
if entry_dir:exists() and entry_dir:is_dir() then
83-
log.debug(string.format("Filesystem event: path '%s' added", tostring(entry_dir)))
84-
init_fs_watcher(tostring(entry_dir))
88+
log.debug(string.format("Filesystem event: path '%s' added", entry_dir_str))
89+
init_fs_watcher(entry_dir_str)
8590
if info_path:exists() then
86-
mtime = fs_stat(tostring(info_path)).mtime.sec
91+
mtime = fs_stat(info_path_str).mtime.sec
8792
end
8893
elseif entry_dir:is_dir() then
89-
log.debug(string.format("Filesystem event: path '' removed", tostring(entry_dir)))
94+
log.debug(string.format("Filesystem event: path '' removed", entry_dir_str))
9095
-- don't update here, because we'll catch it below under entry events
9196
do_update = false
9297
else
@@ -97,24 +102,26 @@ local function init_fs_watcher(dir_to_watch, is_library_root)
97102
log.debug("Filesystem event in entry directory")
98103
entry_dir = Path(dir_to_watch)
99104
info_path = entry_dir / info_name
105+
local info_path_str = tostring(info_path)
100106
if info_path:exists() then
101107
-- info file exists, update with new info
102-
log.debug(string.format("Filesystem event: '%s' changed", tostring(info_path)))
108+
log.debug(string.format("Filesystem event: '%s' changed", info_path_str))
103109
mtime = fs_stat(tostring(info_path)).mtime.sec
104110
elseif not entry_dir:exists() then
105111
-- info file and entry dir don't exist. delete entry (mtime = nil) and remove watcher
106-
log.debug(string.format("Filesystem event: '%s' removed", tostring(info_path)))
112+
log.debug(string.format("Filesystem event: '%s' removed", info_path_str))
107113
do_unwatch = true
108114
else
109115
-- info file doesn't exist but entry dir does. delete entry but keep watcher
110-
log.debug(string.format("Filesystem event: '%s' removed", tostring(info_path)))
116+
log.debug(string.format("Filesystem event: '%s' removed", info_path_str))
111117
end
112118
end
113119
if do_update then
120+
local info_path_str = tostring(info_path)
114121
log.debug("Update database for this fs event...")
115-
log.debug("Updating: " .. vim.inspect({ path = tostring(info_path), mtime = mtime }))
122+
log.debug("Updating: " .. vim.inspect({ path = info_path_str, mtime = mtime }))
116123
vim.defer_fn(function()
117-
data.update_db({ path = tostring(info_path), mtime = mtime })
124+
data.update_db({ path = info_path_str, mtime = mtime })
118125
end, 200)
119126
elseif do_unwatch then
120127
log.debug("Removing watcher")
@@ -225,7 +232,7 @@ function M.stop()
225232
if not vim.tbl_isempty(handles) then
226233
log.trace("Stopping the fs watchers")
227234
for _, handle in ipairs(handles) do
228-
uv.fs_event_stop(handle)
235+
handle:stop()
229236
end
230237
handles = {}
231238
db.state:set_fw_running()

lua/papis/init.lua

+18-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
local config = require("papis.config")
99
local api = vim.api
10-
1110
local log
1211

1312
---Creates the `autocmd` that starts papis.nvim when configured conditions are fulfilled
@@ -21,6 +20,20 @@ local function make_start_autocmd()
2120
})
2221
end
2322

23+
---Checks whether dependencies are available
24+
local function are_dependencies_available()
25+
local dependencies = { "papis", config["yq_bin"] }
26+
for _, dependency in ipairs(dependencies) do
27+
if vim.fn.executable(dependency) == 0 then
28+
log.error(
29+
string.format("The executable '%s' could not be found. Please install it to use papis.nvim", dependency)
30+
)
31+
return false
32+
end
33+
end
34+
return true
35+
end
36+
2437
local M = {}
2538

2639
---This function is run when neovim starts and sets up papis.nvim.
@@ -42,15 +55,8 @@ function M.start()
4255
-- ensure that config options from Papis (python app) are setup
4356
config:setup_papis_py_conf()
4457

45-
-- checking for dependencies
46-
local dependencies = { "papis", config["yq_bin"] }
47-
for _, dependency in ipairs(dependencies) do
48-
if vim.fn.executable(dependency) == 0 then
49-
log.error(
50-
string.format("The executable '%s' could not be found. Please install it to use papis.nvim", dependency)
51-
)
52-
return nil
53-
end
58+
if not are_dependencies_available() then
59+
return nil
5460
end
5561

5662
-- require what's necessary within `M.start()` instead of globally to allow lazy-loading
@@ -64,14 +70,13 @@ function M.start()
6470
log.warn("Requiring `data.lua` failed. Aborting...")
6571
return nil
6672
end
67-
local does_pid_exist = require("papis.utils").does_pid_exist
6873

6974
-- setup commands
7075
require("papis.commands").setup()
7176
-- setup keymaps
7277
require("papis.keymaps"):setup()
7378

74-
-- get all functions that we need to run the various commands
79+
-- setup enabled modules
7580
for module_name, _ in pairs(config["enable_modules"]) do
7681
log.trace(module_name .. " is enabled")
7782
local has_module, module = pcall(require, "papis." .. module_name)
@@ -83,6 +88,7 @@ function M.start()
8388
end
8489

8590
-- check if other neovim instances has file watchers
91+
local does_pid_exist = require("papis.utils").does_pid_exist
8692
if not does_pid_exist(db.state:get_fw_running()) then
8793
-- setup file watchers (or an autocmd if another instance has file watchers)
8894
if config["enable_fs_watcher"] then

lua/papis/log.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ M.new = function(config, standalone)
152152
local fmt = table.remove(passed, 1)
153153
local inspected = {}
154154
for _, v in ipairs(passed) do
155-
table.insert(inspected, vim.inspect(v))
155+
inspected[#inspected + 1] = vim.inspect(v)
156156
end
157157
return string.format(fmt, unpack(inspected))
158158
end)

lua/papis/papis-storage.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ local function make_full_paths(filenames, path)
124124
local full_paths = {}
125125
for _, filename in ipairs(filenames) do
126126
local full_path = tostring(Path(path, filename))
127-
table.insert(full_paths, full_path)
127+
full_paths[#full_paths + 1] = full_path
128128
end
129129
return full_paths
130130
end
@@ -147,14 +147,14 @@ function M.get_metadata(paths)
147147
paths = {}
148148
for path in library_dir:fs_iterdir() do
149149
if path:basename() == info_name then
150-
table.insert(paths, path)
150+
paths[#paths + 1] = path
151151
end
152152
end
153153
end
154154
local metadata = {}
155155
for _, path in ipairs(paths) do
156156
local mtime = fs_stat(tostring(path)).mtime.sec
157-
table.insert(metadata, { path = tostring(path), mtime = mtime })
157+
metadata[#metadata + 1] = { path = tostring(path), mtime = mtime }
158158
end
159159
return metadata
160160
end
@@ -170,7 +170,7 @@ function M.get_data_full(metadata)
170170
local mtime = metadata_v["mtime"]
171171
local entry = read_yaml(path)
172172
if is_valid_entry(entry, path) then
173-
entry = do_convert_entry_keys(entry)
173+
entry = do_convert_entry_keys(entry) --NOTE: entry is never nil because of `is_valid_entry()`
174174
local data = {}
175175
for key, type_of_val in pairs(data_tbl_schema) do
176176
if type(type_of_val) == "table" then
@@ -205,7 +205,7 @@ function M.get_data_full(metadata)
205205
end
206206
end
207207
end
208-
table.insert(data_complete, { data, { path = path, mtime = mtime } })
208+
data_complete[#data_complete + 1] = { data, { path = path, mtime = mtime } }
209209
end
210210
end
211211
return data_complete

lua/papis/search/data.lua

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ local function format_search_string(entry)
2828

2929
local str_elements = {}
3030
if do_incl_str("author") then
31-
table.insert(str_elements, entry["author"])
31+
str_elements[#str_elements + 1] = entry["author"]
3232
elseif do_incl_str("editor", "author") then
33-
table.insert(str_elements, entry["editor"])
33+
str_elements[#str_elements + 1] = entry["editor"]
3434
end
3535
if do_incl_str("year") then
36-
table.insert(str_elements, entry["year"])
36+
str_elements[#str_elements + 1] = entry["year"]
3737
end
3838
if do_incl_str("title") then
39-
table.insert(str_elements, entry["title"])
39+
str_elements[#str_elements + 1] = entry["title"]
4040
end
4141
if do_incl_str("type") then
42-
table.insert(str_elements, entry["type"])
42+
str_elements[#str_elements + 1] = entry["type"]
4343
end
4444
if do_incl_str("tags") then
45-
table.insert(str_elements, table.concat(entry["tags"], " "))
45+
str_elements[#str_elements + 1] = table.concat(entry["tags"], " ")
4646
end
4747
local search_string = table.concat(str_elements, " ")
4848
return search_string
@@ -107,10 +107,10 @@ local function init_tbl()
107107
local items = {}
108108
local displayer_tbl = {}
109109
for _, vv in ipairs(display_strings) do
110-
table.insert(items, { width = vim.fn.strdisplaywidth(vv[1], 1) })
111-
table.insert(displayer_tbl, { vv[1], vv[2] })
110+
items[#items + 1] = { width = vim.fn.strdisplaywidth(vv[1], 1) }
111+
displayer_tbl[#displayer_tbl + 1] = { vv[1], vv[2] }
112112
end
113-
table.insert(items, { remaining = true })
113+
items[#items + 1] = { remaining = true }
114114

115115
local timestamp = make_timestamp(entry)
116116

lua/papis/search/init.lua

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ end
2222

2323
local telescope_precalc = {}
2424

25+
---Create a telescope entry for a given db entry
26+
---@param entry table #A entry in the library db
27+
---@return table #A telescope entry
2528
local entry_maker = function(entry)
2629
local entry_pre_calc = db["search"]:get(entry["id"])[1]
2730
local timestamp = entry_pre_calc["timestamp"]
@@ -78,11 +81,15 @@ local module_keymaps = {
7881

7982
local M = {}
8083

84+
---Updates the precalculated telescope entry for the picker
85+
---@param entry table #The db entry for which to update the telescope entry
8186
function M.update_precalc(entry)
8287
local id = entry["id"]
8388
telescope_precalc[id] = entry_maker(entry)
8489
end
8590

91+
---Get precalcuated telescope entries (or create them if they don't yet exist)
92+
---@return table #Table with precalculated telescope entries for all db entries
8693
function M.get_precalc()
8794
if vim.tbl_isempty(telescope_precalc) then
8895
local entries = db.data:get()

0 commit comments

Comments
 (0)