From ba41f0eeb13796c26f6c5f3e8c44b2397a466e39 Mon Sep 17 00:00:00 2001
From: Patrick Ziegler
Date: Sat, 9 Oct 2021 15:40:08 +0200
Subject: [PATCH] fix: alias cwd_only and only_cwd option for buffers and
oldfiles (#1316)
* Alias cwd_only and only_cwd option for builtins
Fixes #1199
Closes #1275
---
doc/telescope.txt | 2 ++
lua/telescope/builtin/init.lua | 2 ++
lua/telescope/builtin/internal.lua | 18 +++++++++++++++++-
3 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/doc/telescope.txt b/doc/telescope.txt
index 886c047..4cc6fbb 100644
--- a/doc/telescope.txt
+++ b/doc/telescope.txt
@@ -1059,6 +1059,7 @@ builtin.oldfiles({opts}) *builtin.oldfiles()*
Options: ~
{only_cwd} (boolean) show only files in the cwd (default: false)
+ {cwd_only} (boolean) alias for only_cwd
builtin.command_history({opts}) *builtin.command_history()*
@@ -1148,6 +1149,7 @@ builtin.buffers({opts}) *builtin.buffers()*
{only_cwd} (boolean) if true, only show buffers in the
current working directory (default:
false)
+ {cwd_only} (boolean) alias for only_cwd
{sort_lastused} (boolean) Sorts current and last buffer to
the top and selects the lastused
(default: false)
diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua
index 0a76074..343c1a0 100644
--- a/lua/telescope/builtin/init.lua
+++ b/lua/telescope/builtin/init.lua
@@ -262,6 +262,7 @@ builtin.loclist = require_on_exported_call("telescope.builtin.internal").loclist
--- Lists previously open files, opens on ``
---@param opts table: options to pass to the picker
---@field only_cwd boolean: show only files in the cwd (default: false)
+---@field cwd_only boolean: alias for only_cwd
builtin.oldfiles = require_on_exported_call("telescope.builtin.internal").oldfiles
--- Lists commands that were executed recently, and reruns them on ``
@@ -302,6 +303,7 @@ builtin.reloader = require_on_exported_call("telescope.builtin.internal").reload
---@field show_all_buffers boolean: if true, show all buffers, including unloaded buffers (default: true)
---@field ignore_current_buffer boolean: if true, don't show the current buffer in the list (default: false)
---@field only_cwd boolean: if true, only show buffers in the current working directory (default: false)
+---@field cwd_only boolean: alias for only_cwd
---@field sort_lastused boolean: Sorts current and last buffer to the top and selects the lastused (default: false)
---@field sort_mru boolean: Sorts all buffers after most recent used. Not just the current and last one (default: false)
---@field bufnr_width number: Defines the width of the buffer numbers in front of the filenames (default: dynamic)
diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua
index c2c37d5..03215b1 100644
--- a/lua/telescope/builtin/internal.lua
+++ b/lua/telescope/builtin/internal.lua
@@ -15,6 +15,20 @@ local conf = require("telescope.config").values
local filter = vim.tbl_filter
+-- Makes sure aliased options are set correctly
+local function apply_cwd_only_aliases(opts)
+ local has_cwd_only = opts.cwd_only ~= nil
+ local has_only_cwd = opts.only_cwd ~= nil
+
+ if has_only_cwd and not has_cwd_only then
+ -- Internally, use cwd_only
+ opts.cwd_only = opts.only_cwd
+ opts.only_cwd = nil
+ end
+
+ return opts
+end
+
local internal = {}
-- TODO: What the heck should we do for accepting this.
@@ -346,6 +360,7 @@ internal.loclist = function(opts)
end
internal.oldfiles = function(opts)
+ opts = apply_cwd_only_aliases(opts)
opts.include_current_session = utils.get_default(opts.include_current_session, true)
local current_buffer = vim.api.nvim_get_current_buf()
@@ -700,6 +715,7 @@ internal.reloader = function(opts)
end
internal.buffers = function(opts)
+ opts = apply_cwd_only_aliases(opts)
local bufnrs = filter(function(b)
if 1 ~= vim.fn.buflisted(b) then
return false
@@ -711,7 +727,7 @@ internal.buffers = function(opts)
if opts.ignore_current_buffer and b == vim.api.nvim_get_current_buf() then
return false
end
- if opts.only_cwd and not string.find(vim.api.nvim_buf_get_name(b), vim.loop.cwd(), 1, true) then
+ if opts.cwd_only and not string.find(vim.api.nvim_buf_get_name(b), vim.loop.cwd(), 1, true) then
return false
end
return true