vscode-bookmarks icon indicating copy to clipboard operation
vscode-bookmarks copied to clipboard

[FEATURE] - Integration with Git for scoped bookmarks

Open jasonwilliams opened this issue 4 years ago • 1 comments

This is a great extension, I use it for Todo lists sometimes, to mark lines of code which I haven't got around to changing yet. The problem is these items can be specific to the branch I'm working on, once i switch branch to something else they no longer make sense. It would be nice to see (scoped) bookmarks show/hide when I switch to and from a branch.

I understand this could be out of scope, but would make a nice feature.

Implementation

You can interact with Git via an API in VSCode using the native git extension. I think its possible to find out if Git is being used by the workspace, if not then you can just not enable this feature.

You can get the RepositoryState object here: https://github.com/Microsoft/vscode/blob/master/extensions/git/src/api/git.d.ts#L99-L116 more repository info here: https://github.com/Microsoft/vscode/blob/master/extensions/git/src/api/git.d.ts#L143-L202

For example github-pull-requests extension uses it here: https://github.com/Microsoft/vscode-pull-request-github/blob/master/src/extension.ts#L53

The branch name can be used as a namespace

Example:

const gitExtension = vscode.extensions.getExtension('vscode.git').exports;
const api = gitExtension.getAPI(1);

const repo = api.repositories[0];
const head = repo.state.HEAD;

// Get the branch and commit 
const {commit,name: branch} = head;

Potential issues

  • Knowing when to remove scoped bookmarks, if a branch is deleted for example. Some prune command may be needed to remove orphan bookmarks.
  • Staying in sync with branch switching, from what I understand there is an events which fire when the branch changes https://github.com/Microsoft/vscode/blob/master/extensions/git/src/api/git.d.ts#L235-L240
  • Gracefully handling no existence of Git extension

jasonwilliams avatar Jan 05 '21 13:01 jasonwilliams

Hi @jasonwilliams ,

First of all, sorry for taking so long to answer you.

I'm glad to know you like the extension. It's one of my favorites, btw 😁 .

About your request, there is already an open issue (with less details) about it #177, and in some cases (depending on your use-case, maybe the already existing bookmarks.saveBookmarksInProject would fit.

The idea behind this setting is to store the bookmarks with the project/folder/repo (.vscode/bookmarks.json file) instead of VS Code internals, so when you share the project, others will see the same bookmarks. If you move this idea to Git branches, when you change the branches, the bookmarks will be read from that branch. The same goes for any point in time (commits). The downside is: You have another resource to take care (commit/push). It is not a perfect solution, but it may work.

Moving to a proper Git support idea, as you already pointed out, have some "potential issues". I also added others to #177. It won't be easy, but I think is doable. It just need a few reengineering, specially because the extension today, uses a file based approach, instead of Uri and relative paths. This refactoring is one of my main desires, because it would make the extension "Remote ready". I have made some progress, but it still need some work, specially for multi-root and external files.

Do you know any extension that already does something similar. I remember CodeTour added some Git ref related feature, but when I first used (right at the beginning) it had some issues.

Thank you for your API directions. It will be of great help.

Alessandro

alefragnani avatar Jan 16 '21 15:01 alefragnani