324
324
MiniCompletion .completefunc_lsp = function (findstart , base )
325
325
-- Early return
326
326
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 {}
332
329
end
333
330
334
331
-- NOTE: having code for request inside this function enables its use
@@ -368,31 +365,24 @@ MiniCompletion.completefunc_lsp = function(findstart, base)
368
365
H .completion .lsp .cancel_fun = cancel_fun
369
366
370
367
-- 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 {}
376
370
else
377
371
if findstart == 1 then return H .get_completion_start () end
378
372
379
- local config = H .get_config ()
380
-
373
+ local process_items = H .get_config ().lsp_completion .process_items
381
374
local words = H .process_lsp_response (H .completion .lsp .result , function (response , client_id )
382
375
-- Response can be `CompletionList` with 'items' field or `CompletionItem[]`
383
376
local items = H .table_get (response , { ' items' }) or response
384
377
if type (items ) ~= ' table' then return {} end
385
- items = config . lsp_completion . process_items (items , base )
378
+ items = process_items (items , base )
386
379
return H .lsp_completion_response_items_to_complete_items (items , client_id )
387
380
end )
388
381
389
382
H .completion .lsp .status = ' done'
390
383
391
384
-- 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
396
386
397
387
-- Track from which source is current popup
398
388
H .completion .source = ' lsp'
@@ -556,12 +546,9 @@ H.create_autocommands = function(config)
556
546
au (' TextChangedP' , ' *' , H .on_text_changed_p , ' On TextChangedP' )
557
547
558
548
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' )
565
552
end
566
553
567
554
au (' ColorScheme' , ' *' , H .create_default_hl , ' Ensure proper colors' )
@@ -632,7 +619,7 @@ H.auto_info = function()
632
619
633
620
-- Defer execution because of textlock during `CompleteChanged` event
634
621
-- 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 )
636
623
637
624
-- Stop current LSP request that tries to get not current data
638
625
H .cancel_lsp ({ H .info })
@@ -724,22 +711,24 @@ H.trigger_lsp = function()
724
711
end
725
712
726
713
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 )
743
732
end
744
733
745
734
-- Stop actions ---------------------------------------------------------------
797
786
798
787
H .is_lsp_trigger = function (char , type )
799
788
local triggers
800
- local providers = {
801
- completion = ' completionProvider' ,
802
- signature = ' signatureHelpProvider' ,
803
- }
789
+ local providers = { completion = ' completionProvider' , signature = ' signatureHelpProvider' }
804
790
805
791
for _ , client in pairs (vim .lsp .buf_get_clients ()) do
806
792
triggers = H .table_get (client , { ' server_capabilities' , providers [type ], ' triggerCharacters' })
@@ -817,8 +803,7 @@ H.cancel_lsp = function(caches)
817
803
c .lsp .status = ' canceled'
818
804
end
819
805
820
- c .lsp .result = nil
821
- c .lsp .cancel_fun = nil
806
+ c .lsp .result , c .lsp .cancel_fun = nil , nil
822
807
end
823
808
end
824
809
@@ -947,11 +932,11 @@ H.show_info_window = function()
947
932
local opts = H .info_window_options ()
948
933
949
934
-- Defer execution because of textlock during `CompleteChanged` event
950
- vim .defer_fn (function ()
935
+ vim .schedule (function ()
951
936
-- Ensure that window doesn't open when it shouldn't be
952
937
if not (H .pumvisible () and vim .fn .mode () == ' i' ) then return end
953
938
H .open_action_window (H .info , opts )
954
- end , 0 )
939
+ end )
955
940
end
956
941
957
942
H .info_window_lines = function (info_id )
@@ -962,7 +947,7 @@ H.info_window_lines = function(info_id)
962
947
if not H .is_whitespace (text ) then
963
948
-- Use `<text></text>` to be properly processed by `stylize_markdown()`
964
949
local lines = { ' <text>' }
965
- vim .list_extend (lines , vim .split (text , ' \n ' , false ))
950
+ vim .list_extend (lines , vim .split (text , ' \n ' ))
966
951
table.insert (lines , ' </text>' )
967
952
return lines
968
953
end
@@ -1011,7 +996,7 @@ H.info_window_options = function()
1011
996
local win_config = H .get_config ().window .info
1012
997
1013
998
-- 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 )
1015
1000
local info_height , info_width = H .floating_dimensions (lines , win_config .height , win_config .width )
1016
1001
1017
1002
-- Compute position
@@ -1095,19 +1080,14 @@ H.show_signature_window = function()
1095
1080
1096
1081
-- Add `lines` to signature buffer. Use `wrap_at` to have proper width of
1097
1082
-- '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 })
1099
1085
1100
1086
-- Add highlighting of active parameter
1101
1087
for i , hl_range in ipairs (hl_ranges ) do
1102
1088
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 )
1111
1091
end
1112
1092
end
1113
1093
@@ -1170,7 +1150,7 @@ H.process_signature_response = function(response)
1170
1150
1171
1151
-- Computing active parameter only when parameter id is inside bounds is not
1172
1152
-- 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
1174
1154
-- those values to indicate that nothing needs to be highlighted.
1175
1155
-- Sources:
1176
1156
-- https://github.com/microsoft/pyright/pull/1876
@@ -1185,9 +1165,7 @@ H.process_signature_response = function(response)
1185
1165
if type (param_label ) == ' string' then
1186
1166
first , last = signature_label :find (vim .pesc (param_label ))
1187
1167
-- 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
1191
1169
elseif type (param_label ) == ' table' then
1192
1170
first , last = unpack (param_label )
1193
1171
end
@@ -1201,7 +1179,7 @@ end
1201
1179
1202
1180
H .signature_window_opts = function ()
1203
1181
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 )
1205
1183
local height , width = H .floating_dimensions (lines , win_config .height , win_config .width )
1206
1184
1207
1185
-- Compute position
0 commit comments