Skip to content

Commit 1bd4c7d

Browse files
committed
VIM-4120 extract base classes for output panel and model
1 parent a7ca65c commit 1bd4c7d

File tree

10 files changed

+502
-645
lines changed

10 files changed

+502
-645
lines changed

src/main/java/com/maddyhome/idea/vim/ex/ExOutputModel.kt

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,41 @@
77
*/
88
package com.maddyhome.idea.vim.ex
99

10-
import com.intellij.openapi.application.ApplicationManager
1110
import com.intellij.openapi.editor.Editor
12-
import com.maddyhome.idea.vim.api.VimOutputPanelBase
13-
import com.maddyhome.idea.vim.api.injector
1411
import com.maddyhome.idea.vim.helper.vimExOutput
1512
import com.maddyhome.idea.vim.ui.ExOutputPanel
13+
import com.maddyhome.idea.vim.ui.IjOutputModelBase
14+
import java.awt.event.KeyEvent
1615
import java.lang.ref.WeakReference
1716
import javax.swing.KeyStroke
1817

1918
// TODO: We need a nicer way to handle output, especially wrt testing, appending + clearing
20-
class ExOutputModel(private val myEditor: WeakReference<Editor>) : VimOutputPanelBase() {
21-
private var isActiveInTestMode = false
19+
class ExOutputModel(editor: WeakReference<Editor>) : IjOutputModelBase<ExOutputPanel>(editor) {
2220

23-
val editor get() = myEditor.get()
21+
override fun onBeforeShow() {
22+
editor?.let { it.vimExOutput = this }
23+
}
2424

25-
override val isActive: Boolean
26-
get() = if (!ApplicationManager.getApplication().isUnitTestMode) {
27-
editor?.let { ExOutputPanel.getNullablePanel(it) }?.myActive ?: false
28-
} else {
29-
isActiveInTestMode
25+
override fun handleKey(key: KeyStroke) {
26+
if (atEnd) {
27+
closeWithKey(key)
28+
return
3029
}
3130

32-
override fun addText(text: String, isNewLine: Boolean) {
33-
if (this.text.isNotEmpty() && isNewLine) this.text += "\n$text" else this.text += text
34-
}
35-
36-
override fun show() {
37-
if (editor == null) return
38-
val currentPanel = injector.outputPanel.getCurrentOutputPanel()
39-
if (currentPanel != null && currentPanel != this) currentPanel.close()
40-
41-
editor!!.vimExOutput = this
42-
val exOutputPanel = ExOutputPanel.getInstance(editor!!)
43-
if (!exOutputPanel.myActive) {
44-
if (ApplicationManager.getApplication().isUnitTestMode) {
45-
isActiveInTestMode = true
46-
} else {
47-
exOutputPanel.activate()
31+
when (key.keyChar) {
32+
' ' -> scrollPage()
33+
'd' -> scrollHalfPage()
34+
'q', '\u001b' -> close()
35+
'\n' -> scrollLine()
36+
KeyEvent.CHAR_UNDEFINED -> {
37+
when (key.keyCode) {
38+
KeyEvent.VK_ENTER -> scrollLine()
39+
KeyEvent.VK_ESCAPE -> close()
40+
else -> onBadKey()
41+
}
4842
}
43+
44+
else -> onBadKey()
4945
}
5046
}
5147

@@ -67,32 +63,6 @@ class ExOutputModel(private val myEditor: WeakReference<Editor>) : VimOutputPane
6763
panel.scrollLine()
6864
}
6965

70-
override fun setContent(text: String) {
71-
this.text = text
72-
}
73-
74-
override fun clearText() {
75-
text = ""
76-
}
77-
78-
override var text: String = ""
79-
get() = if (!ApplicationManager.getApplication().isUnitTestMode) {
80-
editor?.let { ExOutputPanel.getInstance(it).text } ?: ""
81-
} else {
82-
// ExOutputPanel always returns a non-null string
83-
field
84-
}
85-
set(value) {
86-
// ExOutputPanel will strip a trailing newline. We'll do it now so that tests have the same behaviour. We also
87-
// never pass null to ExOutputPanel, but we do store it for tests, so we know if we're active or not
88-
val newValue = value.removeSuffix("\n")
89-
if (!ApplicationManager.getApplication().isUnitTestMode) {
90-
editor?.let { ExOutputPanel.getInstance(it).text = newValue }
91-
} else {
92-
field = newValue
93-
isActiveInTestMode = newValue.isNotEmpty()
94-
}
95-
}
9666
override var label: String
9767
get() {
9868
val notNullEditor = editor ?: return ""
@@ -113,31 +83,25 @@ class ExOutputModel(private val myEditor: WeakReference<Editor>) : VimOutputPane
11383
text = ""
11484
}
11585

116-
override val atEnd: Boolean
86+
private val atEnd: Boolean
11787
get() {
11888
val notNullEditor = editor ?: return false
11989
val panel = ExOutputPanel.getNullablePanel(notNullEditor) ?: return false
12090
return panel.isAtEnd
12191
}
12292

123-
override fun onBadKey() {
93+
private fun onBadKey() {
12494
val notNullEditor = editor ?: return
12595
val panel = ExOutputPanel.getNullablePanel(notNullEditor) ?: return
12696
panel.onBadKey()
12797
}
12898

129-
override fun close(key: KeyStroke?) {
130-
val notNullEditor = editor ?: return
131-
val panel = ExOutputPanel.getNullablePanel(notNullEditor) ?: return
132-
panel.close(key)
99+
override fun getNullablePanel(): ExOutputPanel? {
100+
return editor?.let { ExOutputPanel.getNullablePanel(it) }
133101
}
134102

135-
override fun close() {
136-
if (!ApplicationManager.getApplication().isUnitTestMode) {
137-
editor?.let { ExOutputPanel.getInstance(it).close() }
138-
} else {
139-
isActiveInTestMode = false
140-
}
103+
override fun getOrCreatePanel(): ExOutputPanel {
104+
return ExOutputPanel.getInstance(editor!!)
141105
}
142106

143107
companion object {

0 commit comments

Comments
 (0)