1
- local M = {}
1
+ local cmp = {}
2
2
3
- M .kind_icons = {
3
+ cmp .kind_icons = {
4
4
Text = ' ' ,
5
5
Method = ' ' ,
6
6
Function = ' ' ,
@@ -32,17 +32,17 @@ M.kind_icons = {
32
32
Operator = ' ' ,
33
33
TypeParameter = ' ' ,
34
34
}
35
- M .filtered_items = {}
35
+ cmp .filtered_items = {}
36
36
37
- M .fuzzy = require (' blink.fuzzy' ).fuzzy
38
- M .lsp = require (' blink.cmp.lsp' )
37
+ cmp .fuzzy = require (' blink.fuzzy' ).fuzzy
38
+ cmp .lsp = require (' blink.cmp.lsp' )
39
39
40
- M .accept = function (cmp_win )
40
+ cmp .accept = function (cmp_win )
41
41
local bufnr = vim .api .nvim_get_current_buf ()
42
- local current_line , start_col , end_col = M .get_query_to_replace (bufnr )
42
+ local current_line , start_col , end_col = cmp .get_query_to_replace (bufnr )
43
43
44
44
-- Get the item from the filtered items based on the cursorline position
45
- local item = M .filtered_items [vim .api .nvim_win_get_cursor (cmp_win .id )[1 ]]
45
+ local item = cmp .filtered_items [vim .api .nvim_win_get_cursor (cmp_win .id )[1 ]]
46
46
47
47
-- Apply text edit
48
48
vim .api .nvim_buf_set_text (bufnr , current_line , start_col , current_line , end_col , { item .word })
@@ -53,50 +53,53 @@ M.accept = function(cmp_win)
53
53
-- These are used for things like auto-imports
54
54
-- todo: check capabilities to know ahead of time
55
55
if item .additionalTextEdits ~= nil then
56
- M .apply_additional_text_edits (item .client_id , item )
56
+ cmp .apply_additional_text_edits (item .client_id , item )
57
57
else
58
- M .lsp .resolve (item , function (client_id , resolved_item ) M .apply_additional_text_edits (client_id , resolved_item ) end )
58
+ cmp .lsp .resolve (
59
+ item ,
60
+ function (client_id , resolved_item ) cmp .apply_additional_text_edits (client_id , resolved_item ) end
61
+ )
59
62
end
60
63
end
61
64
62
- M .select_next = function (cmp_win , doc_win )
65
+ cmp .select_next = function (cmp_win , doc_win )
63
66
if cmp_win .id == nil then return end
64
67
65
68
local current_line = vim .api .nvim_win_get_cursor (cmp_win .id )[1 ]
66
- local item_count = # M .filtered_items
69
+ local item_count = # cmp .filtered_items
67
70
local line_count = vim .api .nvim_buf_line_count (cmp_win .buf )
68
71
69
72
-- draw a new line if we're at the end and there's more items
70
73
-- todo: this is a hack while waiting for virtual scroll
71
74
if current_line == line_count and item_count > line_count then
72
- M .draw_item (cmp_win .buf , line_count + 1 , M .filtered_items [line_count + 1 ])
75
+ cmp .draw_item (cmp_win .buf , line_count + 1 , cmp .filtered_items [line_count + 1 ])
73
76
vim .api .nvim_win_set_cursor (cmp_win .id , { line_count + 1 , 0 })
74
77
-- otherwise just move the cursor, wrapping if at the bottom
75
78
else
76
79
local line = current_line == item_count and 1 or current_line + 1
77
80
vim .api .nvim_win_set_cursor (cmp_win .id , { line , 0 })
78
81
end
79
82
80
- M .update_doc (cmp_win , doc_win )
83
+ cmp .update_doc (cmp_win , doc_win )
81
84
end
82
85
83
86
-- todo: how to handle overflow to the bottom? should probably just do proper virtual scroll
84
- M .select_prev = function (cmp_win , doc_win )
87
+ cmp .select_prev = function (cmp_win , doc_win )
85
88
if cmp_win .id == nil then return end
86
89
87
90
local current_line = vim .api .nvim_win_get_cursor (cmp_win .id )[1 ]
88
91
local line_count = vim .api .nvim_buf_line_count (cmp_win .buf )
89
92
local line = current_line - 1 == 0 and line_count or current_line - 1
90
93
vim .api .nvim_win_set_cursor (cmp_win .id , { line , 0 })
91
94
92
- M .update_doc (cmp_win , doc_win )
95
+ cmp .update_doc (cmp_win , doc_win )
93
96
end
94
97
95
- M .update = function (cmp_win , doc_win , items , opts )
96
- local query = M .get_query ()
98
+ cmp .update = function (cmp_win , doc_win , items , opts )
99
+ local query = cmp .get_query ()
97
100
98
101
-- get the items based on the user's query
99
- local filtered_items = M .filter_items (query , items )
102
+ local filtered_items = cmp .filter_items (query , items )
100
103
101
104
-- guards for cases where we shouldn't show the completion window
102
105
local no_items = # filtered_items == 0
@@ -115,7 +118,7 @@ M.update = function(cmp_win, doc_win, items, opts)
115
118
vim .api .nvim_buf_set_option (cmp_win .buf , ' modified' , false )
116
119
117
120
for idx , item in ipairs (filtered_items ) do
118
- M .draw_item (cmp_win .buf , idx , item )
121
+ cmp .draw_item (cmp_win .buf , idx , item )
119
122
-- only draw until the window is full
120
123
if idx >= cmp_win .config .max_height then break end
121
124
end
@@ -124,23 +127,23 @@ M.update = function(cmp_win, doc_win, items, opts)
124
127
cmp_win :update ()
125
128
126
129
-- documentation
127
- M .update_doc (cmp_win , doc_win )
130
+ cmp .update_doc (cmp_win , doc_win )
128
131
129
- M .filtered_items = filtered_items
132
+ cmp .filtered_items = filtered_items
130
133
end
131
134
132
- function M .update_doc (cmp_win , doc_win )
135
+ function cmp .update_doc (cmp_win , doc_win )
133
136
-- completion window isn't open
134
137
if cmp_win .id == nil then return end
135
138
136
139
local current_line = vim .api .nvim_win_get_cursor (cmp_win .id )[1 ]
137
- local item = M .filtered_items [current_line ]
140
+ local item = cmp .filtered_items [current_line ]
138
141
if item == nil then
139
142
doc_win :close ()
140
143
return
141
144
end
142
145
143
- M .lsp .resolve (item , function (_ , resolved_item )
146
+ cmp .lsp .resolve (item , function (_ , resolved_item )
144
147
if resolved_item .detail == nil then
145
148
doc_win :close ()
146
149
return
@@ -158,10 +161,10 @@ function M.update_doc(cmp_win, doc_win)
158
161
end
159
162
160
163
---- ------ UTILS ------------
161
- M .draw_item = function (bufnr , idx , item )
164
+ cmp .draw_item = function (bufnr , idx , item )
162
165
-- get highlight
163
166
local kind_hl = ' CmpItemKind' .. item .kind
164
- local kind_icon = M .kind_icons [item .kind ] or M .kind_icons .Field
167
+ local kind_icon = cmp .kind_icons [item .kind ] or cmp .kind_icons .Field
165
168
local kind = item .kind
166
169
167
170
-- get line text
@@ -181,7 +184,7 @@ M.draw_item = function(bufnr, idx, item)
181
184
vim .api .nvim_buf_set_option (bufnr , ' modified' , false )
182
185
end
183
186
184
- M .filter_items = function (query , items )
187
+ cmp .filter_items = function (query , items )
185
188
if query == ' ' then return items end
186
189
187
190
-- convert to table of strings
@@ -192,15 +195,15 @@ M.filter_items = function(query, items)
192
195
193
196
-- perform fuzzy search
194
197
local filtered_items = {}
195
- local selected_indices = M .fuzzy (query , words )
198
+ local selected_indices = cmp .fuzzy (query , words )
196
199
for _ , selected_index in ipairs (selected_indices ) do
197
200
table.insert (filtered_items , items [selected_index + 1 ])
198
201
end
199
202
200
203
return filtered_items
201
204
end
202
205
203
- M .get_query = function ()
206
+ cmp .get_query = function ()
204
207
local bufnr = vim .api .nvim_get_current_buf ()
205
208
local current_line = vim .api .nvim_win_get_cursor (0 )[1 ] - 1
206
209
local current_col = vim .api .nvim_win_get_cursor (0 )[2 ] - 1
@@ -209,7 +212,7 @@ M.get_query = function()
209
212
return query
210
213
end
211
214
212
- M .get_query_to_replace = function (bufnr )
215
+ cmp .get_query_to_replace = function (bufnr )
213
216
local current_line = vim .api .nvim_win_get_cursor (0 )[1 ]
214
217
local current_col = vim .api .nvim_win_get_cursor (0 )[2 ]
215
218
local line = vim .api .nvim_buf_get_lines (bufnr , current_line - 1 , current_line , false )[1 ]
@@ -233,11 +236,13 @@ M.get_query_to_replace = function(bufnr)
233
236
return current_line - 1 , start_col - 1 , end_col
234
237
end
235
238
236
- M .apply_additional_text_edits = function (client_id , item ) M .apply_text_edits (client_id , item .additionalTextEdits or {}) end
239
+ cmp .apply_additional_text_edits = function (client_id , item )
240
+ cmp .apply_text_edits (client_id , item .additionalTextEdits or {})
241
+ end
237
242
238
- M .apply_text_edits = function (client_id , edits )
243
+ cmp .apply_text_edits = function (client_id , edits )
239
244
local offset_encoding = vim .lsp .get_client_by_id (client_id ).offset_encoding
240
245
vim .lsp .util .apply_text_edits (edits , vim .api .nvim_get_current_buf (), offset_encoding )
241
246
end
242
247
243
- return M
248
+ return cmp
0 commit comments