Skip to content

Commit

Permalink
Fix project leak
Browse files Browse the repository at this point in the history
  • Loading branch information
lippfi committed Jul 8, 2024
1 parent ea98e50 commit c0419d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/main/java/com/maddyhome/idea/vim/ex/ExOutputModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import com.maddyhome.idea.vim.api.VimOutputPanel
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.helper.vimExOutput
import com.maddyhome.idea.vim.ui.ExOutputPanel
import java.lang.ref.WeakReference

// TODO: We need a nicer way to handle output, especially wrt testing, appending + clearing
class ExOutputModel(private val myEditor: Editor) : VimOutputPanel {
class ExOutputModel(private val myEditor: WeakReference<Editor>) : VimOutputPanel {
private var isActiveInTestMode = false

val editor get() = myEditor.get()

val isActive: Boolean
get() = if (!ApplicationManager.getApplication().isUnitTestMode) {
ExOutputPanel.getNullablePanel(myEditor)?.myActive ?: false
editor?.let { ExOutputPanel.getNullablePanel(it) }?.myActive ?: false
} else {
isActiveInTestMode
}
Expand All @@ -30,11 +33,12 @@ class ExOutputModel(private val myEditor: Editor) : VimOutputPanel {
}

override fun show() {
if (editor == null) return
val currentPanel = injector.outputPanel.getCurrentOutputPanel()
if (currentPanel != null && currentPanel != this) currentPanel.close()

myEditor.vimExOutput = this
val exOutputPanel = ExOutputPanel.getInstance(myEditor)
editor!!.vimExOutput = this
val exOutputPanel = ExOutputPanel.getInstance(editor!!)
if (!exOutputPanel.myActive) {
if (ApplicationManager.getApplication().isUnitTestMode) {
isActiveInTestMode = true
Expand All @@ -46,7 +50,7 @@ class ExOutputModel(private val myEditor: Editor) : VimOutputPanel {

override var text: String = ""
get() = if (!ApplicationManager.getApplication().isUnitTestMode) {
ExOutputPanel.getInstance(myEditor).text
editor?.let { ExOutputPanel.getInstance(it).text } ?: ""
} else {
// ExOutputPanel always returns a non-null string
field
Expand All @@ -56,7 +60,7 @@ class ExOutputModel(private val myEditor: Editor) : VimOutputPanel {
// never pass null to ExOutputPanel, but we do store it for tests, so we know if we're active or not
val newValue = value.removeSuffix("\n")
if (!ApplicationManager.getApplication().isUnitTestMode) {
ExOutputPanel.getInstance(myEditor).setText(newValue)
editor?.let { ExOutputPanel.getInstance(it).setText(newValue) }
} else {
field = newValue
isActiveInTestMode = newValue.isNotEmpty()
Expand All @@ -73,7 +77,7 @@ class ExOutputModel(private val myEditor: Editor) : VimOutputPanel {

override fun close() {
if (!ApplicationManager.getApplication().isUnitTestMode) {
ExOutputPanel.getInstance(myEditor).close()
editor?.let { ExOutputPanel.getInstance(it).close() }
}
else {
isActiveInTestMode = false
Expand All @@ -85,7 +89,7 @@ class ExOutputModel(private val myEditor: Editor) : VimOutputPanel {
fun getInstance(editor: Editor): ExOutputModel {
var model = editor.vimExOutput
if (model == null) {
model = ExOutputModel(editor)
model = ExOutputModel(WeakReference(editor))
editor.vimExOutput = model
}
return model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ import com.maddyhome.idea.vim.api.VimOutputPanel
import com.maddyhome.idea.vim.api.VimOutputPanelServiceBase
import com.maddyhome.idea.vim.ex.ExOutputModel
import com.maddyhome.idea.vim.newapi.ij
import java.lang.ref.WeakReference

class IjOutputPanelService : VimOutputPanelServiceBase() {
private var activeOutputPanel: VimOutputPanel? = null

override fun getCurrentOutputPanel(): VimOutputPanel? {
return activeOutputPanel?.takeIf { (it as ExOutputModel).isActive }
return activeOutputPanel?.takeIf {
(it as ExOutputModel)
it.isActive && it.editor != null
}
}

override fun create(editor: VimEditor, context: ExecutionContext): VimOutputPanel {
val panel = ExOutputModel(editor.ij)
val panel = ExOutputModel(WeakReference(editor.ij))
activeOutputPanel = panel
return panel
}
Expand Down

0 comments on commit c0419d6

Please sign in to comment.