Restore scratch options after IDE restart

^KT-23441 Fixed
This commit is contained in:
Natalia Selezneva
2018-04-03 16:09:38 +03:00
parent 178dad3b4e
commit db2bcff390
3 changed files with 98 additions and 0 deletions
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.scratch.ui
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.kotlin.idea.core.util.fileAttribute
var VirtualFile.scratchPanelConfig: ScratchPanelConfig? by fileAttribute(
name = "kotlin-scratch-panel-config",
version = 1,
read = { ScratchPanelConfig(readBoolean(), readBoolean()) },
write = {
writeBoolean(it.isRepl)
writeBoolean(it.isMakeBeforeRun)
}
)
@@ -66,13 +66,28 @@ class ScratchTopPanel private constructor(val scratchFile: ScratchFile) : JPanel
isReplCheckbox = JCheckBox("Use REPL", false).customize()
add(isReplCheckbox)
isReplCheckbox.addItemListener {
scratchFile.getPsiFile()?.virtualFile?.apply {
scratchPanelConfig = (scratchPanelConfig ?: ScratchPanelConfig()).copy(isRepl = isReplCheckbox.isSelected)
}
}
add(JSeparator(SwingConstants.VERTICAL))
isMakeBeforeRunCheckbox = JCheckBox("Make before Run", false).customize()
add(isMakeBeforeRunCheckbox)
isMakeBeforeRunCheckbox.addItemListener {
scratchFile.getPsiFile()?.virtualFile?.apply {
scratchPanelConfig = (scratchPanelConfig ?: ScratchPanelConfig()).copy(isMakeBeforeRun = isMakeBeforeRunCheckbox.isSelected)
}
}
add(JSeparator(SwingConstants.VERTICAL))
(scratchFile.getPsiFile()?.virtualFile?.scratchPanelConfig ?: ScratchPanelConfig()).let {
isReplCheckbox.isSelected = it.isRepl
isMakeBeforeRunCheckbox.isSelected = it.isMakeBeforeRun
}
}
fun getModule(): Module? = moduleChooser.selectedModule
@@ -99,6 +114,11 @@ class ScratchTopPanel private constructor(val scratchFile: ScratchFile) : JPanel
isReplCheckbox.isSelected = isSelected
}
@TestOnly
fun setMakeBeforeRun(isSelected: Boolean) {
isMakeBeforeRunCheckbox.isSelected = isSelected
}
private fun JCheckBox.customize(): JCheckBox {
verticalTextPosition = SwingConstants.BOTTOM
return this
@@ -120,3 +140,5 @@ class ScratchTopPanel private constructor(val scratchFile: ScratchFile) : JPanel
}
}
}
data class ScratchPanelConfig(val isRepl: Boolean = false, val isMakeBeforeRun: Boolean = false)
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.scratch
import com.intellij.ide.scratch.ScratchFileService
import com.intellij.ide.scratch.ScratchRootType
import junit.framework.Assert
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.scratch.ui.ScratchTopPanel
import javax.swing.JCheckBox
import kotlin.reflect.full.createType
import kotlin.reflect.full.declaredMemberProperties
class ScratchOptionsSaveTest : AbstractScratchRunActionTest() {
fun testOptionsSaveOnClosingFile() {
val fileText = "val a = 1"
val scratchFile = ScratchRootType.getInstance().createScratchFile(
project,
"scratch_1.kts",
KotlinLanguage.INSTANCE,
fileText,
ScratchFileService.Option.create_if_missing
) ?: error("Couldn't create scratch file")
myManager.openFile(scratchFile, true)
val (_, scratchPanelBeforeClosingFile) = getEditorWithScratchPanel(myManager, scratchFile) ?: error("Couldn't find scratch panel")
Assert.assertEquals(
"This test checks that checkbox options are restored after file closing. Not all checkboxes are checked in this test",
2,
ScratchTopPanel::class.declaredMemberProperties.filter { it.returnType == JCheckBox::class.createType() }.size
)
val newIsReplValue = !scratchPanelBeforeClosingFile.isRepl()
val newIsMakeBeforeRunValue = !scratchPanelBeforeClosingFile.isMakeBeforeRun()
scratchPanelBeforeClosingFile.setReplMode(newIsReplValue)
scratchPanelBeforeClosingFile.setMakeBeforeRun(newIsMakeBeforeRunValue)
myManager.closeFile(scratchFile)
myManager.openFile(scratchFile, true)
val (_, scratchPanelAfterClosingFile) = getEditorWithScratchPanel(myManager, scratchFile) ?: error("Couldn't find scratch panel")
Assert.assertEquals("Wrong value for isRepl checkbox", newIsReplValue, scratchPanelAfterClosingFile.isRepl())
Assert.assertEquals(
"Wrong value for isMakeBeforeRun checkbox",
newIsMakeBeforeRunValue,
scratchPanelAfterClosingFile.isMakeBeforeRun()
)
}
}