@@ -25,11 +25,23 @@ M.ref_insert = function(prompt_bufnr)
25
25
local multi = get_multi (prompt_bufnr )
26
26
actions .close (prompt_bufnr )
27
27
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 " "
30
30
local ref_prefix = cite_format .ref_prefix or " "
31
31
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
33
45
34
46
if vim .tbl_isempty (multi ) then
35
47
local ref = ref_prefix .. action_state .get_selected_entry ().id .ref
@@ -41,7 +53,11 @@ M.ref_insert = function(prompt_bufnr)
41
53
end
42
54
string_to_insert = string_to_insert .. table.concat (refs , separator_str )
43
55
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
+
45
61
vim .api .nvim_put ({ string_to_insert }, " " , false , true )
46
62
end
47
63
0 commit comments