Fix #129 (experimental)

This commit is contained in:
hrsh7th
2021-09-01 20:11:02 +09:00
parent a8f62d2364
commit 98124321fe
2 changed files with 47 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
local debug = require('cmp.utils.debug') local debug = require('cmp.utils.debug')
local char = require('cmp.utils.char') local char = require('cmp.utils.char')
local str = require('cmp.utils.str') local pattern = require('cmp.utils.pattern')
local async = require('cmp.utils.async') local async = require('cmp.utils.async')
local keymap = require('cmp.utils.keymap') local keymap = require('cmp.utils.keymap')
local context = require('cmp.context') local context = require('cmp.context')
@@ -130,27 +130,47 @@ end
---Check auto-completion ---Check auto-completion
core.on_change = function(event) core.on_change = function(event)
local ctx = core.get_context({ reason = types.cmp.ContextReason.Auto }) core.autoindent(event, function()
local ctx = core.get_context({ reason = types.cmp.ContextReason.Auto })
-- Skip autocompletion when the item is selected manually. -- Skip autocompletion when the item is selected manually.
if ctx.pumvisible and not vim.tbl_isempty(vim.v.completed_item) then if ctx.pumvisible and not vim.tbl_isempty(vim.v.completed_item) then
return return
end end
debug.log(('ctx: `%s`'):format(ctx.cursor_before_line)) debug.log(('ctx: `%s`'):format(ctx.cursor_before_line))
if ctx:changed(ctx.prev_context) then if ctx:changed(ctx.prev_context) then
debug.log('changed') debug.log('changed')
core.menu:restore(ctx) core.menu:restore(ctx)
if vim.tbl_contains(config.get().completion.autocomplete or {}, event) then if vim.tbl_contains(config.get().completion.autocomplete or {}, event) then
core.complete(ctx) core.complete(ctx)
else else
core.filter.timeout = 50 core.filter.timeout = 50
core.filter() core.filter()
end
else
debug.log('unchanged')
end
end)
end
---Check autoindent
---@param event cmp.TriggerEvent
---@param callback function
core.autoindent = function(event, callback)
if event == types.cmp.TriggerEvent.TextChanged then
local ctx = context.new()
local prefix = pattern.matchstr('[^[:blank:]]\\+$', ctx.cursor_before_line)
if prefix then
for _, key in ipairs(vim.split(vim.bo.indentkeys, ',')) do
if vim.tbl_contains({ '=' .. prefix, '0=' .. prefix }, key) then
return keymap.feedkeys(keymap.t('<C-f>'), 'n', callback)
end
end
end end
else
debug.log('unchanged')
end end
callback()
end end
---Invoke completion ---Invoke completion

View File

@@ -17,5 +17,13 @@ pattern.offset = function(p, text)
return nil, nil return nil, nil
end end
pattern.matchstr = function(p, text)
local s, e = pattern.offset(p, text)
if s then
return string.sub(text, s, e)
end
return nil
end
return pattern return pattern