telescope-repo.nvim icon indicating copy to clipboard operation
telescope-repo.nvim copied to clipboard

feat: Add function to switch between opened projects

Open phgz opened this issue 2 years ago • 1 comments

Hi!

So I've been using a feature for a while on my side with some hacked code and would like to integrate it better in the main repo, so I am asking for how to best do it. The concept is as follow: when the function is called, a telescope picker is opened with current opened projects using opened_projects registry. When a selection is made, the most recent buffer opened in that project is opened (should also lcd if enabled). Here are some screenshots:

  1. Example of classic telescope repo. telescope_repo

  2. When 2 projects have been opened and calling the function. switch

Link to a hacky implementation of mine: https://github.com/phgz/telescope-repo.nvim/commit/e1236c1e7c022ff2e9e5995c2769272ac6cd78a8

In there, I add a get_project_paths() function to be used by user and call user_opts.post_action(dir) if it's defined. Other than that a (I think) bug fix calling project_live_grep with user_opts instead of list_opts and a previous fix on autocmd that I mentioned a while ago, which I saw on one of your dev branch you fixed.

On the user part, here's how I define my command: I first do the inventory of active buffers and "map" them to an opened project. Then I use that list to feed to the search_dirs and set the post action to execute.

vim.keymap.set("n", "<leader>s", function() --
	local project_paths = require("telescope._extensions.repo.autocmd_lcd").get_project_paths()
	local context = api.nvim_get_context({ types = { "bufs" } })
	local bufs = call("msgpackparse", { context["bufs"] })[4]

	local open_projects = vim.iter.filter(function(project_path)
		local found = vim.iter(bufs):find(function(buf_path)
			return vim.startswith(buf_path["f"], project_path)
		end)
		return found or false
	end, project_paths)

	require("telescope").extensions.repo.list(vim.tbl_extend("error", dropdown_theme, {
		search_dirs = vim.tbl_isempty(open_projects) and { "" } or open_projects,
		post_action = post_action_fn,
		prompt = " (opened projects)",
	}))
end)

My post action function takes the selected project by the user and browses jumplist to find most recent file of that project and then simply perform a vim.cmd.edit(to_edit) on the found file.

So either there could be a possibility to leave the opportunity to the users to define their own post action, or simply add a function as a whole (like telescope.extensions.repo.switch) embedded in telescope-repo` and make it public in the API.

Thanks!

phgz avatar Jun 20 '23 01:06 phgz

Hi, thanks for taking the time to explain this!

Let me think about this, I’ll try to come back to you this weekend.

cljoly avatar Jun 22 '23 22:06 cljoly