@@ -49,32 +49,58 @@ function trigger.activate_autocmds()
49
49
end ,
50
50
})
51
51
52
- -- check if we've moved outside of the context by diffing against the query boundary
52
+ -- hack: 'a' triggers both the InsertEnter and CursorMovedI event
53
+ -- However, the cursor_col hasn't changed. When we're on a trigger character,
54
+ -- this causes two show() calls, one with the trigger character and one without.
55
+ -- To prevent this, we ignore the first CursorMovedI event when 'a' is pressed
56
+ local ignore_next_cursor_moved = false
57
+ vim .api .nvim_set_keymap (' n' , ' a' , ' ' , {
58
+ callback = function ()
59
+ ignore_next_cursor_moved = true
60
+ return ' a'
61
+ end ,
62
+ expr = true ,
63
+ noremap = true ,
64
+ silent = true ,
65
+ })
66
+
53
67
vim .api .nvim_create_autocmd ({ ' CursorMovedI' , ' InsertEnter' }, {
54
68
callback = function (ev )
55
69
-- characters added so let textchanged handle it
56
70
if last_char ~= ' ' then return end
57
71
58
- local is_within_bounds = trigger .within_query_bounds (vim .api .nvim_win_get_cursor (0 ))
72
+ -- ignore CursorMovedI event when flag is enabled
73
+ if ev .event == ' CursorMovedI' and ignore_next_cursor_moved then
74
+ ignore_next_cursor_moved = false
75
+ return
76
+ end
59
77
60
78
local cursor_col = vim .api .nvim_win_get_cursor (0 )[2 ]
61
79
local char_under_cursor = vim .api .nvim_get_current_line ():sub (cursor_col , cursor_col )
62
80
local is_on_trigger = vim .tbl_contains (sources .get_trigger_characters (), char_under_cursor )
81
+ local is_on_trigger_for_show_on_insert = is_on_trigger
63
82
and not vim .tbl_contains (config .show_on_insert_blocked_trigger_characters , char_under_cursor )
64
83
local is_on_context_char = char_under_cursor :match (config .keyword_regex ) ~= nil
65
84
66
- if is_within_bounds then
85
+ -- check if we're still within the bounds of the query used for the context
86
+ if trigger .within_query_bounds (vim .api .nvim_win_get_cursor (0 )) then
67
87
trigger .show ()
88
+
89
+ -- check if we've entered insert mode on a trigger character
90
+ -- or if we've moved onto a trigger character
68
91
elseif
69
- -- check if we've gone 1 char behind the context and we're still on a context char
70
- (is_on_context_char and trigger .context ~= nil and cursor_col == trigger .context .bounds .start_col - 1 )
71
- -- or if we've moved onto a trigger character
92
+ (config .show_on_insert_on_trigger_character and is_on_trigger_for_show_on_insert and ev .event == ' InsertEnter' )
72
93
or (is_on_trigger and trigger .context ~= nil )
73
94
then
74
95
trigger .context = nil
75
- trigger .show ()
76
- elseif config .show_on_insert_on_trigger_character and is_on_trigger and ev .event == ' InsertEnter' then
77
96
trigger .show ({ trigger_character = char_under_cursor })
97
+
98
+ -- show if we currently have a context, and we've moved outside of it's bounds by 1 char
99
+ elseif is_on_context_char and trigger .context ~= nil and cursor_col == trigger .context .bounds .start_col - 1 then
100
+ trigger .context = nil
101
+ trigger .show ()
102
+
103
+ -- otherwise hide
78
104
else
79
105
trigger .hide ()
80
106
end
@@ -84,7 +110,7 @@ function trigger.activate_autocmds()
84
110
-- definitely leaving the context
85
111
vim .api .nvim_create_autocmd ({ ' InsertLeave' , ' BufLeave' }, { callback = trigger .hide })
86
112
87
- -- trigger InsertLeave autocommand when exiting insert mode with ctrl+c
113
+ -- manually hide when exiting insert mode with ctrl+c, since it doesn't trigger InsertLeave
88
114
local ctrl_c = vim .api .nvim_replace_termcodes (' <C-c>' , true , true , true )
89
115
vim .on_key (function (key )
90
116
if key == ctrl_c then
0 commit comments