From 2f2794decdc5cb80f254199b5f697fcd2bd931e7 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Mon, 11 Oct 2021 16:29:42 +0900 Subject: [PATCH] refactor --- lua/cmp/config.lua | 2 +- lua/cmp/config/default.lua | 8 ++++---- lua/cmp/context.lua | 7 +++---- lua/cmp/core.lua | 4 ++-- lua/cmp/entry.lua | 4 ++-- lua/cmp/utils/misc.lua | 9 +++++++-- lua/cmp/view/custom_entries_view.lua | 2 +- lua/cmp/view/native_entries_view.lua | 5 +++-- 8 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lua/cmp/config.lua b/lua/cmp/config.lua index ac54ea0..a701016 100644 --- a/lua/cmp/config.lua +++ b/lua/cmp/config.lua @@ -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 diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index 111b33d..5b294e8 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -81,10 +81,10 @@ return function() event = {}, mapping = { - [''] = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }), - [''] = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Select }), - [''] = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }), - [''] = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }), + [''] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }), { 'i' }), + [''] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Select }), { 'i' }), + [''] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i' }), + [''] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i' }), [''] = function(fallback) require('cmp').close() fallback() diff --git a/lua/cmp/context.lua b/lua/cmp/context.lua index bd18565..46ce0ff 100644 --- a/lua/cmp/context.lua +++ b/lua/cmp/context.lua @@ -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 diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index 49960a6..6003c9e 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -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() diff --git a/lua/cmp/entry.lua b/lua/cmp/entry.lua index 2050d87..78085df 100644 --- a/lua/cmp/entry.lua +++ b/lua/cmp/entry.lua @@ -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) diff --git a/lua/cmp/utils/misc.lua b/lua/cmp/utils/misc.lua index 8378b03..66feaac 100644 --- a/lua/cmp/utils/misc.lua +++ b/lua/cmp/utils/misc.lua @@ -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 diff --git a/lua/cmp/view/custom_entries_view.lua b/lua/cmp/view/custom_entries_view.lua index fa1c8a7..d5f70b4 100644 --- a/lua/cmp/view/custom_entries_view.lua +++ b/lua/cmp/view/custom_entries_view.lua @@ -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) diff --git a/lua/cmp/view/native_entries_view.lua b/lua/cmp/view/native_entries_view.lua index 48837f8..48017ed 100644 --- a/lua/cmp/view/native_entries_view.lua +++ b/lua/cmp/view/native_entries_view.lua @@ -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