Get VCS (git) status of PsiElement

Answered

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?

Thanks

0
3 comments

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 
`com.intellij.codeInsight.actions.VcsFacade`
with 
`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.

1

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!

https://github.com/codin-ai/comments-checker/blob/main/src/main/kotlin/com/github/codinai/comments/annotator/CommentHighlighterAnnotator.kt

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

https://plugins.jetbrains.com/plugin/18855-commentsfixer

0

>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.

>element.text.split("\n")
This creates multiple string objects, which is not necessary. `count { it == '\n' }` ?

>PsiDocumentManager.getInstance(element.project).getLastCommittedDocument(element.containingFile)
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.

0

Please sign in to leave a comment.