Skip to content

Commit 7c2339f

Browse files
committed
(mini.completion) Make cosmetic refactor.
1 parent d1413e1 commit 7c2339f

File tree

1 file changed

+42
-64
lines changed

1 file changed

+42
-64
lines changed

lua/mini/completion.lua

+42-64
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,8 @@ end
324324
MiniCompletion.completefunc_lsp = function(findstart, base)
325325
-- Early return
326326
if not H.has_lsp_clients('completionProvider') or H.completion.lsp.status == 'sent' then
327-
if findstart == 1 then
328-
return -3
329-
else
330-
return {}
331-
end
327+
if findstart == 1 then return -3 end
328+
return {}
332329
end
333330

334331
-- NOTE: having code for request inside this function enables its use
@@ -368,31 +365,24 @@ MiniCompletion.completefunc_lsp = function(findstart, base)
368365
H.completion.lsp.cancel_fun = cancel_fun
369366

370367
-- End completion and wait for LSP callback
371-
if findstart == 1 then
372-
return -3
373-
else
374-
return {}
375-
end
368+
if findstart == 1 then return -3 end
369+
return {}
376370
else
377371
if findstart == 1 then return H.get_completion_start() end
378372

379-
local config = H.get_config()
380-
373+
local process_items = H.get_config().lsp_completion.process_items
381374
local words = H.process_lsp_response(H.completion.lsp.result, function(response, client_id)
382375
-- Response can be `CompletionList` with 'items' field or `CompletionItem[]`
383376
local items = H.table_get(response, { 'items' }) or response
384377
if type(items) ~= 'table' then return {} end
385-
items = config.lsp_completion.process_items(items, base)
378+
items = process_items(items, base)
386379
return H.lsp_completion_response_items_to_complete_items(items, client_id)
387380
end)
388381

389382
H.completion.lsp.status = 'done'
390383

391384
-- Maybe trigger fallback action
392-
if vim.tbl_isempty(words) and H.completion.fallback then
393-
H.trigger_fallback()
394-
return
395-
end
385+
if vim.tbl_isempty(words) and H.completion.fallback then return H.trigger_fallback() end
396386

397387
-- Track from which source is current popup
398388
H.completion.source = 'lsp'
@@ -556,12 +546,9 @@ H.create_autocommands = function(config)
556546
au('TextChangedP', '*', H.on_text_changed_p, 'On TextChangedP')
557547

558548
if config.lsp_completion.auto_setup then
559-
au(
560-
'BufEnter',
561-
'*',
562-
function() vim.bo[config.lsp_completion.source_func] = 'v:lua.MiniCompletion.completefunc_lsp' end,
563-
'Set completion function'
564-
)
549+
local source_func = config.lsp_completion.source_func
550+
local callback = function() vim.bo[source_func] = 'v:lua.MiniCompletion.completefunc_lsp' end
551+
au('BufEnter', '*', callback, 'Set completion function')
565552
end
566553

567554
au('ColorScheme', '*', H.create_default_hl, 'Ensure proper colors')
@@ -632,7 +619,7 @@ H.auto_info = function()
632619

633620
-- Defer execution because of textlock during `CompleteChanged` event
634621
-- Don't stop timer when closing info window because it is needed
635-
vim.defer_fn(function() H.close_action_window(H.info, true) end, 0)
622+
vim.schedule(function() H.close_action_window(H.info, true) end)
636623

637624
-- Stop current LSP request that tries to get not current data
638625
H.cancel_lsp({ H.info })
@@ -724,22 +711,24 @@ H.trigger_lsp = function()
724711
end
725712

726713
H.trigger_fallback = function()
727-
local no_popup = H.completion.force or (not H.pumvisible())
728-
if no_popup and vim.fn.mode() == 'i' then
729-
-- Track from which source is current popup
730-
H.completion.source = 'fallback'
731-
local config = H.get_config()
732-
if type(config.fallback_action) == 'string' then
733-
-- Having `<C-g><C-g>` also (for some mysterious reason) helps to avoid
734-
-- some weird behavior. For example, if `keys = '<C-x><C-l>'` then Neovim
735-
-- starts new line when there is no suggestions.
736-
local keys = string.format('<C-g><C-g>%s', config.fallback_action)
737-
local trigger_keys = vim.api.nvim_replace_termcodes(keys, true, false, true)
738-
vim.api.nvim_feedkeys(trigger_keys, 'n', false)
739-
else
740-
config.fallback_action()
741-
end
742-
end
714+
-- Fallback only in Insert mode when no popup is visible
715+
local has_popup = H.pumvisible() and not H.completion.force
716+
if has_popup or vim.fn.mode() ~= 'i' then return end
717+
718+
-- Track from which source is current popup
719+
H.completion.source = 'fallback'
720+
721+
-- Execute fallback action
722+
local fallback_action = H.get_config().fallback_action
723+
if vim.is_callable(fallback_action) then return fallback_action() end
724+
if type(fallback_action) ~= 'string' then return end
725+
726+
-- Having `<C-g><C-g>` also (for some mysterious reason) helps to avoid
727+
-- some weird behavior. For example, if `keys = '<C-x><C-l>'` then Neovim
728+
-- starts new line when there is no suggestions.
729+
local keys = string.format('<C-g><C-g>%s', fallback_action)
730+
local trigger_keys = vim.api.nvim_replace_termcodes(keys, true, false, true)
731+
vim.api.nvim_feedkeys(trigger_keys, 'n', false)
743732
end
744733

