1
0
mirror of https://github.com/chylex/IntelliJ-Inspection-Lens.git synced 2025-05-03 23:34:09 +02:00

Replace MultiParentDisposable with IntelliJ's Lifetime API

This commit is contained in:
chylex 2022-10-24 00:55:54 +02:00
parent ce85aa130d
commit 7c3910854d
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
2 changed files with 6 additions and 25 deletions
src/main/kotlin/com/chylex/intellij/inspectionlens

View File

@ -1,6 +1,5 @@
package com.chylex.intellij.inspectionlens
import com.chylex.intellij.inspectionlens.util.MultiParentDisposable
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.openapi.application.ApplicationManager
@ -11,6 +10,9 @@ import com.intellij.openapi.editor.impl.DocumentMarkupModel
import com.intellij.openapi.editor.impl.event.MarkupModelListener
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.openapi.fileEditor.TextEditor
import com.intellij.openapi.rd.createLifetime
import com.intellij.openapi.rd.createNestedDisposable
import com.jetbrains.rd.util.lifetime.intersect
/**
* Listens for inspection highlights and reports them to [EditorInlayLensManager].
@ -95,14 +97,11 @@ class LensMarkupModelListener private constructor(editor: Editor) : MarkupModelL
val editor = textEditor.editor
val markupModel = DocumentMarkupModel.forDocument(editor.document, editor.project, false)
if (markupModel is MarkupModelEx) {
val pluginDisposable = ApplicationManager.getApplication().getService(InspectionLensPluginDisposableService::class.java)
val listenerDisposable = MultiParentDisposable()
listenerDisposable.registerWithParent(textEditor)
listenerDisposable.registerWithParent(pluginDisposable)
val pluginLifetime = ApplicationManager.getApplication().getService(InspectionLensPluginDisposableService::class.java).createLifetime()
val editorLifetime = textEditor.createLifetime()
val listener = LensMarkupModelListener(editor)
markupModel.addMarkupModelListener(listenerDisposable.self, listener)
markupModel.addMarkupModelListener(pluginLifetime.intersect(editorLifetime).createNestedDisposable(), listener)
listener.showAllValid(markupModel.allHighlighters)
}
}

View File

@ -1,18 +0,0 @@
package com.chylex.intellij.inspectionlens.util
import com.intellij.openapi.Disposable
import com.intellij.openapi.util.Disposer
import java.lang.ref.WeakReference
/**
* A [Disposable] that can have multiple parents, and will be disposed when any parent is disposed.
* A [WeakReference] and a lambda will remain in memory for every parent that is not disposed.
*/
class MultiParentDisposable {
val self = Disposer.newDisposable()
fun registerWithParent(parent: Disposable) {
val weakSelfReference = WeakReference(self)
Disposer.register(parent) { weakSelfReference.get()?.let(Disposer::dispose) }
}
}