diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchFileAttributes.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchFileAttributes.kt new file mode 100644 index 00000000000..8111243b95a --- /dev/null +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchFileAttributes.kt @@ -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) + } +) \ No newline at end of file diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchTopPanel.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchTopPanel.kt index 1c44fdf06de..e14a6759b9a 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchTopPanel.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchTopPanel.kt @@ -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) diff --git a/idea/tests/org/jetbrains/kotlin/idea/scratch/ScratchOptionsSaveTest.kt b/idea/tests/org/jetbrains/kotlin/idea/scratch/ScratchOptionsSaveTest.kt new file mode 100644 index 00000000000..08292f9ef46 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/scratch/ScratchOptionsSaveTest.kt @@ -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() + ) + } +} \ No newline at end of file