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