local regex = require('blink.cmp.sources.path.regex') local lib = {} --- @param get_cwd fun(context: blink.cmp.Context): string --- @param context blink.cmp.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.length == 0 and 1 or 0)) 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), regex.NAME .. '*$', '') -- exclude '/' local prefix = string.sub(line_before_cursor, 1, s + 1) -- include '/' local buf_dirname = 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 return vim.fn.resolve(buf_dirname .. '/' .. dirname) end if prefix:match('~/$') then return vim.fn.resolve(vim.fn.expand('~') .. '/' .. dirname) end local env_var_name = prefix:match('%$([%a_]+)/$') if env_var_name then local env_var_value = vim.fn.getenv(env_var_name) if env_var_value ~= vim.NIL then return vim.fn.resolve(env_var_value .. '/' .. dirname) end end if prefix:match('/$') then local accept = true -- Ignore URL components accept = accept and not prefix:match('%a/$') -- Ignore URL scheme accept = accept and not prefix:match('%a+:/$') and not prefix:match('%a+://$') -- Ignore HTML closing tags accept = accept and not prefix:match('