refactor
This commit is contained in:
@@ -47,7 +47,7 @@ config.enabled = function()
|
||||
if type(enabled) == 'function' then
|
||||
enabled = enabled()
|
||||
end
|
||||
return enabled and string.sub(vim.api.nvim_get_mode().mode, 1, 1) == 'i'
|
||||
return enabled and misc.is_suitable_mode()
|
||||
end
|
||||
|
||||
---Return source config
|
||||
|
||||
@@ -81,10 +81,10 @@ return function()
|
||||
event = {},
|
||||
|
||||
mapping = {
|
||||
['<Down>'] = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }),
|
||||
['<Up>'] = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Select }),
|
||||
['<C-n>'] = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }),
|
||||
['<C-p>'] = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }),
|
||||
['<Down>'] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }), { 'i' }),
|
||||
['<Up>'] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Select }), { 'i' }),
|
||||
['<C-n>'] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i' }),
|
||||
['<C-p>'] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i' }),
|
||||
['<C-c>'] = function(fallback)
|
||||
require('cmp').close()
|
||||
fallback()
|
||||
|
||||
@@ -12,7 +12,6 @@ local cache = require('cmp.utils.cache')
|
||||
---@field public time number
|
||||
---@field public mode string
|
||||
---@field public bufnr number
|
||||
---@field public virtcol number
|
||||
---@field public cursor vim.Position|lsp.Position
|
||||
---@field public cursor_line string
|
||||
---@field public cursor_after_line string
|
||||
@@ -47,10 +46,10 @@ context.new = function(prev_context, option)
|
||||
self.time = vim.loop.now()
|
||||
self.mode = vim.api.nvim_get_mode().mode
|
||||
self.bufnr = vim.api.nvim_get_current_buf()
|
||||
self.cursor_line = vim.api.nvim_get_current_line()
|
||||
self.virtcol = vim.fn.virtcol('.')
|
||||
self.cursor = {}
|
||||
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
self.cursor_line = vim.api.nvim_get_current_line()
|
||||
self.cursor = {}
|
||||
self.cursor.row = cursor[1]
|
||||
self.cursor.col = cursor[2] + 1
|
||||
self.cursor.line = self.cursor.row - 1
|
||||
|
||||
@@ -227,7 +227,7 @@ end
|
||||
---Invoke completion
|
||||
---@param ctx cmp.Context
|
||||
core.complete = function(self, ctx)
|
||||
if not misc.is_insert_mode() then
|
||||
if not misc.is_suitable_mode() then
|
||||
return
|
||||
end
|
||||
self:set_context(ctx)
|
||||
@@ -259,7 +259,7 @@ end
|
||||
---Update completion menu
|
||||
core.filter = async.throttle(
|
||||
vim.schedule_wrap(function(self)
|
||||
if not misc.is_insert_mode() then
|
||||
if not misc.is_suitable_mode() then
|
||||
return
|
||||
end
|
||||
local ctx = self:get_context()
|
||||
|
||||
@@ -346,9 +346,9 @@ end
|
||||
entry.match = function(self, input)
|
||||
return self.match_cache:ensure(input, function()
|
||||
local score, matches, _
|
||||
score, matches = matcher.match(input, self:get_filter_text(), { self:get_word() })
|
||||
score, matches = matcher.match(input, self:get_filter_text(), { self:get_word(), self:get_completion_item().label })
|
||||
if self:get_filter_text() ~= self:get_completion_item().label then
|
||||
_, matches = matcher.match(input, self:get_completion_item().label)
|
||||
_, matches = matcher.match(input, self:get_completion_item().label, { self:get_word() })
|
||||
end
|
||||
return { score = score, matches = matches }
|
||||
end)
|
||||
|
||||
@@ -24,8 +24,13 @@ end
|
||||
|
||||
---Return current mode is insert-mode or not.
|
||||
---@return boolean
|
||||
misc.is_insert_mode = function()
|
||||
return string.sub(vim.api.nvim_get_mode().mode, 1, 1) == 'i'
|
||||
misc.is_suitable_mode = function()
|
||||
local mode = vim.api.nvim_get_mode().mode
|
||||
return vim.tbl_contains({
|
||||
'i',
|
||||
'ic',
|
||||
'ix',
|
||||
}, mode)
|
||||
end
|
||||
|
||||
---Merge two tables recursively
|
||||
|
||||
@@ -121,7 +121,7 @@ custom_entries_view.open = function(self, offset, entries)
|
||||
width = width + self.column_width.menu + 1
|
||||
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
local pos = vim.fn.screenpos('.', cursor[1], cursor[2] + 1)
|
||||
local pos = vim.fn.screenpos(0, cursor[1], cursor[2] + 1)
|
||||
local height = vim.api.nvim_get_option('pumheight')
|
||||
height = height == 0 and #self.entries or height
|
||||
height = math.min(height, #self.entries)
|
||||
|
||||
@@ -3,6 +3,7 @@ local autocmd = require('cmp.utils.autocmd')
|
||||
local keymap = require('cmp.utils.keymap')
|
||||
local types = require('cmp.types')
|
||||
local config = require('cmp.config')
|
||||
local misc = require('cmp.utils.misc')
|
||||
|
||||
---@class cmp.NativeEntriesView
|
||||
---@field private offset number
|
||||
@@ -69,7 +70,7 @@ native_entries_view.open = function(self, offset, entries)
|
||||
end
|
||||
|
||||
native_entries_view.close = function(self)
|
||||
if string.sub(vim.api.nvim_get_mode().mode, 1, 1) == 'i' then
|
||||
if misc.is_suitable_mode() then
|
||||
vim.fn.complete(1, {})
|
||||
end
|
||||
self.offset = -1
|
||||
@@ -79,7 +80,7 @@ native_entries_view.close = function(self)
|
||||
end
|
||||
|
||||
native_entries_view.abort = function(_)
|
||||
if string.sub(vim.api.nvim_get_mode().mode, 1, 1) == 'i' then
|
||||
if misc.is_suitable_mode() then
|
||||
vim.api.nvim_select_popupmenu_item(-1, true, true, {})
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user