reddit / emacs

xref-find-definitions with multiple active projects

mgulick · 2022-10-19 21:39
I'm not an emacs power user nor very proficient at elisp. I use magit quite heavily in emacs, and I'm regularly working on multiple git repositories at the same time. These are different repositories/worktrees for the same project, just with different branches checked out. I'm not sure if it is relevant, but these are Perl projects, using cperl-mode, and each project has a single TAGS files at the project root. The first time I use \`M-.\` (\`xref-find-definitions\`), I get prompted to load a TAGS file, and I select the one for the current repository I'm using. This works as expected. However when I happen to be editing a file from a different repository and hit M-., the previously loaded TAGS file is still used, so this brings me to the source file from the wrong repository. Since the files look identical, I don't notice this and regularly make edits in the wrong git worktree. I saw that I can load multiple tags tables using visit-tags-table, and then switch between them using select-tags-table. Is there any way to automate this so that when I use \`M-.\` it automatically chooses the TAGS file closest to the current file path? I looked into project.el and projectile, but I couldn't find any examples for how to do this.
redguardtoo · 2022-10-19 22:38
https://github.com/redguardtoo/counsel-etags , it automatically picks up right tags per project.
hvis · 2022-10-20 13:14
I suspect it will require a different xref backend, or a modification of the existing one. Someone could monkeypatch that with third-party code, but `M-x report-emacs-bug` might get you there as well.
mgulick · 2022-10-20 13:28
Thanks for the suggestion. I gave this a try, but I couldn't get it to pick up the existing TAGS files. It kept falling back to a grep. I also didn't see a way to jump to the definition of a function. It looks like it was just searching for references, which you then need to wade through to find which one is the definition.
redguardtoo · 2022-10-20 22:27
It automatically detects the TAGS in project root folder. It falls back to grep if the definition does not exist in TAGS. So you need check TAGS.
FreeAd7233 · 2022-10-24 09:41
Give citre a try. Citre picks up tags depending on the project root automatically, and provides you find definitions and completions easily (and even references if using gnu global)