diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..2759aa194 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +#editorconfig.org +root = true + +[*.lua] +trim_trailing_whitespace = false +indent_style = space +indent_size = 2 diff --git a/lua/cmp/init.lua b/lua/cmp/init.lua index 4aaf2fe6c..982621753 100644 --- a/lua/cmp/init.lua +++ b/lua/cmp/init.lua @@ -329,8 +329,6 @@ local on_insert_enter = function() cmp.core:on_change('InsertEnter') end end -autocmd.subscribe({ 'CmdlineEnter' }, async.debounce_next_tick(on_insert_enter)) -autocmd.subscribe({ 'InsertEnter' }, async.debounce_next_tick_by_keymap(on_insert_enter)) -- async.throttle is needed for performance. The mapping `:...` will fire `CmdlineChanged` for each character. local on_text_changed = function() @@ -338,24 +336,31 @@ local on_text_changed = function() cmp.core:on_change('TextChanged') end end + +-- If make this asynchronous, the completion menu will not close when the command output is displayed. +local on_insert_leave = function() + cmp.core:reset() + cmp.core.view:close() +end + +autocmd.subscribe('InsertEnter', async.debounce_next_tick_by_keymap(on_insert_enter)) autocmd.subscribe({ 'TextChangedI', 'TextChangedP' }, on_text_changed) -autocmd.subscribe('CmdlineChanged', async.debounce_next_tick(on_text_changed)) +autocmd.subscribe('InsertLeave', on_insert_leave) + +if not config.is_native_menu() then + autocmd.subscribe('CmdlineEnter', async.debounce_next_tick(on_insert_enter)) + autocmd.subscribe('CmdlineChanged', async.debounce_next_tick(on_text_changed)) + autocmd.subscribe('CmdlineLeave', on_insert_leave) +end autocmd.subscribe('CursorMovedI', function() if config.enabled() then cmp.core:on_moved() else - cmp.core:reset() - cmp.core.view:close() + on_insert_leave() end end) --- If make this asynchronous, the completion menu will not close when the command output is displayed. -autocmd.subscribe({ 'InsertLeave', 'CmdlineLeave' }, function() - cmp.core:reset() - cmp.core.view:close() -end) - cmp.event:on('complete_done', function(evt) if evt.entry then cmp.config.compare.recently_used:add_entry(evt.entry) diff --git a/lua/cmp/source.lua b/lua/cmp/source.lua index d733224da..321b99aac 100644 --- a/lua/cmp/source.lua +++ b/lua/cmp/source.lua @@ -295,7 +295,7 @@ source.complete = function(self, ctx, callback) completion_context = { triggerKind = types.lsp.CompletionTriggerKind.TriggerForIncompleteCompletions, } - elseif not vim.tbl_contains({ self.request_offset, self.offset }, offset) then + else completion_context = { triggerKind = types.lsp.CompletionTriggerKind.Invoked, } diff --git a/lua/cmp/utils/str.lua b/lua/cmp/utils/str.lua index 84b0177e0..dba0f5db6 100644 --- a/lua/cmp/utils/str.lua +++ b/lua/cmp/utils/str.lua @@ -122,6 +122,7 @@ str.get_word = function(text, stop_char, min_length) local c = string.byte(text, i, i) if #word < min_length then table.insert(word, string.char(c)) + has_alnum = has_alnum or char.is_alnum(c) elseif not INVALIDS[c] then add(c) has_alnum = has_alnum or char.is_alnum(c) diff --git a/lua/cmp/utils/str_spec.lua b/lua/cmp/utils/str_spec.lua index 1a218559f..9e4d54c58 100644 --- a/lua/cmp/utils/str_spec.lua +++ b/lua/cmp/utils/str_spec.lua @@ -10,6 +10,13 @@ describe('utils.str', function() assert.are.equal(str.get_word('"devDependencies": ${1},', string.byte('"')), '"devDependencies') assert.are.equal(str.get_word('#[cfg(test)]'), '#[cfg(test)]') assert.are.equal(str.get_word('import { GetStaticProps$1 } from "next";', nil, 9), 'import { GetStaticProps') + assert.are.equal(str.get_word('do {\n${1:statements}\n}while ($0)', nil, 0), 'do') + assert.are.equal(str.get_word('do {\n${1:statements}\n}while ($0)', nil, 1), 'do') + assert.are.equal(str.get_word('do {\n${1:statements}\n}while ($0)', nil, 2), 'do') + assert.are.equal(str.get_word('for (${1:init-statement}; ${2:condition}; ${3:inc-expression}) {\n$0\n}', nil, 0), 'for') + assert.are.equal(str.get_word('for (${1:init-statement}; ${2:condition}; ${3:inc-expression}) {\n$0\n}', nil, 1), 'for') + assert.are.equal(str.get_word('for (${1:init-statement}; ${2:condition}; ${3:inc-expression}) {\n$0\n}', nil, 2), 'for') + assert.are.equal(str.get_word('for (${1:init-statement}; ${2:condition}; ${3:inc-expression}) {\n$0\n}', nil, 3), 'for') end) it('remove_suffix', function()