From be0da08d8d2f72941509558c65ae86d5abe6bee4 Mon Sep 17 00:00:00 2001 From: Ilan Schemoul Date: Wed, 29 Apr 2026 19:38:06 +0200 Subject: [PATCH] add use_git_root_as_cwd option --- README.md | 4 ++++ lua/telescope/_extensions/smart_open.lua | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 622e370..aad281b 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,10 @@ require('telescope').extensions.smart_open.smart_open { Limit the results to files under the current working directory. This is normally not needed because if you prefer this pattern of access, then the plugin will pick up on that over time regardless, to the point where files under `cwd` will be recommended above all others. +- `use_git_root_as_cwd` (default: `false`) + + Use the root (based on .git) as the working directory. + - `filename_first` (default: `true`) Format filename as "filename path/to/parent/directory" if `true` and "path/to/parent/directory/filename" if `false`. diff --git a/lua/telescope/_extensions/smart_open.lua b/lua/telescope/_extensions/smart_open.lua index dc03edf..f8e40ff 100644 --- a/lua/telescope/_extensions/smart_open.lua +++ b/lua/telescope/_extensions/smart_open.lua @@ -10,8 +10,12 @@ local config = require("smart-open").config local smart_open = function(opts) opts = opts or {} - ---@diagnostic disable-next-line: missing-parameter - opts.cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()) + if opts.use_git_root_as_cwd then + opts.cwd = vim.fn.expand(vim.fs.root(0, ".git") or vim.fn.getcwd()) + else + ---@diagnostic disable-next-line: missing-parameter + opts.cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()) + end opts.current_buffer = vim.fn.bufnr("%") > 0 and vim.api.nvim_buf_get_name(vim.fn.bufnr("%")) or "" opts.alternate_buffer = vim.fn.bufnr("#") > 0 and vim.api.nvim_buf_get_name(vim.fn.bufnr("#")) or "" opts.filename_first = opts.filename_first == nil and true or opts.filename_first