Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add function to switch between opened projects #62

Open
phgz opened this issue Jun 20, 2023 · 1 comment
Open

feat: Add function to switch between opened projects #62

phgz opened this issue Jun 20, 2023 · 1 comment

Comments

@phgz
Copy link

phgz commented Jun 20, 2023

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
  1. When 2 projects have been opened and calling the function.
switch

Link to a hacky implementation of mine: phgz@e1236c1

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!

@cljoly
Copy link
Owner

cljoly commented Jun 22, 2023

Hi, thanks for taking the time to explain this!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants