Get VCS (git) status of PsiElement


I'm trying to develop a plugin that highlights in a file the lines which have changed but are not committed yet. 

I've defined an annotator which is able to color PsiElement, but I'm having a hard time getting the VCS info, only just git info will also work.  Any ideas if I can achieve this from the annotator API or do I need another extension point?



Could you expand a bit on the idea?

It sounds like you're trying to expand gutter highlighting for the changed lines to the whole line (as background/text highlighting) ?

Overall, there's no 'good' API here.
Most notably, no single event that would notify you that smth has changed. 

To read the data you can use
`com.intellij.openapi.vcs.impl.LineStatusTrackerManagerI` -> `getLineStatusTracker(Document)` and `LineStatusTracker.getRanges()`.

As an alternative, there's 
`getChangedRangesInfo(PsiFile)` / `getChangedTextRanges(@NotNull Project project, @NotNull PsiFile file)` / `VcsFacadeImpl.getChangedElements(...)`.

As for events, typing should not be a problem ("text changed -> PSI changed -> annotator updated").
But "git reset --soft" and "git commit" might change line info without touching the Document/Psi. `git4idea.repo.GitRepository#GIT_REPO_CHANGE` should handle such changes via .git/index'.

But there's no event for when `LineStatusTrackerI` updates its state (it needs to load HEAD revision which happens in background).
There's `com.intellij.openapi.vcs.ex.PartialLocalLineStatusTracker#addListener` -> `Listener.onBecomingValid` - but it is not applicable for "Use staging area for commit" mode.

An alternative to `LineStatusTrackerI` would be to load HEAD revision contents and compare them with Document yourself.


Hey Aleksey, thanks for your response.

My goal is to link a comment line (or multiple lines) to the code at the next line, and if that code has changed, highlight the comment.

We end up indeed using the LineStatusTracker, here is the annotator for reference.
It will be great if you could have a glance and tell me what do you think about the implementation!

Also we uploaded a first version to the marketplace for anyone who likes to give it a try:


>It will be great if you could have a glance and tell me what do you think about the implementation!
Note, that I do not have much experience with Psi/annotators.

This creates multiple string objects, which is not necessary. `count { it == '\n' }` ?

Note, that LineStatusTracker works in terms of FileDocumentManager#getDocument(VirtualFile).
If the documents do not match (and lines were shifted by not yet committed changes) - results might be wrong.

I'd also suggest to double-check that 'isRangeModified' works as you expected for deleted lines near start/end of your area of interest.

Otherwise, looks good - keeping in mind the 'event' problem (someone has to restart annotator after commit).
If you end up needing a better API, come to youtrack with suggestions.


Please sign in to leave a comment.