Refactoring: move scratch options to ScratchFile from ScratchPanel

This commit is contained in:
Natalia Selezneva
2018-12-12 12:25:14 +03:00
parent a408711f37
commit d389c9fd85
6 changed files with 38 additions and 23 deletions
@@ -23,6 +23,7 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.idea.scratch.ui.scratchFileOptions
abstract class ScratchFile(val project: Project, val editor: TextEditor) { abstract class ScratchFile(val project: Project, val editor: TextEditor) {
fun getExpressions(): List<ScratchExpression> = runReadAction { fun getExpressions(): List<ScratchExpression> = runReadAction {
@@ -37,7 +38,23 @@ abstract class ScratchFile(val project: Project, val editor: TextEditor) {
return editor.getScratchPanel()?.getModule() return editor.getScratchPanel()?.getModule()
} }
val options: ScratchFileOptions
get() = getPsiFile()?.virtualFile?.scratchFileOptions ?: ScratchFileOptions()
fun saveOptions(update: ScratchFileOptions.() -> ScratchFileOptions) {
val virtualFile = getPsiFile()?.virtualFile ?: return
with(virtualFile) {
val configToUpdate = scratchFileOptions ?: ScratchFileOptions()
scratchFileOptions = configToUpdate.update()
}
}
abstract fun getExpressions(psiFile: PsiFile): List<ScratchExpression> abstract fun getExpressions(psiFile: PsiFile): List<ScratchExpression>
} }
data class ScratchExpression(val element: PsiElement, val lineStart: Int, val lineEnd: Int = lineStart) data class ScratchExpression(val element: PsiElement, val lineStart: Int, val lineEnd: Int = lineStart)
data class ScratchFileOptions(
val isRepl: Boolean = false,
val isMakeBeforeRun: Boolean = false
)
@@ -45,8 +45,8 @@ class RunScratchAction : ScratchAction(
val scratchFile = scratchPanel.scratchFile val scratchFile = scratchPanel.scratchFile
val psiFile = scratchFile.getPsiFile() ?: return val psiFile = scratchFile.getPsiFile() ?: return
val isMakeBeforeRun = scratchPanel.isMakeBeforeRun() val isMakeBeforeRun = scratchFile.options.isMakeBeforeRun
val isRepl = scratchPanel.isRepl() val isRepl = scratchFile.options.isRepl
val provider = ScratchFileLanguageProvider.get(psiFile.language) ?: return val provider = ScratchFileLanguageProvider.get(psiFile.language) ?: return
@@ -7,11 +7,12 @@ package org.jetbrains.kotlin.idea.scratch.ui
import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.kotlin.idea.core.util.cachedFileAttribute import org.jetbrains.kotlin.idea.core.util.cachedFileAttribute
import org.jetbrains.kotlin.idea.scratch.ScratchFileOptions
var VirtualFile.scratchPanelConfig: ScratchPanelConfig? by cachedFileAttribute( var VirtualFile.scratchFileOptions: ScratchFileOptions? by cachedFileAttribute(
name = "kotlin-scratch-panel-config", name = "kotlin-scratch-panel-config",
version = 1, version = 1,
read = { ScratchPanelConfig(readBoolean(), readBoolean()) }, read = { ScratchFileOptions(readBoolean(), readBoolean()) },
write = { write = {
writeBoolean(it.isRepl) writeBoolean(it.isRepl)
writeBoolean(it.isMakeBeforeRun) writeBoolean(it.isMakeBeforeRun)
@@ -74,27 +74,27 @@ class ScratchTopPanel private constructor(val scratchFile: ScratchFile) : JPanel
add(JSeparator(SwingConstants.VERTICAL)) add(JSeparator(SwingConstants.VERTICAL))
isReplCheckbox = JCheckBox("Use REPL", false) isReplCheckbox = JCheckBox("Use REPL")
add(isReplCheckbox) add(isReplCheckbox)
isReplCheckbox.addItemListener { isReplCheckbox.addItemListener {
scratchFile.getPsiFile()?.virtualFile?.apply { scratchFile.saveOptions {
scratchPanelConfig = (scratchPanelConfig ?: ScratchPanelConfig()).copy(isRepl = isReplCheckbox.isSelected) copy(isRepl = isReplCheckbox.isSelected)
} }
} }
add(JSeparator(SwingConstants.VERTICAL)) add(JSeparator(SwingConstants.VERTICAL))
isMakeBeforeRunCheckbox = JCheckBox("Make before Run", false) isMakeBeforeRunCheckbox = JCheckBox("Make before Run")
add(isMakeBeforeRunCheckbox) add(isMakeBeforeRunCheckbox)
isMakeBeforeRunCheckbox.addItemListener { isMakeBeforeRunCheckbox.addItemListener {
scratchFile.getPsiFile()?.virtualFile?.apply { scratchFile.saveOptions {
scratchPanelConfig = (scratchPanelConfig ?: ScratchPanelConfig()).copy(isMakeBeforeRun = isMakeBeforeRunCheckbox.isSelected) copy(isMakeBeforeRun = isMakeBeforeRunCheckbox.isSelected)
} }
} }
add(JSeparator(SwingConstants.VERTICAL)) add(JSeparator(SwingConstants.VERTICAL))
(scratchFile.getPsiFile()?.virtualFile?.scratchPanelConfig ?: ScratchPanelConfig()).let { scratchFile.options.let {
isReplCheckbox.isSelected = it.isRepl isReplCheckbox.isSelected = it.isRepl
isMakeBeforeRunCheckbox.isSelected = it.isMakeBeforeRun isMakeBeforeRunCheckbox.isSelected = it.isMakeBeforeRun
} }
@@ -116,9 +116,6 @@ class ScratchTopPanel private constructor(val scratchFile: ScratchFile) : JPanel
} }
} }
fun isRepl() = isReplCheckbox.isSelected
fun isMakeBeforeRun() = isMakeBeforeRunCheckbox.isSelected
@TestOnly @TestOnly
fun setReplMode(isSelected: Boolean) { fun setReplMode(isSelected: Boolean) {
isReplCheckbox.isSelected = isSelected isReplCheckbox.isSelected = isSelected
@@ -153,6 +150,4 @@ class ScratchTopPanel private constructor(val scratchFile: ScratchFile) : JPanel
}) })
} }
} }
} }
data class ScratchPanelConfig(val isRepl: Boolean = false, val isMakeBeforeRun: Boolean = false)
@@ -112,7 +112,9 @@ abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() {
if (!KotlinHighlightingUtil.shouldHighlight(psiFile)) error("Highlighting for scratch file is switched off") if (!KotlinHighlightingUtil.shouldHighlight(psiFile)) error("Highlighting for scratch file is switched off")
val (editor, scratchPanel) = getEditorWithScratchPanel(myManager, scratchFile)?: error("Couldn't find scratch panel") val (editor, scratchPanel) = getEditorWithScratchPanel(myManager, scratchFile)?: error("Couldn't find scratch panel")
scratchPanel.setReplMode(isRepl) scratchPanel.scratchFile.saveOptions {
copy(isRepl = isRepl)
}
launchScratch(scratchFile) launchScratch(scratchFile)
@@ -37,8 +37,8 @@ class ScratchOptionsSaveTest : AbstractScratchRunActionTest() {
ScratchTopPanel::class.declaredMemberProperties.filter { it.returnType == JCheckBox::class.createType() }.size ScratchTopPanel::class.declaredMemberProperties.filter { it.returnType == JCheckBox::class.createType() }.size
) )
val newIsReplValue = !scratchPanelBeforeClosingFile.isRepl() val newIsReplValue = !scratchPanelBeforeClosingFile.scratchFile.options.isRepl
val newIsMakeBeforeRunValue = !scratchPanelBeforeClosingFile.isMakeBeforeRun() val newIsMakeBeforeRunValue = !scratchPanelBeforeClosingFile.scratchFile.options.isMakeBeforeRun
scratchPanelBeforeClosingFile.setReplMode(newIsReplValue) scratchPanelBeforeClosingFile.setReplMode(newIsReplValue)
scratchPanelBeforeClosingFile.setMakeBeforeRun(newIsMakeBeforeRunValue) scratchPanelBeforeClosingFile.setMakeBeforeRun(newIsMakeBeforeRunValue)
@@ -47,11 +47,11 @@ class ScratchOptionsSaveTest : AbstractScratchRunActionTest() {
val (_, scratchPanelAfterClosingFile) = getEditorWithScratchPanel(myManager, scratchFile) ?: error("Couldn't find scratch panel") 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 isRepl checkbox", newIsReplValue, scratchPanelAfterClosingFile.scratchFile.options.isRepl)
Assert.assertEquals( Assert.assertEquals(
"Wrong value for isMakeBeforeRun checkbox", "Wrong value for isMakeBeforeRun checkbox",
newIsMakeBeforeRunValue, newIsMakeBeforeRunValue,
scratchPanelAfterClosingFile.isMakeBeforeRun() scratchPanelAfterClosingFile.scratchFile.options.isMakeBeforeRun
) )
} }
} }