From 4a8861d15395d945c1df9236b1823ff5c5c43489 Mon Sep 17 00:00:00 2001 From: ilan schemoul Date: Sun, 7 Jul 2024 13:35:56 +0200 Subject: [PATCH] feat: options no_ignore, no_ignore_parent, hidden and follow These four options are brought from the find_files finder of Telescope. Default option for no_ignore is false therefore unlike the current behaviour all files from .gitignore will be ignored (as it is always done with Telescope and massively improve performance) --- README.md | 22 +++++++++---- lua/smart-open/init.lua | 4 +++ .../_extensions/smart_open/default_config.lua | 4 +++ .../_extensions/smart_open/file_scanner.lua | 33 ++++++++++++++----- .../_extensions/smart_open/finder/finder.lua | 2 +- .../_extensions/smart_open/picker.lua | 4 +++ 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b3555e1..7783b02 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,22 @@ See [default configuration](https://github.com/nvim-telescope/telescope.nvim#tel - `open_buffer_indicators` (default: `{previous = "•", others = "∘"}`) +- `follow` (default: `false`) + + If true, follows symlinks (i.e. uses `-L` flag for the `rg` command) + +- `hidden` (default: `false`) + + Determines whether to show hidden files or not + +- `no_ignore` (default: `false`) + + Show files ignored by .gitignore, .ignore, etc. + +- `no_ignore_parent` (default: `false`) + + Show files ignored by .gitignore, .ignore, etc. in parent dirs. + ### Example Configuration: ``` @@ -205,12 +221,6 @@ telescope.setup { ``` -### Known Limitations - -For files not already in your history, smart-open uses ripgrep for scanning the current directory. (The command is roughly: `rg --files --glob-case-insensitive --hidden --ignore-file=/.ff-ignore -g `). - -As a result, files added to git, _but also ignored by git_, will not be included. While not common, this is something that git allows. If this becomes a problem you can work around it by either changing your git ignore patterns, editing the file in neovim in some other way, (thereby adding it to the history), or by using ripgrep's `.ignore` file for overriding git. - ### Highlight Groups ```vim diff --git a/lua/smart-open/init.lua b/lua/smart-open/init.lua index 9403cf8..eafbe9e 100644 --- a/lua/smart-open/init.lua +++ b/lua/smart-open/init.lua @@ -19,6 +19,10 @@ return { set_config("match_algorithm", ext_config.match_algorithm) set_config("cwd_only", ext_config.cwd_only) set_config("buffer_indicators", ext_config.buffer_indicators) + set_config("hidden", ext_config.hidden) + set_config("no_ignore", ext_config.no_ignore) + set_config("no_ignore_parent", ext_config.no_ignore_parent) + set_config("follow", ext_config.follow) config.db_filename = vim.fn.stdpath("data") .. "/smart_open.sqlite3" diff --git a/lua/telescope/_extensions/smart_open/default_config.lua b/lua/telescope/_extensions/smart_open/default_config.lua index becd9cd..a0e2c18 100644 --- a/lua/telescope/_extensions/smart_open/default_config.lua +++ b/lua/telescope/_extensions/smart_open/default_config.lua @@ -69,4 +69,8 @@ return { previous = "•", others = "∘", }, + no_ignore = false, + no_ignore_parent = false, + follow = false, + hidden = false, } diff --git a/lua/telescope/_extensions/smart_open/file_scanner.lua b/lua/telescope/_extensions/smart_open/file_scanner.lua index 1cba423..f930884 100644 --- a/lua/telescope/_extensions/smart_open/file_scanner.lua +++ b/lua/telescope/_extensions/smart_open/file_scanner.lua @@ -49,27 +49,44 @@ local function spawn(cmd, opts, input, onexit) return stop end -local function ripgrep_scan(basedir, ignore_patterns, on_insert, on_complete) +local function ripgrep_scan(opts, on_insert, on_complete) local stderr = "" local args = { "--files", "--glob-case-insensitive", "--line-buffered", - "--hidden", + "--color", + "never", "--ignore-file", - basedir .. "/.ff-ignore", + opts.cwd .. "/.ff-ignore", } - for _, value in ipairs(ignore_patterns) do + for _, value in ipairs(opts.ignore_patterns) do table.insert(args, "-g") table.insert(args, "!" .. value) end + if opts.hidden then + args[#args + 1] = "--hidden" + end + + if opts.no_ignore then + args[#args + 1] = "--no-ignore" + end + + if opts.no_ignore_parent then + args[#args + 1] = "--no-ignore-parent" + end + + if opts.follow then + args[#args + 1] = "-L" + end + local done = false local stop local start_time - stop = spawn("rg", { args = args, cwd = basedir }, { + stop = spawn("rg", { args = args, cwd = opts.cwd }, { stdout = function(_, chunk) if not start_time then start_time = vim.loop.uptime() @@ -80,7 +97,7 @@ local function ripgrep_scan(basedir, ignore_patterns, on_insert, on_complete) end for line in splitlines(chunk) do - if #line > 0 and on_insert(basedir .. "/" .. line) == false then + if #line > 0 and on_insert(opts.cwd .. "/" .. line) == false then done = true stop() return vim.schedule(function() @@ -99,8 +116,8 @@ local function ripgrep_scan(basedir, ignore_patterns, on_insert, on_complete) end) end -return function(cwd, ignore_patterns, on_insert, on_complete) - ripgrep_scan(cwd, ignore_patterns, on_insert, function(exit_code, err) +return function(opts, on_insert, on_complete) + ripgrep_scan(opts, on_insert, function(exit_code, err) if exit_code ~= 0 then print("ripgrep exited with code", exit_code, "and error:", err) end diff --git a/lua/telescope/_extensions/smart_open/finder/finder.lua b/lua/telescope/_extensions/smart_open/finder/finder.lua index e4975fa..f6fe127 100644 --- a/lua/telescope/_extensions/smart_open/finder/finder.lua +++ b/lua/telescope/_extensions/smart_open/finder/finder.lua @@ -41,7 +41,7 @@ return function(history, opts, context) local h = { frecency = 0, recent_rank = 0 } - file_scanner(opts.cwd, opts.ignore_patterns, function(fullpath) + file_scanner(opts, function(fullpath) if not is_added[fullpath] then local entry_data = create_entry_data(fullpath, h, context) match_runner.add_entry(entry_data) diff --git a/lua/telescope/_extensions/smart_open/picker.lua b/lua/telescope/_extensions/smart_open/picker.lua index ea8e73b..5d7d1b1 100644 --- a/lua/telescope/_extensions/smart_open/picker.lua +++ b/lua/telescope/_extensions/smart_open/picker.lua @@ -36,6 +36,10 @@ function M.start(opts) ignore_patterns = vim.F.if_nil(opts.ignore_patterns, config.ignore_patterns), show_scores = vim.F.if_nil(opts.show_scores, config.show_scores), match_algorithm = opts.match_algorithm or config.match_algorithm, + no_ignore = vim.F.if_nil(opts.no_ignore, config.no_ignore), + no_ignore_parent = vim.F.if_nil(opts.no_ignore_parent, config.no_ignore_parent), + follow = vim.F.if_nil(opts.follow, config.follow), + hidden = vim.F.if_nil(opts.hidden, config.hidden), }, context) opts.get_status_text = finder.get_status_text