Skip to content

Commit 5295e6a

Browse files
feat: ignore_root_slash optiion for path source (#1678)
1 parent 07a09ac commit 5295e6a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lua/blink/cmp/sources/path/init.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
--- @field label_trailing_slash boolean
1010
--- @field get_cwd fun(context: blink.cmp.Context): string
1111
--- @field show_hidden_files_by_default boolean
12+
--- @field ignore_root_slash boolean
1213

1314
--- @class blink.cmp.Source
1415
--- @field opts blink.cmp.PathOpts
@@ -23,12 +24,14 @@ function path.new(opts)
2324
label_trailing_slash = true,
2425
get_cwd = function(context) return vim.fn.expand(('#%d:p:h'):format(context.bufnr)) end,
2526
show_hidden_files_by_default = false,
27+
ignore_root_slash = false,
2628
})
2729
require('blink.cmp.config.utils').validate('sources.providers.path', {
2830
trailing_slash = { opts.trailing_slash, 'boolean' },
2931
label_trailing_slash = { opts.label_trailing_slash, 'boolean' },
3032
get_cwd = { opts.get_cwd, 'function' },
3133
show_hidden_files_by_default = { opts.show_hidden_files_by_default, 'boolean' },
34+
ignore_root_slash = { opts.ignore_root_slash, 'boolean' },
3235
}, opts)
3336

3437
self.opts = opts
@@ -43,7 +46,7 @@ function path:get_completions(context, callback)
4346

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

46-
local dirname = lib.dirname(self.opts.get_cwd, context)
49+
local dirname = lib.dirname(self.opts, context)
4750
if not dirname then return callback({ is_incomplete_forward = false, is_incomplete_backward = false, items = {} }) end
4851

4952
local include_hidden = self.opts.show_hidden_files_by_default

lua/blink/cmp/sources/path/lib.lua

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
local regex = require('blink.cmp.sources.path.regex')
22
local lib = {}
33

4-
--- @param get_cwd fun(context: blink.cmp.Context): string
4+
--- @param opts blink.cmp.PathOpts
55
--- @param context blink.cmp.Context
6-
function lib.dirname(get_cwd, context)
6+
function lib.dirname(opts, context)
77
-- HACK: move this :sub logic into the context?
88
-- it's not obvious that you need to avoid going back a char if the start_col == end_col
99
local line_before_cursor = context.line:sub(1, context.bounds.start_col - (context.bounds.length == 0 and 1 or 0))
@@ -13,7 +13,7 @@ function lib.dirname(get_cwd, context)
1313
local dirname = string.gsub(string.sub(line_before_cursor, s + 2), regex.NAME .. '*$', '') -- exclude '/'
1414
local prefix = string.sub(line_before_cursor, 1, s + 1) -- include '/'
1515

16-
local buf_dirname = get_cwd(context)
16+
local buf_dirname = opts.get_cwd(context)
1717
if vim.api.nvim_get_mode().mode == 'c' then buf_dirname = vim.fn.getcwd() end
1818
if prefix:match('%.%./$') then return vim.fn.resolve(buf_dirname .. '/../' .. dirname) end
1919
if prefix:match('%./$') or prefix:match('"$') or prefix:match("'$") then
@@ -37,7 +37,13 @@ function lib.dirname(get_cwd, context)
3737
accept = accept and not prefix:match('[%d%)]%s*/$')
3838
-- Ignore / comment
3939
accept = accept and (not prefix:match('^[%s/]*$') or not lib.is_slash_comment())
40-
if accept then return vim.fn.resolve('/' .. dirname) end
40+
if accept then
41+
if opts.ignore_root_slash then
42+
return vim.fn.resolve(buf_dirname .. '/' .. dirname)
43+
else
44+
return vim.fn.resolve('/' .. dirname)
45+
end
46+
end
4147
end
4248
-- Windows drive letter (C:/)
4349
if prefix:match('(%a:)[/\\]$') then return vim.fn.resolve(prefix:match('(%a:)[/\\]$') .. '/' .. dirname) end

0 commit comments

Comments
 (0)