Skip to content
Merged
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
5 changes: 4 additions & 1 deletion lua/blink/cmp/sources/path/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
--- @field label_trailing_slash boolean
--- @field get_cwd fun(context: blink.cmp.Context): string
--- @field show_hidden_files_by_default boolean
--- @field ignore_root_slash boolean

--- @class blink.cmp.Source
--- @field opts blink.cmp.PathOpts
Expand All @@ -23,12 +24,14 @@ function path.new(opts)
label_trailing_slash = true,
get_cwd = function(context) return vim.fn.expand(('#%d:p:h'):format(context.bufnr)) end,
show_hidden_files_by_default = false,
ignore_root_slash = false,
})
require('blink.cmp.config.utils').validate('sources.providers.path', {
trailing_slash = { opts.trailing_slash, 'boolean' },
label_trailing_slash = { opts.label_trailing_slash, 'boolean' },
get_cwd = { opts.get_cwd, 'function' },
show_hidden_files_by_default = { opts.show_hidden_files_by_default, 'boolean' },
ignore_root_slash = { opts.ignore_root_slash, 'boolean' },
}, opts)

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

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

local dirname = lib.dirname(self.opts.get_cwd, context)
local dirname = lib.dirname(self.opts, 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
14 changes: 10 additions & 4 deletions lua/blink/cmp/sources/path/lib.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local regex = require('blink.cmp.sources.path.regex')
local lib = {}

--- @param get_cwd fun(context: blink.cmp.Context): string
--- @param opts blink.cmp.PathOpts
--- @param context blink.cmp.Context
function lib.dirname(get_cwd, context)
function lib.dirname(opts, 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.length == 0 and 1 or 0))
Expand All @@ -13,7 +13,7 @@ function lib.dirname(get_cwd, context)
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)
local buf_dirname = opts.get_cwd(context)
if vim.api.nvim_get_mode().mode == 'c' then buf_dirname = vim.fn.getcwd() end
if prefix:match('%.%./$') then return vim.fn.resolve(buf_dirname .. '/../' .. dirname) end
if prefix:match('%./$') or prefix:match('"$') or prefix:match("'$") then
Expand All @@ -37,7 +37,13 @@ function lib.dirname(get_cwd, context)
accept = accept and not prefix:match('[%d%)]%s*/$')
-- Ignore / comment
accept = accept and (not prefix:match('^[%s/]*$') or not lib.is_slash_comment())
if accept then return vim.fn.resolve('/' .. dirname) end
if accept then
if opts.ignore_root_slash then
return vim.fn.resolve(buf_dirname .. '/' .. dirname)
else
return vim.fn.resolve('/' .. dirname)
end
end
end
-- Windows drive letter (C:/)
if prefix:match('(%a:)[/\\]$') then return vim.fn.resolve(prefix:match('(%a:)[/\\]$') .. '/' .. dirname) end
Expand Down