Skip to content

Commit

Permalink
feat: validate biome.json path config (#32)
Browse files Browse the repository at this point in the history
* feat: validate biome.json path config

* refactor: changed how to handle config extension validation
  • Loading branch information
victor-teles authored Feb 2, 2024
1 parent 14eb397 commit 64e056f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
16 changes: 11 additions & 5 deletions src/main/kotlin/com/github/biomejs/intellijbiome/BiomePackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ class BiomePackage(private val project: Project) {

val configPath: String?
get() {
val configPath = BiomeSettings.getInstance(project).configPath
val settings = BiomeSettings.getInstance(project)
val configurationMode = settings.configurationMode

if (configPath.isNotEmpty()) {
return configPath
return when (configurationMode) {
ConfigurationMode.DISABLED -> null
ConfigurationMode.AUTOMATIC -> null
ConfigurationMode.MANUAL -> BiomeSettings.getInstance(project).configPath
}

return null
}

fun versionNumber(): String? {
Expand Down Expand Up @@ -71,4 +72,9 @@ class BiomePackage(private val project: Project) {
return matchResult?.value
}.getOrNull()
}

companion object {
const val configName = "biome"
val configValidExtensions = listOf("json", "jsonc")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.biomejs.intellijbiome.listeners

import com.github.biomejs.intellijbiome.BiomePackage
import com.github.biomejs.intellijbiome.services.BiomeServerService
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
Expand All @@ -10,9 +11,9 @@ class BiomeConfigListener(val project: Project) : BulkFileListener {
override fun after(events: MutableList<out VFileEvent>) {
super.after(events)
events.forEach {
if (it.file?.name?.contains("biome.json") == true) {
if (it.file?.name?.contains(BiomePackage.configName) == true && BiomePackage.configValidExtensions.contains(
it.file?.extension)) {
val biomeServerService = project.service<BiomeServerService>()

biomeServerService.restartBiomeServer()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.github.biomejs.intellijbiome.settings

import com.github.biomejs.intellijbiome.BiomeBundle
import com.github.biomejs.intellijbiome.BiomePackage
import com.github.biomejs.intellijbiome.services.BiomeServerService
import com.intellij.ide.actionsOnSave.ActionsOnSaveConfigurable
import com.intellij.lang.javascript.JavaScriptBundle
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationNamesInfo
import com.intellij.openapi.components.service
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.observable.properties.ObservableMutableProperty
import com.intellij.openapi.observable.util.whenItemSelected
import com.intellij.openapi.options.BoundSearchableConfigurable
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogPanel
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.ContextHelpLabel
Expand All @@ -23,13 +26,13 @@ import com.intellij.ui.layout.not
import com.intellij.ui.layout.selected
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import java.io.File
import java.nio.file.FileSystems
import java.util.regex.PatternSyntaxException
import javax.swing.JCheckBox
import javax.swing.JRadioButton
import javax.swing.text.JTextComponent

private const val CONFIGURABLE_ID = "settings.biome"
private const val HELP_TOPIC = "reference.settings.biome"

class BiomeConfigurable(internal val project: Project) :
Expand Down Expand Up @@ -83,7 +86,7 @@ class BiomeConfigurable(internal val project: Project) :
"settings.javascript.linters.autodetect.configure.automatically.help.text",
ApplicationNamesInfo.getInstance().fullProductName,
displayName,
"biome.json"
"${BiomePackage.configName}.json"
)

val helpLabel = ContextHelpLabel.create(detectAutomaticallyHelpText)
Expand All @@ -107,14 +110,19 @@ class BiomeConfigurable(internal val project: Project) :
// Manual configuration row
// *********************
panel {
row(BiomeBundle.message("biome.path.label")) {
textFieldWithBrowseButton(BiomeBundle.message("biome.path.label")) { fileChosen(it) }
row(BiomeBundle.message("biome.path.executable")) {
textFieldWithBrowseButton(BiomeBundle.message("biome.path.executable")) { fileChosen(it) }
.bindText(settings::executablePath)
}.visibleIf(manualConfiguration.selected)

row(BiomeBundle.message("biome.config.path.label")) {
textFieldWithBrowseButton(BiomeBundle.message("biome.config.path.label")) { fileChosen(it) }
textFieldWithBrowseButton(
BiomeBundle.message("biome.config.path.label"),
project,
FileChooserDescriptorFactory.createSingleFileOrFolderDescriptor()
) { fileChosen(it) }
.bindText(settings::configPath)
.validationOnInput(validateConfigDir())
}.visibleIf(manualConfiguration.selected)
}

Expand All @@ -133,7 +141,6 @@ class BiomeConfigurable(internal val project: Project) :
.component
}.enabledIf(!disabledConfiguration.selected)


// *********************
// Format on save row
// *********************
Expand Down Expand Up @@ -201,6 +208,23 @@ class BiomeConfigurable(internal val project: Project) :
}
}

private fun validateConfigDir(): ValidationInfoBuilder.(TextFieldWithBrowseButton) -> ValidationInfo? =
{
val selected = File(it.text)

if (!selected.exists()) {
ValidationInfo(BiomeBundle.message("biome.configuration.file.not.found"), it)
} else {
if (!selected.name.contains(BiomePackage.configName) && BiomePackage.configValidExtensions.contains(
selected.extension
)
) {
ValidationInfo(BiomeBundle.message("biome.configuration.file.not.found"), it)
} else {
null
}
}
}

private fun fileChosen(file: VirtualFile): String {
return file.path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.lang.javascript.linter.GlobPatternUtil
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import java.io.File


@Service(Service.Level.PROJECT)
Expand All @@ -18,6 +19,11 @@ class BiomeSettings :
var configPath: String
get() = state.configPath ?: ""
set(value) {
val file = File(value)
if (file.isFile) {
state.configPath = file.parentFile.path
return
}
state.configPath = value
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/messages/BiomeBundle.properties
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
name=Biome
biome.settings.name=Biome
biome.path.label=Biome CLI Path
biome.config.path.label=Directory path of biome.json
biome.path.executable=Biome executable
biome.config.path.label=Path of biome.json
biome.language.server.not.found=Biome language server is not found, make sure you have @biomejs/biome installed.
biome.loading=Biome is loading...
biome.language.server.is.running=Biome server is running
biome.language.server.is.stopped=Biome server is stopped
biome.language.server.restarted=Biome server restarted
biome.interpreter.not.configured=Your node interpreter not configured.
biome.invalid.pattern=Invalid pattern
biome.configuration.file.not.found=Failed to locate biome configuration file
biome.run.format.for.files.label=Run biome for &files:
biome.run.format.on.save.label=Run format on &save
biome.run.safe.fixes.on.save.label=Run safe fixes on &save
Expand Down

0 comments on commit 64e056f

Please sign in to comment.