How to get the selected content in a Commit?
The version I am using is: Gradle: com.jetbrains:ideaIU:2023.3 (app.jar)
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
CommitMessageI commitPanel = getCommitPanel(anActionEvent);
if (commitPanel == null) {
return;
}
Project project = anActionEvent.getProject();
AbstractCommitWorkflowHandler data = (AbstractCommitWorkflowHandler) anActionEvent.getData(VcsDataKeys.COMMIT_WORKFLOW_HANDLER);
if (data != null){
List<Change> includedChanges = data.getUi().getIncludedChanges();
includedChanges.forEach(change -> {
PartialLocalLineStatusTracker partialTracker = PartialChangesUtil.getPartialTracker(project, change);
partialTracker.getRanges().forEach(range -> {
System.out.println(range);
});
});
}
}
I used the method in this link to get the selected content:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/10709161728018-How-to-get-the-list-of-files-that-are-selected-in-the-commit-dialog
But at partialTracker, a null pointer exception occurs. How can I get the diff content of the selected files and their file names?
请先登录再写评论。
Project project = anActionEvent.getProject();
CommitWorkflowUi data = anActionEvent.getData(VcsDataKeys.COMMIT_WORKFLOW_UI);
if (data != null){
List<Change> includedChanges = data.getIncludedChanges();
includedChanges.forEach(change -> {
VirtualFile virtualFile = change.getVirtualFile();
});
}
At this point, I can already retrieve the content of the selected changes. I found that there is a class that can get the git diff,
GitChangeUtils.getDiff(). Or I could use this approach? My current issue is that I am not sure which method to use to retrieve the diff content of the selected local change.While reading the article in the link, it mentioned using
VcsDataKeys.COMMIT_WORKFLOW_HANDLER, which requires an additional conversion toAbstractCommitWorkflowHandlerto finally getCommitWorkflowUi. However, I found that usingVcsDataKeys.COMMIT_WORKFLOW_UIdirectly retrieves theCommitWorkflowUi.