From efb5e6b23bd51a8804a11b1775a85a10e0594083 Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Tue, 8 Oct 2024 15:36:36 -0400 Subject: [PATCH] fix: context not clearing on trigger character, path regexes closes #16 --- lua/blink/cmp/sources/path/init.lua | 3 +++ lua/blink/cmp/sources/path/lib.lua | 5 ++++- lua/blink/cmp/trigger/completion.lua | 10 +++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lua/blink/cmp/sources/path/init.lua b/lua/blink/cmp/sources/path/init.lua index ec8fb00d..16607a28 100644 --- a/lua/blink/cmp/sources/path/init.lua +++ b/lua/blink/cmp/sources/path/init.lua @@ -29,6 +29,9 @@ end function path:get_trigger_characters() return { '/', '.' } end function path:get_completions(context, callback) + -- we use libuv, but the rest of the library expects to be synchronous + callback = vim.schedule_wrap(callback) + local lib = require('blink.cmp.sources.path.lib') local dirname = lib.dirname(PATH_REGEX, self.opts.get_cwd, context) diff --git a/lua/blink/cmp/sources/path/lib.lua b/lua/blink/cmp/sources/path/lib.lua index b10f9c5c..f8c5afcd 100644 --- a/lua/blink/cmp/sources/path/lib.lua +++ b/lua/blink/cmp/sources/path/lib.lua @@ -4,7 +4,10 @@ local lib = {} --- @param get_cwd fun(context: blink.cmp.Context): string --- @param context blink.cmp.Context function lib.dirname(path_regex, get_cwd, context) - local line_before_cursor = context.line:sub(1, context.cursor[2]) + -- HACK: move this :sub logic into the context? + -- it's not obvious that you need to avoid going back a char if the start_col == end_col + local line_before_cursor = + context.line:sub(1, context.bounds.start_col - (context.bounds.start_col ~= context.bounds.end_col and 1 or 0)) local s = path_regex:match_str(line_before_cursor) if not s then return nil end diff --git a/lua/blink/cmp/trigger/completion.lua b/lua/blink/cmp/trigger/completion.lua index 2f416ec9..e962e76e 100644 --- a/lua/blink/cmp/trigger/completion.lua +++ b/lua/blink/cmp/trigger/completion.lua @@ -63,10 +63,14 @@ function trigger.activate_autocmds() and not vim.tbl_contains(config.show_on_insert_blocked_trigger_characters, char_under_cursor) local is_on_context_char = char_under_cursor:match(config.keyword_regex) ~= nil - if is_within_bounds or (is_on_trigger and trigger.context ~= nil) then + if is_within_bounds then trigger.show() - -- check if we've gone 1 char behind the context and we're still on a context char - elseif is_on_context_char and trigger.context ~= nil and cursor_col == trigger.context.bounds.start_col - 1 then + elseif + -- check if we've gone 1 char behind the context and we're still on a context char + (is_on_context_char and trigger.context ~= nil and cursor_col == trigger.context.bounds.start_col - 1) + -- or if we've moved onto a trigger character + or (is_on_trigger and trigger.context ~= nil) + then trigger.context = nil trigger.show() elseif config.show_on_insert_on_trigger_character and is_on_trigger and ev.event == 'InsertEnter' then