Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: utils.str.get_word #1781

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#editorconfig.org
root = true

[*.lua]
trim_trailing_whitespace = false
indent_style = space
indent_size = 2
27 changes: 16 additions & 11 deletions lua/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -329,33 +329,38 @@ 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 `:<C-u>...<CR>` will fire `CmdlineChanged` for each character.
local on_text_changed = function()
if config.enabled() then
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)
Expand Down
2 changes: 1 addition & 1 deletion lua/cmp/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions lua/cmp/utils/str.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions lua/cmp/utils/str_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down