Refactoring: move scratch options to ScratchFile from ScratchPanel
This commit is contained in:
@@ -23,6 +23,7 @@ import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.idea.scratch.ui.scratchFileOptions
|
||||
|
||||
abstract class ScratchFile(val project: Project, val editor: TextEditor) {
|
||||
fun getExpressions(): List<ScratchExpression> = runReadAction {
|
||||
@@ -37,7 +38,23 @@ abstract class ScratchFile(val project: Project, val editor: TextEditor) {
|
||||
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>
|
||||
}
|
||||
|
||||
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 psiFile = scratchFile.getPsiFile() ?: return
|
||||
|
||||
val isMakeBeforeRun = scratchPanel.isMakeBeforeRun()
|
||||
val isRepl = scratchPanel.isRepl()
|
||||
val isMakeBeforeRun = scratchFile.options.isMakeBeforeRun
|
||||
val isRepl = scratchFile.options.isRepl
|
||||
|
||||
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 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",
|
||||
version = 1,
|
||||
read = { ScratchPanelConfig(readBoolean(), readBoolean()) },
|
||||
read = { ScratchFileOptions(readBoolean(), readBoolean()) },
|
||||
write = {
|
||||
writeBoolean(it.isRepl)
|
||||
writeBoolean(it.isMakeBeforeRun)
|
||||
|
||||
@@ -74,27 +74,27 @@ class ScratchTopPanel private constructor(val scratchFile: ScratchFile) : JPanel
|
||||
|
||||
add(JSeparator(SwingConstants.VERTICAL))
|
||||
|
||||
isReplCheckbox = JCheckBox("Use REPL", false)
|
||||
isReplCheckbox = JCheckBox("Use REPL")
|
||||
add(isReplCheckbox)
|
||||
isReplCheckbox.addItemListener {
|
||||
scratchFile.getPsiFile()?.virtualFile?.apply {
|
||||
scratchPanelConfig = (scratchPanelConfig ?: ScratchPanelConfig()).copy(isRepl = isReplCheckbox.isSelected)
|
||||
scratchFile.saveOptions {
|
||||
copy(isRepl = isReplCheckbox.isSelected)
|
||||
}
|
||||
}
|
||||
|
||||
add(JSeparator(SwingConstants.VERTICAL))
|
||||
|
||||
isMakeBeforeRunCheckbox = JCheckBox("Make before Run", false)
|
||||
isMakeBeforeRunCheckbox = JCheckBox("Make before Run")
|
||||
add(isMakeBeforeRunCheckbox)
|
||||
isMakeBeforeRunCheckbox.addItemListener {
|
||||
scratchFile.getPsiFile()?.virtualFile?.apply {
|
||||
scratchPanelConfig = (scratchPanelConfig ?: ScratchPanelConfig()).copy(isMakeBeforeRun = isMakeBeforeRunCheckbox.isSelected)
|
||||
scratchFile.saveOptions {
|
||||
copy(isMakeBeforeRun = isMakeBeforeRunCheckbox.isSelected)
|
||||
}
|
||||
}
|
||||
|
||||
add(JSeparator(SwingConstants.VERTICAL))
|
||||
|
||||
(scratchFile.getPsiFile()?.virtualFile?.scratchPanelConfig ?: ScratchPanelConfig()).let {
|
||||
scratchFile.options.let {
|
||||
isReplCheckbox.isSelected = it.isRepl
|
||||
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
|
||||
fun setReplMode(isSelected: Boolean) {
|
||||
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")
|
||||
|
||||
val (editor, scratchPanel) = getEditorWithScratchPanel(myManager, scratchFile)?: error("Couldn't find scratch panel")
|
||||
scratchPanel.setReplMode(isRepl)
|
||||
scratchPanel.scratchFile.saveOptions {
|
||||
copy(isRepl = isRepl)
|
||||
}
|
||||
|
||||
launchScratch(scratchFile)
|
||||
|
||||
|
||||
@@ -37,8 +37,8 @@ class ScratchOptionsSaveTest : AbstractScratchRunActionTest() {
|
||||
ScratchTopPanel::class.declaredMemberProperties.filter { it.returnType == JCheckBox::class.createType() }.size
|
||||
)
|
||||
|
||||
val newIsReplValue = !scratchPanelBeforeClosingFile.isRepl()
|
||||
val newIsMakeBeforeRunValue = !scratchPanelBeforeClosingFile.isMakeBeforeRun()
|
||||
val newIsReplValue = !scratchPanelBeforeClosingFile.scratchFile.options.isRepl
|
||||
val newIsMakeBeforeRunValue = !scratchPanelBeforeClosingFile.scratchFile.options.isMakeBeforeRun
|
||||
scratchPanelBeforeClosingFile.setReplMode(newIsReplValue)
|
||||
scratchPanelBeforeClosingFile.setMakeBeforeRun(newIsMakeBeforeRunValue)
|
||||
|
||||
@@ -47,11 +47,11 @@ class ScratchOptionsSaveTest : AbstractScratchRunActionTest() {
|
||||
|
||||
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(
|
||||
"Wrong value for isMakeBeforeRun checkbox",
|
||||
newIsMakeBeforeRunValue,
|
||||
scratchPanelAfterClosingFile.isMakeBeforeRun()
|
||||
scratchPanelAfterClosingFile.scratchFile.options.isMakeBeforeRun
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user