How to close a `SimpleDiffRequest` in code

Hi team,

I would like to ask how to close a SimpleDiffRequest in code.

I can display the diff between two files using the following code in an editor tab, but I don’t know how to close it:

val diffRequest = SimpleDiffRequest(
    "name", 
    diffContentFactory.create(project, document?.text ?: "", fileType),
    diffContentFactory.create(project, updatedContent, fileType),
    "A", 
    "B" 
)

DiffManager.getInstance().showDiff(project, diffRequest, DiffDialogHints.FRAME)

Could you please advise on how to properly close it in code (e.g which function I should use)?

Thanks!

Hi @phanthaiduong22!

If you always show diff in editor, you can use com.intellij.diff.editor.DiffEditorTabFilesManager#showDiffFile explicitly passing something like ChainDiffVirtualFile(SimpleDiffRequestChain(diffRequest), "My Diff") there. This way you can close the editor via com.intellij.openapi.fileEditor.FileEditorManager#closeFile passing the file there.

Otherwise, you can get com.intellij.openapi.fileEditor.FileEditorManager#getOpenFiles and do filtering.

1 Like

Thank you, Ilia Shulgin! This approach works for me.

I have one further question: My SimpleDiffView request creates another tab that contains the DiffView.

I also tried using ChainDiffVirtualFile, but it also creates a new editor tab displaying the DiffView.

Is there a way to show the DiffView directly in a single editor?

For example, I have a main.py file and an updatedContent (string), and I want to display the diff directly within main.py.

It’s not achievable out-of-the-box using any *DiffViewer we have now.

However, you can calculate diff via ComparisonManager and plug-in into editor to display diff fragments using low-level API as DiffDrawUtil.LineHighlighterBuilder, DiffDrawUtil.createInlineHighlighter and DiffGutterOperation.

I will also add this as an example how it’s implemented in displaying code review changes in our gitlab/github plugins - intellij-community/platform/collaboration-tools/src/com/intellij/collaboration/ui/codereview/editor/CodeReviewEditorGutterChangesRenderer.kt at 8c9a8c358b9fae54828ed283e7b6bab90b87ee9a · JetBrains/intellij-community · GitHub