Skip to content

Commit

Permalink
fix: use correct regex for filenames
Browse files Browse the repository at this point in the history
Closes #761
  • Loading branch information
Saghen committed Dec 31, 2024
1 parent 98fded2 commit 8df826f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
6 changes: 2 additions & 4 deletions lua/blink/cmp/sources/path/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
-- credit to https://github.com/hrsh7th/cmp-path for the original implementation
-- and https://codeberg.org/FelipeLema/cmp-async-path for the async implementation

local regex = require('blink.cmp.sources.path.regex')
local path = {}
local NAME_REGEX = '\\%([^/\\\\:\\*?<>\'"`\\|]\\)'
local PATH_REGEX =
assert(vim.regex(([[\%(\%(/PAT*[^/\\\\:\\*?<>\'"`\\| .~]\)\|\%(/\.\.\)\)*/\zePAT*$]]):gsub('PAT', NAME_REGEX)))

function path.new(opts)
local self = setmetatable({}, { __index = path })
Expand Down Expand Up @@ -34,7 +32,7 @@ function path:get_completions(context, callback)

local lib = require('blink.cmp.sources.path.lib')

local dirname = lib.dirname(PATH_REGEX, self.opts.get_cwd, context)
local dirname = lib.dirname(self.opts.get_cwd, context)
if not dirname then return callback({ is_incomplete_forward = false, is_incomplete_backward = false, items = {} }) end

local include_hidden = self.opts.show_hidden_files_by_default
Expand Down
8 changes: 4 additions & 4 deletions lua/blink/cmp/sources/path/lib.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
local regex = require('blink.cmp.sources.path.regex')
local lib = {}

--- @param path_regex vim.regex
--- @param get_cwd fun(context: blink.cmp.Context): string
--- @param context blink.cmp.Context
function lib.dirname(path_regex, get_cwd, context)
function lib.dirname(get_cwd, context)
-- 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)
local s = regex.PATH:match_str(line_before_cursor)
if not s then return nil end

local dirname = string.gsub(string.sub(line_before_cursor, s + 2), '%a*$', '') -- exclude '/'
local dirname = string.gsub(string.sub(line_before_cursor, s + 2), regex.NAME .. '*$', '') -- exclude '/'
local prefix = string.sub(line_before_cursor, 1, s + 1) -- include '/'

local buf_dirname = get_cwd(context)
Expand Down

0 comments on commit 8df826f

Please sign in to comment.