From b0c04c62b5cce850ce0d10cd6a06163b57042318 Mon Sep 17 00:00:00 2001 From: Logan Connolly Date: Wed, 1 Sep 2021 16:52:45 +0200 Subject: [PATCH] feat: add utility for fetching buffer directory (#902) --- lua/telescope/builtin/init.lua | 9 ++++++--- lua/telescope/utils.lua | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 3d9d3f6..ffc217e 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -66,8 +66,9 @@ local builtin = {} -- -- ---- Search for a string in your current working directory and get results live as you type (respecting .gitignore) +--- Search for a string and get results live as you type (respecting .gitignore) ---@param opts table: options to pass to the picker +---@field cwd string: directory path to search from (default is cwd, use utils.buffer_dir() to search relative to open buffer) ---@field grep_open_files boolean: if true, restrict search to open files only, mutually exclusive with `search_dirs` ---@field search_dirs table: directory/directories to search in, mutually exclusive with `grep_open_files` ---@field additional_args function: function(opts) which returns a table of additional arguments to be passed on @@ -75,14 +76,16 @@ builtin.live_grep = require("telescope.builtin.files").live_grep --- Searches for the string under your cursor in your current working directory ---@param opts table: options to pass to the picker +---@field cwd string: directory path to search from (default is cwd, use utils.buffer_dir() to search relative to open buffer) ---@field search string: the query to search ---@field search_dirs table: directory/directories to search in ---@field use_regex boolean: if true, special characters won't be escaped, allows for using regex (default is false) ---@field additional_args function: function(opts) which returns a table of additional arguments to be passed on builtin.grep_string = require("telescope.builtin.files").grep_string ---- Lists files in your current working directory, respects .gitignore +--- Search for files (respecting .gitignore) ---@param opts table: options to pass to the picker +---@field cwd string: directory path to search from (default is cwd, use utils.buffer_dir() to search relative to open buffer) ---@field find_command table: command line arguments for `find_files` to use for the search, overrides default config ---@field follow boolean: if true, follows symlinks (i.e. uses `-L` flag for the `find` command) ---@field hidden boolean: determines whether to show hidden files or not (default is false) @@ -102,7 +105,7 @@ builtin.fd = builtin.find_files --- create the file `init.lua` inside of `lua/telescope` and will create the necessary folders (similar to how --- `mkdir -p` would work) if they do not already exist ---@param opts table: options to pass to the picker ----@field cwd string: directory path to browse (default is cwd) +---@field cwd string: directory path to browse (default is cwd, use utils.buffer_dir() to browse relative to open buffer) ---@field depth number: file tree depth to display (default is 1) ---@field dir_icon string: change the icon for a directory. default:  ---@field hidden boolean: determines whether to show hidden files or not (default is false) diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index ee57417..50b5298 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -414,6 +414,10 @@ function utils.data_directory() return Path:new({ base_directory, "data" }):absolute() .. Path.path.sep end +function utils.buffer_dir() + return vim.fn.expand('%:p:h') +end + function utils.display_termcodes(str) return str:gsub(string.char(9), ""):gsub("", ""):gsub(" ", "") end