@@ -48,13 +48,10 @@ function autocomplete.setup()
48
48
end ,
49
49
})
50
50
51
- vim .api .nvim_create_autocmd (' CursorMovedI' , {
51
+ vim .api .nvim_create_autocmd ({ ' CursorMovedI' , ' WinScrolled ' , ' WinResized ' } , {
52
52
callback = function ()
53
53
if autocomplete .context == nil then return end
54
-
55
- local cursor_column = vim .api .nvim_win_get_cursor (0 )[2 ]
56
- autocomplete .win :update_position (' cursor' , autocomplete .context .bounds .start_col - cursor_column - 1 )
57
- autocomplete .event_targets .on_position_update ()
54
+ autocomplete .update_position (autocomplete .context )
58
55
end ,
59
56
})
60
57
65
62
66
63
function autocomplete .open_with_items (context , items )
67
64
autocomplete .items = items
68
- autocomplete .context = context
69
65
autocomplete .draw ()
70
66
71
67
autocomplete .win :open ()
72
- local cursor_column = vim . api . nvim_win_get_cursor ( 0 )[ 2 ]
73
- autocomplete .win : update_position ( ' cursor ' , autocomplete . context . bounds . start_col - cursor_column - 1 )
74
- autocomplete .event_targets . on_position_update ( )
68
+
69
+ autocomplete .context = context
70
+ autocomplete .update_position ( context )
75
71
76
72
-- todo: some logic to maintain the selection if the user moved the cursor?
77
73
vim .api .nvim_win_set_cursor (autocomplete .win :get_win (), { 1 , 0 })
78
74
autocomplete .event_targets .on_select (autocomplete .get_selected_item ())
79
75
end
80
76
81
- function autocomplete .listen_on_position_update (callback ) autocomplete .event_targets .on_position_update = callback end
82
-
83
77
function autocomplete .open ()
84
78
if autocomplete .win :is_open () then return end
85
79
autocomplete .win :open ()
@@ -92,6 +86,15 @@ function autocomplete.close()
92
86
end
93
87
function autocomplete .listen_on_close (callback ) autocomplete .event_targets .on_close = callback end
94
88
89
+ function autocomplete .update_position (context )
90
+ -- todo: should point to the window of the context?
91
+ local cursor_column = vim .api .nvim_win_get_cursor (0 )[2 ]
92
+ autocomplete .win :update_position (' cursor' , context .bounds .start_col - cursor_column - 1 )
93
+ autocomplete .event_targets .on_position_update ()
94
+ end
95
+
96
+ function autocomplete .listen_on_position_update (callback ) autocomplete .event_targets .on_position_update = callback end
97
+
95
98
---- ------ Selection ----------
96
99
97
100
function autocomplete .select_next ()
0 commit comments