Skip to content

Commit 5e0c9c7

Browse files
author
jghauser
committed
fix: add missing modules (oops)
1 parent 728e017 commit 5e0c9c7

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

lua/papis/at-cursor/init.lua

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
--
2+
-- PAPIS | AT-CURSOR
3+
--
4+
--
5+
-- Various functionalities when the cursor is over a citation reference.
6+
--
7+
8+
local NuiPopup = require("nui.popup")
9+
local nuiAutocmd = require("nui.utils.autocmd")
10+
local nuiEvent = require("nui.utils.autocmd").event
11+
12+
local fn = vim.fn
13+
14+
local log = require("papis.log")
15+
local config = require("papis.config")
16+
local popup_format = config["at-cursor"]["popup_format"]
17+
local utils = require("papis.utils")
18+
local commands = require("papis.commands")
19+
local keymaps = require("papis.keymaps")
20+
local db = require("papis.sqlite-wrapper")
21+
if not db then
22+
return nil
23+
end
24+
local hover_required_db_keys = utils:get_required_db_keys({ popup_format })
25+
26+
---Tries to identify the ref under cursor
27+
---@return string|nil #Nil if nothing is found, otherwise is the identified ref
28+
local function get_ref_under_cursor()
29+
-- get the word under the cursor
30+
local ref = fn.expand("<cWORD>")
31+
local filetype = vim.bo.filetype
32+
log.debug("The filetype is: " .. filetype)
33+
local cite_format = utils.get_cite_format(filetype)
34+
if type(cite_format) == "table" then
35+
cite_format = cite_format[2]
36+
end
37+
log.debug("The cite_format is: " .. cite_format)
38+
local _, prefix_end = string.find(cite_format, "%%s")
39+
prefix_end = prefix_end - 2
40+
local cite_format_prefix = string.sub(cite_format, 1, prefix_end)
41+
local _, ref_start = string.find(ref, cite_format_prefix)
42+
-- if we found the cite_format prefix in the string, we need to strip it
43+
if ref_start then
44+
ref_start = ref_start + 1
45+
ref = string.sub(ref, ref_start)
46+
end
47+
-- remove all punctuation characters at the beginning and end of string
48+
ref = ref:gsub("^[%p]*(.-)[%p]*$", "%1")
49+
50+
return ref
51+
end
52+
53+
---Runs function if there is a valid ref under cursor which exists in the database
54+
---@param fun function #The function to be run with the papis_id
55+
---@param self? table #Self argument to be passed to fun
56+
---@param type? string #Type argument to be passed to fun
57+
local function if_ref_valid_run_fun(fun, self, type)
58+
local ref = get_ref_under_cursor()
59+
local entry = db.data:get({ ref = ref }, { "papis_id" })
60+
if not vim.tbl_isempty(entry) then
61+
local papis_id = entry[1]["papis_id"]
62+
if self then
63+
fun(self, papis_id, type)
64+
else
65+
fun(papis_id, type)
66+
end
67+
else
68+
log.info(string.format("No entry in database corresponds to '%s'", ref))
69+
end
70+
end
71+
72+
---Creates a popup with information regarding the entry specified by `ref`
73+
---@param papis_id string #The `papis_id` of the entry
74+
local function create_hover_popup(papis_id)
75+
local entry = db.data:get({ papis_id = papis_id }, hover_required_db_keys)[1]
76+
local clean_popup_format = utils.do_clean_format_tbl(popup_format, entry)
77+
local popup_lines, width = utils.make_nui_lines(clean_popup_format, entry)
78+
79+
local popup = NuiPopup({
80+
position = 1,
81+
size = {
82+
width = width,
83+
height = #popup_lines,
84+
},
85+
relative = "cursor",
86+
border = {
87+
style = "single",
88+
},
89+
})
90+
91+
local bufnr = vim.api.nvim_get_current_buf()
92+
nuiAutocmd.buf.define(bufnr, { nuiEvent.BufLeave, nuiEvent.CursorMoved, nuiEvent.BufWinLeave }, function()
93+
popup:unmount()
94+
end, { once = true })
95+
96+
-- mount/open the component
97+
popup:mount()
98+
99+
for line_nr, line in ipairs(popup_lines) do
100+
line:render(popup.bufnr, -1, line_nr)
101+
end
102+
end
103+
104+
---@class PapisSubcommand
105+
local module_subcommands = {
106+
["at-cursor"] = {
107+
impl = function(args, _)
108+
if args[1] == "open-file" then
109+
if_ref_valid_run_fun(utils.do_open_attached_files, utils)
110+
elseif args[1] == "open-note" then
111+
if_ref_valid_run_fun(utils.do_open_text_file, utils, "note")
112+
elseif args[1] == "edit" then
113+
if_ref_valid_run_fun(utils.do_open_text_file, utils, "info")
114+
elseif args[1] == "show-popup" then
115+
if_ref_valid_run_fun(create_hover_popup)
116+
end
117+
end,
118+
complete = function(subcmd_arg_lead)
119+
local reload_args = {
120+
"open-file",
121+
"open-note",
122+
"edit",
123+
"show-popup",
124+
}
125+
return vim.iter(reload_args)
126+
:filter(function(install_arg)
127+
return install_arg:find(subcmd_arg_lead) ~= nil
128+
end)
129+
:totable()
130+
end,
131+
}
132+
}
133+
134+
---@class PapisKeymaps
135+
local module_keymaps = {
136+
open_file = {
137+
mode = "n",
138+
lhs = "<leader>pof",
139+
rhs = function()
140+
vim.cmd("Papis at-cursor open-file")
141+
end,
142+
opts = { desc = "Papis: open file under cursor" },
143+
},
144+
edit_entry = {
145+
mode = "n",
146+
lhs = "<leader>pe",
147+
rhs = function()
148+
vim.cmd("Papis at-cursor edit")
149+
end,
150+
opts = { desc = "Papis: edit entry under cursor" },
151+
},
152+
open_note = {
153+
mode = "n",
154+
lhs = "<leader>pon",
155+
rhs = function()
156+
vim.cmd("Papis at-cursor open-note")
157+
end,
158+
opts = { desc = "Papis: open note under cursor" },
159+
},
160+
show_popup = {
161+
mode = "n",
162+
lhs = "<leader>pi",
163+
rhs = function()
164+
vim.cmd("Papis at-cursor show-popup")
165+
end,
166+
opts = { desc = "Papis: show entry info popup" },
167+
},
168+
}
169+
170+
local M = {}
171+
172+
function M.setup()
173+
log.debug("Setting up at-cursor")
174+
commands:add_commands(module_subcommands)
175+
keymaps:add_keymaps(module_keymaps)
176+
end
177+
178+
return M

lua/papis/debug/init.lua

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--
2+
-- PAPIS | DEBUG
3+
--
4+
--
5+
-- Debugging module.
6+
--
7+
8+
local log = require("papis.log")
9+
local commands = require("papis.commands")
10+
11+
---@class PapisSubcommand
12+
local module_subcommands = {
13+
debug = {
14+
impl = function(args, _)
15+
if args[1] == "start-watchers" then
16+
require("papis.fs-watcher").start()
17+
elseif args[1] == "stop-watchers" then
18+
require("papis.fs-watcher").stop()
19+
elseif args[1] == "info" then
20+
require("papis.log").get_path()
21+
end
22+
end,
23+
complete = function(subcmd_arg_lead)
24+
local reload_args = {
25+
"start-watchers",
26+
"stop-watchers",
27+
"info",
28+
}
29+
return vim.iter(reload_args)
30+
:filter(function(install_arg)
31+
return install_arg:find(subcmd_arg_lead) ~= nil
32+
end)
33+
:totable()
34+
end,
35+
}
36+
}
37+
38+
local M = {}
39+
40+
function M.setup()
41+
log.debug("Setting up debug module")
42+
commands:add_commands(module_subcommands)
43+
end
44+
45+
return M

0 commit comments

Comments
 (0)