Skip to content

Commit

Permalink
Merge pull request #83 from jghauser/simplify-table-indexing
Browse files Browse the repository at this point in the history
refactor: use dot indexing where possible and consistent
  • Loading branch information
jghauser authored Jul 2, 2024
2 parents 26e8a50 + ac7e58f commit 8e9b676
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 112 deletions.
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 24
*papis.txt* For NVIM v0.8.0 Last change: 2024 July 02

==============================================================================
Table of Contents *papis-table-of-contents*
Expand Down
4 changes: 2 additions & 2 deletions lua/papis/at-cursor/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local fn = vim.fn

local log = require("papis.log")
local config = require("papis.config")
local popup_format = config["at-cursor"]["popup_format"]
local popup_format = config["at-cursor"].popup_format
local utils = require("papis.utils")
local commands = require("papis.commands")
local keymaps = require("papis.keymaps")
Expand Down Expand Up @@ -57,7 +57,7 @@ local function if_ref_valid_run_fun(fun, self, type)
local ref = get_ref_under_cursor()
local entry = db.data:get({ ref = ref }, { "papis_id" })
if not vim.tbl_isempty(entry) then
local papis_id = entry[1]["papis_id"]
local papis_id = entry[1].papis_id
if self then
fun(self, papis_id, type)
else
Expand Down
4 changes: 2 additions & 2 deletions lua/papis/completion/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ local function get_all_tags()
local all_tags = {}
local result = db.data:get(nil, { "tags" })
for _, entry in pairs(result) do
if entry["tags"] then
for _, tag in pairs(entry["tags"]) do
if entry.tags then
for _, tag in pairs(entry.tags) do
all_tags[tag] = true
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/papis/completion/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function M:complete(request, callback)

