Skip to content

Commit 1c513b2

Browse files
author
jghauser
committed
feat(search): only insert ref_prefix + ref if inserting into existing citation
1 parent 43061e7 commit 1c513b2

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

lua/telescope/_extensions/papis/actions.lua

+20-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,23 @@ M.ref_insert = function(prompt_bufnr)
2525
local multi = get_multi(prompt_bufnr)
2626
actions.close(prompt_bufnr)
2727
local cite_format = config:get_cite_format()
28-
local start_str = cite_format.start_str
29-
local end_str = cite_format.end_str
28+
local start_str = cite_format.start_str or ""
29+
local end_str = cite_format.end_str or ""
3030
local ref_prefix = cite_format.ref_prefix or ""
3131
local separator_str = cite_format.separator_str
32-
local string_to_insert = start_str or ""
32+
local string_to_insert = ""
33+
34+
-- Get the current line and cursor position
35+
local current_line = vim.api.nvim_get_current_line()
36+
local cursor_pos = vim.api.nvim_win_get_cursor(0)[2] + 1
37+
38+
-- Check if the cursor is enclosed by start_str and end_str
39+
local enclosed = current_line:sub(1, cursor_pos - 1):find(start_str, 1, true) and
40+
current_line:sub(cursor_pos):find(end_str, 1, true)
41+
42+
if not enclosed then
43+
string_to_insert = start_str
44+
end
3345

3446
if vim.tbl_isempty(multi) then
3547
local ref = ref_prefix .. action_state.get_selected_entry().id.ref
@@ -41,7 +53,11 @@ M.ref_insert = function(prompt_bufnr)
4153
end
4254
string_to_insert = string_to_insert .. table.concat(refs, separator_str)
4355
end
44-
string_to_insert = string_to_insert .. (end_str or "")
56+
57+
if not enclosed then
58+
string_to_insert = string_to_insert .. end_str
59+
end
60+
4561
vim.api.nvim_put({ string_to_insert }, "", false, true)
4662
end
4763

0 commit comments

Comments
 (0)