745734
-- Stop actions ---------------------------------------------------------------
@@ -797,10 +786,7 @@ end
797786

798787
H.is_lsp_trigger = function(char, type)
799788
local triggers
800-
local providers = {
801-
completion = 'completionProvider',
802-
signature = 'signatureHelpProvider',
803-
}
789+
local providers = { completion = 'completionProvider', signature = 'signatureHelpProvider' }
804790

805791
for _, client in pairs(vim.lsp.buf_get_clients()) do
806792
triggers = H.table_get(client, { 'server_capabilities', providers[type], 'triggerCharacters' })
@@ -817,8 +803,7 @@ H.cancel_lsp = function(caches)
817803
c.lsp.status = 'canceled'
818804
end
819805

820-
c.lsp.result = nil
821-
c.lsp.cancel_fun = nil
806+
c.lsp.result, c.lsp.cancel_fun = nil, nil
822807
end
823808
end
824809

@@ -947,11 +932,11 @@ H.show_info_window = function()
947932
local opts = H.info_window_options()
948933

949934
-- Defer execution because of textlock during `CompleteChanged` event
950-
vim.defer_fn(function()
935+
vim.schedule(function()
951936
-- Ensure that window doesn't open when it shouldn't be
952937
if not (H.pumvisible() and vim.fn.mode() == 'i') then return end
953938
H.open_action_window(H.info, opts)
954-
end, 0)
939+
end)
955940
end
956941

957942
H.info_window_lines = function(info_id)
@@ -962,7 +947,7 @@ H.info_window_lines = function(info_id)
962947
if not H.is_whitespace(text) then
963948
-- Use `<text></text>` to be properly processed by `stylize_markdown()`
964949
local lines = { '<text>' }
965-
vim.list_extend(lines, vim.split(text, '\n', false))
950+
vim.list_extend(lines, vim.split(text, '\n'))
966951
table.insert(lines, '</text>')
967952
return lines
968953
end
@@ -1011,7 +996,7 @@ H.info_window_options = function()
1011996
local win_config = H.get_config().window.info
1012997

1013998
-- Compute dimensions based on lines to be displayed
1014-
local lines = vim.api.nvim_buf_get_lines(H.info.bufnr, 0, -1, {})
999+
local lines = vim.api.nvim_buf_get_lines(H.info.bufnr, 0, -1, false)
10151000
local info_height, info_width = H.floating_dimensions(lines, win_config.height, win_config.width)
10161001

10171002
-- Compute position
@@ -1095,19 +1080,14 @@ H.show_signature_window = function()
10951080

10961081
-- Add `lines` to signature buffer. Use `wrap_at` to have proper width of
10971082
-- 'non-UTF8' section separators.
1098-
vim.lsp.util.stylize_markdown(H.signature.bufnr, lines, { wrap_at = H.get_config().window.signature.width })
1083+
local buf_id = H.signature.bufnr
1084+
vim.lsp.util.stylize_markdown(buf_id, lines, { wrap_at = H.get_config().window.signature.width })
10991085

11001086
-- Add highlighting of active parameter
11011087
for i, hl_range in ipairs(hl_ranges) do
11021088
if not vim.tbl_isempty(hl_range) and hl_range.first and hl_range.last then
1103-
vim.api.nvim_buf_add_highlight(
1104-
H.signature.bufnr,
1105-
H.ns_id,
1106-
'MiniCompletionActiveParameter',
1107-
i - 1,
1108-
hl_range.first,
1109-
hl_range.last
1110-
)
1089+
local first, last = hl_range.first, hl_range.last
1090+
vim.api.nvim_buf_add_highlight(buf_id, H.ns_id, 'MiniCompletionActiveParameter', i - 1, first, last)
11111091
end
11121092
end
11131093

@@ -1170,7 +1150,7 @@ H.process_signature_response = function(response)
11701150

11711151
-- Computing active parameter only when parameter id is inside bounds is not
11721152
-- strictly based on specification, as currently (v3.16) it says to treat
1173-
-- out-of-bounds value as first parameter. However, some clients seems to use
1153+
-- out-of-bounds value as first parameter. However, some clients seem to use
11741154
-- those values to indicate that nothing needs to be highlighted.
11751155
-- Sources:
11761156
-- https://github.com/microsoft/pyright/pull/1876
@@ -1185,9 +1165,7 @@ H.process_signature_response = function(response)
11851165
if type(param_label) == 'string' then
11861166
first, last = signature_label:find(vim.pesc(param_label))
11871167
-- Make zero-indexed and end-exclusive
1188-
if first then
1189-
first, last = first - 1, last
1190-
end
1168+
if first then first = first - 1 end
11911169
elseif type(param_label) == 'table' then
11921170
first, last = unpack(param_label)
11931171
end
@@ -1201,7 +1179,7 @@ end
12011179

12021180
H.signature_window_opts = function()
12031181
local win_config = H.get_config().window.signature
1204-
local lines = vim.api.nvim_buf_get_lines(H.signature.bufnr, 0, -1, {})
1182+
local lines = vim.api.nvim_buf_get_lines(H.signature.bufnr, 0, -1, false)
12051183
local height, width = H.floating_dimensions(lines, win_config.height, win_config.width)
12061184

12071185
-- Compute position

0 commit comments

Comments
 (0)