if comp_after_tag_delimiter or comp_after_keyword then
log.debug("Running cmp `complete()` function.")
self.items = db.completion:get()[1]["tag_strings"]
self.items = db.completion:get()[1].tag_strings
callback(self.items)
end
end
Expand Down
8 changes: 4 additions & 4 deletions lua/papis/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ local default_config = {
db_path = vim.fn.stdpath("data") .. "/papis_db/papis-nvim.sqlite3",
yq_bin = "yq",
create_new_note_fn = function(papis_id, notes_name)
local testing_session = require("papis.config")["enable_modules"]["testing"]
local testing_session = require("papis.config").enable_modules["testing"]
local testing_conf_path = ""
if testing_session then
testing_conf_path = "-c ./tests/papis_config "
Expand Down Expand Up @@ -159,15 +159,15 @@ function M:update(opts)
local newconf = vim.tbl_deep_extend("force", default_config, opts or {})

-- set disabled modules to nil if false
for module_name, is_enabled in pairs(newconf["enable_modules"]) do
for module_name, is_enabled in pairs(newconf.enable_modules) do
if is_enabled == false then
newconf.enable_modules[module_name] = nil
end
end

-- if debug mode is on, log level should be at least debug
if newconf["enable_modules"]["debug"] == true then
newconf["log"] = {
if newconf.enable_modules["debug"] == true then
newconf.log = {
level = "trace",
use_console = "false",
use_file = "true",
Expand Down
32 changes: 16 additions & 16 deletions lua/papis/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local papis_storage = require("papis.papis-storage")
if not papis_storage then
return nil
end
local enable_modules = require("papis.config")["enable_modules"]
local enable_modules = require("papis.config").enable_modules
local log = require("papis.log")
local db = require("papis.sqlite-wrapper")
if not db then
Expand All @@ -25,16 +25,16 @@ local function update_module_tbls(metadata)
if has_module then
log.debug(string.format("Updating module '%s' sqlite table", module_name))
module_name = string.gsub(module_name, "-", "_")
if module["opts"]["has_row_for_each_main_tbl_row"] then
if module.opts.has_row_for_each_main_tbl_row then
-- this is to handle modules newly activated
if db[module_name]:empty() then
db["data"]:each(function(row)
db.data:each(function(row)
db[module_name]:update(row.id)
end)
else
-- we're adding or editing an entry (deletions cascade)
if metadata["mtime"] then
local id = db.metadata:get_value({ path = metadata["path"] }, "entry")
if metadata.mtime then
local id = db.metadata:get_value({ path = metadata.path }, "entry")
-- check if metadata exists (which it might not if the .yaml couldnt be read and wasn't added to db)
if id then
db[module_name]:update(id)
Expand All @@ -52,24 +52,24 @@ end
---@param metadata table #Has structure { path = path, mtime = mtime }
local function update_main_tbls(metadata)
log.debug("Updating main tables")
if metadata["mtime"] then
if metadata.mtime then
log.debug("Changing/Adding an entry")
local entry_full_data = papis_storage.get_data_full({ metadata })[1]
log.debug("Update/Add entry with following data: " .. vim.inspect(entry_full_data))
if entry_full_data then
local papis_id = entry_full_data[1]["papis_id"]
local papis_id = entry_full_data[1].papis_id
local data_row = entry_full_data[1]
local metadata_row = entry_full_data[2]
local id = db.data:get({ papis_id = papis_id }, { "id" })
if not vim.tbl_isempty(id) then
log.debug("Changing an existing entry")
id = id[1]["id"]
id = id[1].id
db.data:clean_update({ id = id }, data_row)
db.metadata:update({ id = id }, metadata_row)
else
log.debug("Adding a new entry")
id = db.data:insert(data_row)
metadata_row["entry"] = id
metadata_row.entry = id
-- check if entry already exists (can happen because fs watcher sends multiple events)
if vim.tbl_isempty(db.metadata:__get({ where = { entry = id } })) then
db.metadata:insert(metadata_row)
Expand All @@ -79,15 +79,15 @@ local function update_main_tbls(metadata)
-- we're deleting an entry
else
log.debug("Deleting an entry")
local id = db.metadata:get_value({ path = metadata["path"] }, "entry")
local id = db.metadata:get_value({ path = metadata.path }, "entry")
if id then
db.data:remove({ id = id })
-- HACK because `on_delete = cascade` doesn't work
db.metadata:remove({ entry = id })
for module_name, _ in pairs(enable_modules) do
module_name = string.gsub(module_name, "-", "_")
local has_module, module = pcall(require, "papis." .. module_name .. ".data")
if has_module and module["opts"]["has_row_for_each_main_tbl_row"] then
if has_module and module.opts.has_row_for_each_main_tbl_row then
db[module_name]:remove({ entry = id })
end
end
Expand All @@ -102,15 +102,15 @@ local function sync_storage_data()
local old_metadata = db.metadata:get()
local old_metadata_ass = {}
for _, metadata_entry in ipairs(old_metadata) do
old_metadata_ass[metadata_entry["path"]] = metadata_entry["mtime"]
old_metadata_ass[metadata_entry.path] = metadata_entry.mtime
end
local new_metadata_ass = {}
for _, metadata_entry in ipairs(new_metadata) do
new_metadata_ass[metadata_entry["path"]] = metadata_entry["mtime"]
new_metadata_ass[metadata_entry.path] = metadata_entry.mtime
end
-- handle deleted files
for _, metadata_entry in ipairs(old_metadata) do
local path_old = metadata_entry["path"]
local path_old = metadata_entry.path
if not new_metadata_ass[path_old] then
log.debug("An entry on disk has been deleted. Remove from database...")
metadata_entry = { path = path_old, mtime = nil }
Expand All @@ -120,8 +120,8 @@ local function sync_storage_data()
end
-- handle changed and new files
for _, metadata_entry in ipairs(new_metadata) do
local mtime_new = metadata_entry["mtime"]
local mtime_old = old_metadata_ass[metadata_entry["path"]]
local mtime_new = metadata_entry.mtime
local mtime_old = old_metadata_ass[metadata_entry.path]
if mtime_new ~= mtime_old then
log.debug("An entry on disk is new or has changed. Updating from yaml...")
update_main_tbls(metadata_entry)
Expand Down
8 changes: 4 additions & 4 deletions lua/papis/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ local reports = {}
---Creates a report for the sqlite database
reports["sqlite-wrapper"] = function()
local sqlite_is_executable = vim.fn.executable("sqlite3")
local db_exists = Path(config["db_path"]):exists()
local db_exists = Path(config.db_path):exists()
local db_is_empty = db.data:empty()

health.start("Sqlite database")
health.info(string.format("Papis.nvim db location: %s.", config["db_path"]))
health.info(string.format("Papis.nvim db location: %s.", config.db_path))
if sqlite_is_executable then
health.ok("The 'sqlite3' executable was found in path.")
else
Expand Down Expand Up @@ -86,10 +86,10 @@ M.check = function()
end

reports["sqlite-wrapper"]()
if config["enable_fs_watcher"] then
if config.enable_fs_watcher then
reports["fs-watcher"]()
end
for module_name, _ in pairs(config["enable_modules"]) do
for module_name, _ in pairs(config.enable_modules) do
if reports[module_name] then
reports[module_name]()
end
Expand Down
12 changes: 6 additions & 6 deletions lua/papis/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local function make_start_autocmd()
local load_papis = api.nvim_create_augroup("loadPapis", { clear = true })
api.nvim_create_autocmd("FileType", {
once = true,
pattern = config["init_filetypes"],
pattern = config.init_filetypes,
callback = require("papis").start,
group = load_papis,
desc = "Load papis.nvim for defined filetypes",
Expand All @@ -22,7 +22,7 @@ end

---Checks whether dependencies are available
local function are_dependencies_available()
local dependencies = { "papis", config["yq_bin"] }
local dependencies = { "papis", config.yq_bin }
for _, dependency in ipairs(dependencies) do
if vim.fn.executable(dependency) == 0 then
log.error(
Expand Down Expand Up @@ -78,11 +78,11 @@ function M.start()
require("papis.keymaps"):setup()

-- setup enabled modules
for module_name, _ in pairs(config["enable_modules"]) do
for module_name, _ in pairs(config.enable_modules) do
log.trace(module_name .. " is enabled")
local has_module, module = pcall(require, "papis." .. module_name)
if has_module then
if module["setup"] then
if module.setup then
module.setup()
end
end
Expand All @@ -92,7 +92,7 @@ function M.start()
local does_pid_exist = require("papis.utils").does_pid_exist
if not does_pid_exist(db.state:get_fw_running()) then
-- setup file watchers because no other neovim instance has them
if config["enable_fs_watcher"] then
if config.enable_fs_watcher then
require("papis.fs-watcher"):init()
end

Expand All @@ -104,7 +104,7 @@ function M.start()
end
else
-- setup file watchers (or an autocmd if another instance has file watchers)
if config["enable_fs_watcher"] then
if config.enable_fs_watcher then
require("papis.fs-watcher"):init()
end
end
Expand Down
10 changes: 5 additions & 5 deletions lua/papis/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ end
--- Recursively merges the provided table with the keymaps table.
---@param module_keymaps table #A table with a module's keymaps
function M:add_keymaps(module_keymaps)
if config["enable_keymaps"] then
if config.enable_keymaps then
for _, keymap in pairs(module_keymaps) do
local opts = vim.deepcopy(keymap["opts"])
opts["silent"] = true
opts["buffer"] = true
vim.keymap.set(keymap["mode"], keymap["lhs"], keymap["rhs"], opts)
local opts = vim.deepcopy(keymap.opts)
opts.silent = true
opts.buffer = true
vim.keymap.set(keymap.mode, keymap.lhs, keymap.rhs, opts)
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lua/papis/papis-storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ end
local utils = require("papis.utils")
local log = require("papis.log")
local config = require("papis.config")
local data_tbl_schema = config["data_tbl_schema"]
local key_name_conversions = config["papis-storage"]["key_name_conversions"]
local required_keys = config["papis-storage"]["required_keys"]
local tag_format = config["papis-storage"]["tag_format"]
local data_tbl_schema = config.data_tbl_schema
local key_name_conversions = config["papis-storage"].key_name_conversions
local required_keys = config["papis-storage"].required_keys
local tag_format = config["papis-storage"].tag_format
local have_determined_tag_format = false
local yq_bin = config["yq_bin"]
local yq_bin = config.yq_bin

---Determines if tag format is list, space separated, or comma separated
---@param tags any #Either a table or a string with tag(s)
Expand Down Expand Up @@ -166,8 +166,8 @@ function M.get_data_full(metadata)
metadata = metadata or M.get_metadata()
local data_complete = {}
for _, metadata_v in ipairs(metadata) do
local path = metadata_v["path"]
local mtime = metadata_v["mtime"]
local path = metadata_v.path
local mtime = metadata_v.mtime
local entry = read_yaml(path)
if is_valid_entry(entry, path) then
entry = do_convert_entry_keys(entry) --NOTE: entry is never nil because of `is_valid_entry()`
Expand Down Expand Up @@ -199,7 +199,7 @@ function M.get_data_full(metadata)
if type(entry[key]) == "table" then
data[key] = entry[key]
else
log.warn("Wanted to add `" .. key .. "` of `" .. entry["ref"] .. "` but the value is not of type `table`")
log.warn("Wanted to add `" .. key .. "` of `" .. entry.ref .. "` but the value is not of type `table`")
data[key] = {}
end
end
Expand Down
20 changes: 10 additions & 10 deletions lua/papis/search/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ if not db then
return nil
end
local config = require("papis.config")
local search_keys = config["search"]["search_keys"]
local results_format = config["search"]["results_format"]
local search_keys = config["search"].search_keys
local results_format = config["search"].results_format
local utils = require("papis.utils")

---Creates a string that is used to search among entries (not displayed)
Expand All @@ -28,21 +28,21 @@ local function format_search_string(entry)

local str_elements = {}
if do_incl_str("author") then
str_elements[#str_elements + 1] = entry["author"]
str_elements[#str_elements + 1] = entry.author
elseif do_incl_str("editor", "author") then
str_elements[#str_elements + 1] = entry["editor"]
str_elements[#str_elements + 1] = entry.editor
end
if do_incl_str("year") then
str_elements[#str_elements + 1] = entry["year"]
str_elements[#str_elements + 1] = entry.year
end
if do_incl_str("title") then
str_elements[#str_elements + 1] = entry["title"]
str_elements[#str_elements + 1] = entry.title
end
if do_incl_str("type") then
str_elements[#str_elements + 1] = entry["type"]
str_elements[#str_elements + 1] = entry.type
end
if do_incl_str("tags") then
str_elements[#str_elements + 1] = table.concat(entry["tags"], " ")
str_elements[#str_elements + 1] = table.concat(entry.tags, " ")
end
local search_string = table.concat(str_elements, " ")
return search_string
Expand All @@ -52,7 +52,7 @@ end
---@param entry table #A papis entry
---@return integer #The timestamp (date when entry was added in secs since epoch or 1 if missing)
local function make_timestamp(entry)
local timestamp = entry["time_added"]
local timestamp = entry.time_added
if timestamp then
local year, month, day, hour, min, sec = timestamp:match("(%d+)-(%d+)-(%d+)-(%d+):(%d+):(%d+)")
local t = { year = year, month = month, day = day, hour = hour, min = min, sec = sec }
Expand Down Expand Up @@ -98,7 +98,7 @@ local function init_tbl()
---Updates the tbl for a given id
---@param id number #The id of a papis entry
function db.search:update(id)
local entry = db["data"]:__get({
local entry = db.data:__get({
where = { id = id }
})[1]
local display_strings = utils:format_display_strings(entry, results_format, false, true)
Expand Down
Loading

0 comments on commit 8e9b676

Please sign in to comment.