mirror of
https://github.com/chylex/IntelliJ-Inspection-Lens.git
synced 2025-05-18 14:34:09 +02:00
Replace MultiParentDisposable with IntelliJ's Lifetime API
This commit is contained in:
parent
ce85aa130d
commit
7c3910854d
src/main/kotlin/com/chylex/intellij/inspectionlens
@ -1,6 +1,5 @@
|
|||||||
package com.chylex.intellij.inspectionlens
|
package com.chylex.intellij.inspectionlens
|
||||||
|
|
||||||
import com.chylex.intellij.inspectionlens.util.MultiParentDisposable
|
|
||||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo
|
import com.intellij.codeInsight.daemon.impl.HighlightInfo
|
||||||
import com.intellij.lang.annotation.HighlightSeverity
|
import com.intellij.lang.annotation.HighlightSeverity
|
||||||
import com.intellij.openapi.application.ApplicationManager
|
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.impl.event.MarkupModelListener
|
||||||
import com.intellij.openapi.editor.markup.RangeHighlighter
|
import com.intellij.openapi.editor.markup.RangeHighlighter
|
||||||
import com.intellij.openapi.fileEditor.TextEditor
|
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].
|
* 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 editor = textEditor.editor
|
||||||
val markupModel = DocumentMarkupModel.forDocument(editor.document, editor.project, false)
|
val markupModel = DocumentMarkupModel.forDocument(editor.document, editor.project, false)
|
||||||
if (markupModel is MarkupModelEx) {
|
if (markupModel is MarkupModelEx) {
|
||||||
val pluginDisposable = ApplicationManager.getApplication().getService(InspectionLensPluginDisposableService::class.java)
|
val pluginLifetime = ApplicationManager.getApplication().getService(InspectionLensPluginDisposableService::class.java).createLifetime()
|
||||||
|
val editorLifetime = textEditor.createLifetime()
|
||||||
val listenerDisposable = MultiParentDisposable()
|
|
||||||
listenerDisposable.registerWithParent(textEditor)
|
|
||||||
listenerDisposable.registerWithParent(pluginDisposable)
|
|
||||||
|
|
||||||
val listener = LensMarkupModelListener(editor)
|
val listener = LensMarkupModelListener(editor)
|
||||||
markupModel.addMarkupModelListener(listenerDisposable.self, listener)
|
markupModel.addMarkupModelListener(pluginLifetime.intersect(editorLifetime).createNestedDisposable(), listener)
|
||||||
listener.showAllValid(markupModel.allHighlighters)
|
listener.showAllValid(markupModel.allHighlighters)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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) }
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user