From 91fe52de49a9a575e716c458b98bfc2dd241accf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joly?= Date: Sun, 7 Aug 2022 23:13:31 +0100 Subject: [PATCH 1/9] fix: pass down the opts to the second prompts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this patch, the “global” options passed to the Telescope command were not passed down to the second prompt (the one selecting the files for instance). This would cause a change of layout set like so: ``` :Telescope repo list layout_strategy=center ``` not to be applied to the second prompt. This patch fixes that and correctly passes the options down to the second prompt. Related to #44 (might fix it) --- lua/telescope/_extensions/repo/main.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/telescope/_extensions/repo/main.lua b/lua/telescope/_extensions/repo/main.lua index 46743b0..26bc109 100644 --- a/lua/telescope/_extensions/repo/main.lua +++ b/lua/telescope/_extensions/repo/main.lua @@ -164,20 +164,20 @@ local function call_picker(opts, command, prompt_title_supplement) if type == "default" then actions._close(prompt_bufnr, false) vim.schedule(function() - project_files({ cwd = dir }) + project_files(vim.tbl_extend("force", opts, { cwd = dir })) end) end if type == "vertical" then actions._close(prompt_bufnr, false) vim.schedule(function() - project_live_grep({ cwd = dir }) + project_live_grep(vim.tbl_extend("force", opts, { cwd = dir })) end) return end if type == "tab" then vim.cmd("tabe " .. dir) vim.cmd("tcd " .. dir) - project_files({ cwd = dir }) + project_files(vim.tbl_extend("force", opts, { cwd = dir })) return end end) From 95ffb4631f2509468827fc22395169396ce862e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joly?= Date: Tue, 16 Aug 2022 08:24:01 +0100 Subject: [PATCH 2/9] fix: the second picker couldn't open any file Due to options being changed for the first picker (in particular, a custom formatter is set), the second picker was unable to open any file, it would only point to folders. This commit fixes this behavior by passing down only the user options to the second picker, along with the ones specifically needed by it. Fixes #46 --- lua/telescope/_extensions/repo/main.lua | 39 +++++++++++++++---------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/lua/telescope/_extensions/repo/main.lua b/lua/telescope/_extensions/repo/main.lua index 26bc109..e15521c 100644 --- a/lua/telescope/_extensions/repo/main.lua +++ b/lua/telescope/_extensions/repo/main.lua @@ -130,14 +130,21 @@ local function project_live_grep(opts) require("telescope.builtin").live_grep(opts) end -local function call_picker(opts, command, prompt_title_supplement) +local function call_picker(list_opts, command, prompt_title_supplement, user_opts) + if list_opts == nil then + error("Incorrect call to call_picker, list_opts should be specified to pass relevant options to the first picker") + end + if user_opts == nil then + error("Incorrect call to call_picker, user_opts should be specified to pass relevant options to the second picker") + end + local prompt_title = "Git repositories" if prompt_title_supplement ~= nil then prompt_title = prompt_title .. prompt_title_supplement end - pickers.new(opts, { + pickers.new(list_opts, { prompt_title = prompt_title, - finder = finders.new_oneshot_job(command, opts), + finder = finders.new_oneshot_job(command, list_opts), previewer = previewers.new_termopen_previewer({ get_command = function(entry) local dir = Path:new(from_entry.path(entry)) @@ -156,7 +163,7 @@ local function call_picker(opts, command, prompt_title_supplement) return utils.find_generic_previewer_for_document(doc.filename) end, }), - sorter = conf.file_sorter(opts), + sorter = conf.file_sorter(list_opts), attach_mappings = function(prompt_bufnr) actions_set.select:replace(function(_, type) local entry = actions_state.get_selected_entry() @@ -164,20 +171,20 @@ local function call_picker(opts, command, prompt_title_supplement) if type == "default" then actions._close(prompt_bufnr, false) vim.schedule(function() - project_files(vim.tbl_extend("force", opts, { cwd = dir })) + project_files(vim.tbl_extend("force", user_opts, { cwd = dir })) end) end if type == "vertical" then actions._close(prompt_bufnr, false) vim.schedule(function() - project_live_grep(vim.tbl_extend("force", opts, { cwd = dir })) + project_live_grep(vim.tbl_extend("force", list_opts, { cwd = dir })) end) return end if type == "tab" then vim.cmd("tabe " .. dir) vim.cmd("tcd " .. dir) - project_files(vim.tbl_extend("force", opts, { cwd = dir })) + project_files(vim.tbl_extend("force", list_opts, { cwd = dir })) return end end) @@ -188,20 +195,22 @@ end -- List of repos built using locate (or variants) M.cached_list = function(opts) - opts = vim.tbl_deep_extend("force", r_config.values.cached_list or {}, opts or {}) - opts.entry_maker = t_utils.get_lazy_default(opts.entry_maker, gen_from_locate_wrapper, opts) - local locate_command = cached_list.prepare_command(opts) + local common_opts = opts or {} + local list_opts = vim.tbl_deep_extend("force", r_config.values.cached_list or {}, common_opts) + list_opts.entry_maker = t_utils.get_lazy_default(list_opts.entry_maker, gen_from_locate_wrapper, list_opts) + local locate_command = cached_list.prepare_command(list_opts) - call_picker(opts, locate_command, " (cached)") + call_picker(list_opts, locate_command, " (cached)", common_opts) end -- Always up to date list of repos built using fd M.list = function(opts) - opts = vim.tbl_deep_extend("force", r_config.values.list or {}, opts or {}) - opts.entry_maker = t_utils.get_lazy_default(opts.entry_maker, gen_from_fd, opts) - local fd_command = list.prepare_command(opts) + local common_opts = opts or {} + local list_opts = vim.tbl_deep_extend("force", r_config.values.list or {}, common_opts) + list_opts.entry_maker = t_utils.get_lazy_default(list_opts.entry_maker, gen_from_fd, list_opts) + local fd_command = list.prepare_command(list_opts) - call_picker(opts, fd_command, " (built on the fly)") + call_picker(list_opts, fd_command, " (built on the fly)", common_opts) end return M From e5da73d52bb49a1d1aeff2463980ee600f98edc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joly?= Date: Tue, 16 Aug 2022 08:30:03 +0100 Subject: [PATCH 3/9] style: run stylua --- lua/telescope/_extensions/repo/main.lua | 98 +++++++++++++------------ 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/lua/telescope/_extensions/repo/main.lua b/lua/telescope/_extensions/repo/main.lua index e15521c..b31ed38 100644 --- a/lua/telescope/_extensions/repo/main.lua +++ b/lua/telescope/_extensions/repo/main.lua @@ -142,55 +142,57 @@ local function call_picker(list_opts, command, prompt_title_supplement, user_opt if prompt_title_supplement ~= nil then prompt_title = prompt_title .. prompt_title_supplement end - pickers.new(list_opts, { - prompt_title = prompt_title, - finder = finders.new_oneshot_job(command, list_opts), - previewer = previewers.new_termopen_previewer({ - get_command = function(entry) - local dir = Path:new(from_entry.path(entry)) - local doc = search_markdown_readme(dir) - if doc then - return utils.find_markdown_previewer_for_document(doc.filename) - end - doc = search_generic_readme(dir) - if not doc then - -- TODO: doc may be previewed in a plain text. Can I use syntax highlight? - doc = search_doc(dir) - end - if not doc then - return { "echo", "" } - end - return utils.find_generic_previewer_for_document(doc.filename) + pickers + .new(list_opts, { + prompt_title = prompt_title, + finder = finders.new_oneshot_job(command, list_opts), + previewer = previewers.new_termopen_previewer({ + get_command = function(entry) + local dir = Path:new(from_entry.path(entry)) + local doc = search_markdown_readme(dir) + if doc then + return utils.find_markdown_previewer_for_document(doc.filename) + end + doc = search_generic_readme(dir) + if not doc then + -- TODO: doc may be previewed in a plain text. Can I use syntax highlight? + doc = search_doc(dir) + end + if not doc then + return { "echo", "" } + end + return utils.find_generic_previewer_for_document(doc.filename) + end, + }), + sorter = conf.file_sorter(list_opts), + attach_mappings = function(prompt_bufnr) + actions_set.select:replace(function(_, type) + local entry = actions_state.get_selected_entry() + local dir = from_entry.path(entry) + if type == "default" then + actions._close(prompt_bufnr, false) + vim.schedule(function() + project_files(vim.tbl_extend("force", user_opts, { cwd = dir })) + end) + end + if type == "vertical" then + actions._close(prompt_bufnr, false) + vim.schedule(function() + project_live_grep(vim.tbl_extend("force", list_opts, { cwd = dir })) + end) + return + end + if type == "tab" then + vim.cmd("tabe " .. dir) + vim.cmd("tcd " .. dir) + project_files(vim.tbl_extend("force", list_opts, { cwd = dir })) + return + end + end) + return true end, - }), - sorter = conf.file_sorter(list_opts), - attach_mappings = function(prompt_bufnr) - actions_set.select:replace(function(_, type) - local entry = actions_state.get_selected_entry() - local dir = from_entry.path(entry) - if type == "default" then - actions._close(prompt_bufnr, false) - vim.schedule(function() - project_files(vim.tbl_extend("force", user_opts, { cwd = dir })) - end) - end - if type == "vertical" then - actions._close(prompt_bufnr, false) - vim.schedule(function() - project_live_grep(vim.tbl_extend("force", list_opts, { cwd = dir })) - end) - return - end - if type == "tab" then - vim.cmd("tabe " .. dir) - vim.cmd("tcd " .. dir) - project_files(vim.tbl_extend("force", list_opts, { cwd = dir })) - return - end - end) - return true - end, - }):find() + }) + :find() end -- List of repos built using locate (or variants) From 3eaadf6155228e7ece75ddc7b429d389ad275591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joly?= Date: Sun, 9 Oct 2022 18:34:51 +0100 Subject: [PATCH 4/9] chore: update stylua in CI The old version is too different from my local one, causing the CI to fail. --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b3fac3a..6a8e067 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -28,4 +28,4 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} # CLI arguments args: --check . - version: 0.13.1 + version: v0.14.3 From b22a370210ad16e23f94ccf23cace955b801d652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joly?= Date: Sun, 8 May 2022 20:52:33 +0000 Subject: [PATCH 5/9] feat: add a setting to switch to the project directory Switch to an opened project directory, in each window and for the corresponding file. Fixes #8 --- README.md | 3 ++ lua/telescope/_extensions/repo.lua | 10 ++-- .../_extensions/repo/autocmd_lcd.lua | 50 +++++++++++++++++++ lua/telescope/_extensions/repo/config.lua | 7 ++- lua/telescope/_extensions/repo/main.lua | 11 +++- 5 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 lua/telescope/_extensions/repo/autocmd_lcd.lua diff --git a/README.md b/README.md index 094a37a..4b090b8 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,9 @@ You can change the default argument given to subcommands (like [`list`](#list) o "value", }, }, + settings = { + auto_lcd = true, + } }, }, } diff --git a/lua/telescope/_extensions/repo.lua b/lua/telescope/_extensions/repo.lua index 6f8179b..4704328 100644 --- a/lua/telescope/_extensions/repo.lua +++ b/lua/telescope/_extensions/repo.lua @@ -1,12 +1,14 @@ local main = require("telescope._extensions.repo.main") -local r_config = require("telescope._extensions.repo.config") -local health = require("telescope._extensions.repo.health") local fallback_error = { "Falling back to `:Telescope repo list`, but this behavior may change in the future" } return require("telescope").register_extension({ - health = health.check, - setup = r_config.setup, + health = function() + require("telescope._extensions.repo.health").check() + end, + setup = function(opts) + require("telescope._extensions.repo.config").setup(opts) + end, exports = { list = main.list, cached_list = main.cached_list, diff --git a/lua/telescope/_extensions/repo/autocmd_lcd.lua b/lua/telescope/_extensions/repo/autocmd_lcd.lua new file mode 100644 index 0000000..839611b --- /dev/null +++ b/lua/telescope/_extensions/repo/autocmd_lcd.lua @@ -0,0 +1,50 @@ +local Path = require("plenary.path") + +local M = {} + +M.active = false + +-- List of absolute paths to projects opened with the extension +local project_paths = {} + +-- Add a project path so that the autocmd will lcd to it if a file in that path is opened +function M.add_project(path) + local abs_path = Path:new(path) + abs_path = abs_path:absolute() + project_paths[abs_path] = true +end + +-- Find the best suited project path. Can be passed a file +local function find_project(path_or_file) + local buf_path = Path:new(path_or_file) + local abs_buf_path = buf_path:absolute() + while not (project_paths[abs_buf_path] or abs_buf_path == "/") do + buf_path = buf_path:parent() + abs_buf_path = buf_path:absolute() + end + if abs_buf_path ~= "/" then + return abs_buf_path + end + return nil +end + +-- Define autocmd to change the folder of the current file (with lcd). +function M.setup() + M.active = true + -- Ensure we create only one autocmd, even if the function is called multiple times + local autocmd_group = vim.api.nvim_create_augroup("telescope_repo_lcd", { clear = true }) + + vim.api.nvim_create_autocmd({ "BufNewFile", "BufRead" }, { + callback = function() + local path_or_file = vim.fn.expand("%") + local project_path = find_project(path_or_file) + if project_paths then + vim.cmd("lcd " .. project_path) + end + end, + group = autocmd_group, + desc = "lcd to the deepest project directory", + }) +end + +return M diff --git a/lua/telescope/_extensions/repo/config.lua b/lua/telescope/_extensions/repo/config.lua index eaf84b3..d06a91c 100644 --- a/lua/telescope/_extensions/repo/config.lua +++ b/lua/telescope/_extensions/repo/config.lua @@ -2,8 +2,11 @@ local M = {} M.values = {} -M.setup = function(opts) - M.values = opts +function M.setup(opts) + M.values = opts or {} + if M.values.settings and M.values.settings.auto_lcd then + require("telescope._extensions.repo.autocmd_lcd").setup() + end end return M diff --git a/lua/telescope/_extensions/repo/main.lua b/lua/telescope/_extensions/repo/main.lua index b31ed38..4edb23d 100644 --- a/lua/telescope/_extensions/repo/main.lua +++ b/lua/telescope/_extensions/repo/main.lua @@ -14,6 +14,7 @@ local t_utils = require("telescope.utils") local Path = require("plenary.path") -- Other modules in this plugin +local autocmd_lcd = require("telescope._extensions.repo.autocmd_lcd") local utils = require("telescope._extensions.repo.utils") local list = require("telescope._extensions.repo.list") local cached_list = require("telescope._extensions.repo.cached_list") @@ -132,10 +133,12 @@ end local function call_picker(list_opts, command, prompt_title_supplement, user_opts) if list_opts == nil then - error("Incorrect call to call_picker, list_opts should be specified to pass relevant options to the first picker") + error([[ + Incorrect call to call_picker, list_opts should be specified to pass relevant options to the first picker]]) end if user_opts == nil then - error("Incorrect call to call_picker, user_opts should be specified to pass relevant options to the second picker") + error([[ + Incorrect call to call_picker, user_opts should be specified to pass relevant options to the second picker]]) end local prompt_title = "Git repositories" @@ -169,6 +172,10 @@ local function call_picker(list_opts, command, prompt_title_supplement, user_opt actions_set.select:replace(function(_, type) local entry = actions_state.get_selected_entry() local dir = from_entry.path(entry) + if autocmd_lcd.active and type ~= "" then + autocmd_lcd.add_project(dir) + end + if type == "default" then actions._close(prompt_bufnr, false) vim.schedule(function() From 204cc637f64ac6c503fd87141e28060504df0b5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joly?= Date: Fri, 30 Sep 2022 00:08:44 +0100 Subject: [PATCH 6/9] docs: Start to require nvim 0.7 The autocommand Lua API requires at least version 0.6. At the time of writing, telescope requires nvim 0.7. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4b090b8..148c01a 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ end_insert --> # 🦘 telescope-repo.nvim: jump around the repositories in your filesystem, without any setup -![Neovim version](https://img.shields.io/badge/Neovim-0.5-57A143?style=flat&logo=neovim) [![](https://img.shields.io/badge/powered%20by-riss-lightgrey)](https://cj.rs/riss) ![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/cljoly/telescope-repo.nvim?color=darkgreen&sort=semver) +![Neovim version](https://img.shields.io/badge/Neovim-0.7-57A143?style=flat&logo=neovim) [![](https://img.shields.io/badge/powered%20by-riss-lightgrey)](https://cj.rs/riss) ![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/cljoly/telescope-repo.nvim?color=darkgreen&sort=semver)