Skip to content

Commit

Permalink
Allow users to set config file through the run config
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregg Van Hove committed Feb 16, 2018
1 parent 65981c9 commit a3513fc
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/io/pivotal/intellij/jasmine/JasmineConfigurationEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ package io.pivotal.intellij.jasmine
import com.intellij.execution.configuration.EnvironmentVariablesTextFieldWithBrowseButton
import com.intellij.javascript.nodejs.interpreter.NodeJsInterpreterField
import com.intellij.javascript.nodejs.util.NodePackageField
import com.intellij.json.JsonFileType
import com.intellij.lang.javascript.library.JSLibraryUtil
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.options.SettingsEditor
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.search.FileTypeIndex
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.ProjectScope
import com.intellij.ui.RawCommandLineEditor
import com.intellij.ui.TextFieldWithHistoryWithBrowseButton
import com.intellij.ui.components.fields.ExpandableTextField
import com.intellij.util.ui.ComponentWithEmptyText
import com.intellij.util.ui.FormBuilder
Expand All @@ -24,6 +31,8 @@ class JasmineConfigurationEditor(private var project: Project) : SettingsEditor<
private var envVars: EnvironmentVariablesTextFieldWithBrowseButton = EnvironmentVariablesTextFieldWithBrowseButton()
private var jasminePackageField: NodePackageField = NodePackageField(nodeJsInterpreterField, "jasmine")
private var jasmineOptionsField = createJasmineOptionsField()
private var jasmineConfigFileField = createJasmineConfigFileField()

private var rootForm: JPanel

init {
Expand All @@ -35,6 +44,7 @@ class JasmineConfigurationEditor(private var project: Project) : SettingsEditor<
.addLabeledComponent("&Working directory", workingDirectoryField)
.addLabeledComponent("&Environment variables", envVars)
.addLabeledComponent("&Jasmine package", jasminePackageField)
.addLabeledComponent("Jasmine &config file", jasmineConfigFileField)
.addLabeledComponent("E&xtra Jasmine options", jasmineOptionsField)
.panel
}
Expand All @@ -61,6 +71,43 @@ class JasmineConfigurationEditor(private var project: Project) : SettingsEditor<
return editor
}

private fun createJasmineConfigFileField(): TextFieldWithHistoryWithBrowseButton {
val fullField = TextFieldWithHistoryWithBrowseButton()
val innerField = fullField.childComponent
innerField.setHistorySize(-1)
innerField.setMinimumAndPreferredWidth(0)

SwingHelper.addHistoryOnExpansion(innerField) {
innerField.history = emptyList<String>()
listPossibleConfigFilesInProject().map { file ->
FileUtil.toSystemDependentName(file.path)
}.sorted()
}

SwingHelper.installFileCompletionAndBrowseDialog(
project,
fullField,
"Select override Jasmine configuration file",
FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor()
)

return fullField
}

private fun listPossibleConfigFilesInProject(): List<VirtualFile> {
val contentScope = ProjectScope.getContentScope(project)
val scope = contentScope.intersectWith(GlobalSearchScope.notScope(ProjectScope.getLibrariesScope(project)))
val jsonFileType = JsonFileType.INSTANCE

val files = FileTypeIndex.getFiles(jsonFileType, scope)

return files.filter { it != null && it.isValid && !it.isDirectory && isJasmineConfigFile(it.nameSequence) && !JSLibraryUtil.isProbableLibraryFile(it) }
}

private fun isJasmineConfigFile(filename: CharSequence): Boolean {
return filename.startsWith("jasmine", true)
}

override fun createEditor(): JComponent = rootForm

override fun applyEditorTo(config: JasmineRunConfiguration) {
Expand All @@ -69,6 +116,7 @@ class JasmineConfigurationEditor(private var project: Project) : SettingsEditor<
nodeOptions = nodeOptionsField.text,
workingDir = workingDirectoryField.text,
envData = envVars.data,
jasmineConfigFile = jasmineConfigFileField.text,
extraJasmineOptions = jasmineOptionsField.text)
config.setJasminePackage(jasminePackageField.selected)
}
Expand All @@ -80,6 +128,7 @@ class JasmineConfigurationEditor(private var project: Project) : SettingsEditor<
workingDirectoryField.text = FileUtil.toSystemDependentName(runSettings.workingDir)
envVars.data = runSettings.envData
jasminePackageField.selected = config.selectedJasminePackage()
jasmineConfigFileField.text = runSettings.jasmineConfigFile
jasmineOptionsField.text = runSettings.extraJasmineOptions
}
}

0 comments on commit a3513fc

Please sign in to comment.