Skip to content

Commit

Permalink
feat: add support for Biome config icons and improve save actions (#119)
Browse files Browse the repository at this point in the history
Implement an icon provider to display Biome-specific icons for config files. Enhance "Actions on Save" functionality with handlers for formatting, safe fixes, and unsafe fixes.
  • Loading branch information
denbezrukov authored Jan 10, 2025
1 parent 7162e61 commit ddd0222
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.biomejs.intellijbiome

import com.intellij.ide.IconProvider
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import javax.swing.Icon

class BiomeConfigIconProvider : IconProvider() {
override fun getIcon(element: PsiElement, flags: Int): Icon? {
val file = element as? PsiFile ?: return null
if (!file.isValid || file.isDirectory) return null
val virtualFile = file.virtualFile ?: return null

// Check if the file is a valid Biome config file
if (virtualFile.name.contains(BiomePackage.configName) &&
BiomePackage.configValidExtensions.contains(virtualFile.extension)
) {
return BiomeIcons.BiomeIcon
}

return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BiomeConfigurable(internal val project: Project) :

lateinit var runForFilesField: JBTextField

private lateinit var disabledConfiguration: JRadioButton
lateinit var disabledConfiguration: JRadioButton
private lateinit var automaticConfiguration: JRadioButton
private lateinit var manualConfiguration: JRadioButton
override fun createPanel(): DialogPanel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class BiomeOnSaveApplySafeFixesActionInfo(actionOnSaveContext: ActionOnSaveConte
override fun getActionOnSaveName() =
BiomeBundle.message("biome.run.safe.fixes.on.save.checkbox.on.actions.on.save.page")

override fun isApplicableAccordingToStoredState(): Boolean =
BiomeSettings.getInstance(project).configurationMode != ConfigurationMode.DISABLED

override fun isApplicableAccordingToUiState(configurable: BiomeConfigurable): Boolean =
!configurable.disabledConfiguration.isSelected

override fun isActionOnSaveEnabledAccordingToStoredState() = BiomeSettings.getInstance(project).applySafeFixesOnSave

override fun isActionOnSaveEnabledAccordingToUiState(configurable: BiomeConfigurable) =
Expand All @@ -27,6 +33,8 @@ class BiomeOnSaveApplySafeFixesActionInfo(actionOnSaveContext: ActionOnSaveConte
}

override fun getCommentAccordingToUiState(configurable: BiomeConfigurable): ActionOnSaveComment? {
if (!isSaveActionApplicable) return ActionOnSaveComment.info(BiomeBundle.message("biome.on.save.comment.disabled"))

val biomePackage = BiomePackage(project)
val version = runWithModalProgressBlocking(project, BiomeBundle.message("biome.version")) {
biomePackage.versionNumber()
Expand All @@ -35,6 +43,8 @@ class BiomeOnSaveApplySafeFixesActionInfo(actionOnSaveContext: ActionOnSaveConte
}

override fun getCommentAccordingToStoredState(): ActionOnSaveComment? {
if (!isSaveActionApplicable) return ActionOnSaveComment.info(BiomeBundle.message("biome.on.save.comment.disabled"))

val biomePackage = BiomePackage(project)
val settings = BiomeSettings.getInstance(project)
val version = runWithModalProgressBlocking(project, BiomeBundle.message("biome.version")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class BiomeOnSaveApplyUnsafeFixesActionInfo(actionOnSaveContext: ActionOnSaveCon
override fun getActionOnSaveName() =
BiomeBundle.message("biome.run.unsafe.fixes.on.save.checkbox.on.actions.on.save.page")

override fun isApplicableAccordingToStoredState(): Boolean =
BiomeSettings.getInstance(project).configurationMode != ConfigurationMode.DISABLED

override fun isApplicableAccordingToUiState(configurable: BiomeConfigurable): Boolean =
!configurable.disabledConfiguration.isSelected

override fun isActionOnSaveEnabledAccordingToStoredState() =
BiomeSettings.getInstance(project).applyUnsafeFixesOnSave

Expand All @@ -29,6 +35,8 @@ class BiomeOnSaveApplyUnsafeFixesActionInfo(actionOnSaveContext: ActionOnSaveCon
}

override fun getCommentAccordingToUiState(configurable: BiomeConfigurable): ActionOnSaveComment? {
if (!isSaveActionApplicable) return ActionOnSaveComment.info(BiomeBundle.message("biome.on.save.comment.disabled"))

val biomePackage = BiomePackage(project)
val version = runWithModalProgressBlocking(project, BiomeBundle.message("biome.version")) {
biomePackage.versionNumber()
Expand All @@ -37,6 +45,8 @@ class BiomeOnSaveApplyUnsafeFixesActionInfo(actionOnSaveContext: ActionOnSaveCon
}

override fun getCommentAccordingToStoredState(): ActionOnSaveComment? {
if (!isSaveActionApplicable) return ActionOnSaveComment.info(BiomeBundle.message("biome.on.save.comment.disabled"))

val biomePackage = BiomePackage(project)
val settings = BiomeSettings.getInstance(project)
val version = runWithModalProgressBlocking(project, BiomeBundle.message("biome.version")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.github.biomejs.intellijbiome.settings

import com.github.biomejs.intellijbiome.BiomeBundle
import com.github.biomejs.intellijbiome.BiomePackage
import com.intellij.ide.actionsOnSave.ActionOnSaveBackedByOwnConfigurable
import com.intellij.ide.actionsOnSave.ActionOnSaveComment
import com.intellij.ide.actionsOnSave.ActionOnSaveContext
import com.intellij.platform.ide.progress.runWithModalProgressBlocking

class BiomeOnSaveFormatActionInfo(actionOnSaveContext: ActionOnSaveContext) :
ActionOnSaveBackedByOwnConfigurable<BiomeConfigurable>(
actionOnSaveContext,
BiomeConfigurable.CONFIGURABLE_ID,
BiomeConfigurable::class.java
) {

override fun getActionOnSaveName() =
BiomeBundle.message("biome.run.format.on.save.checkbox.on.actions.on.save.page")

override fun isApplicableAccordingToStoredState(): Boolean =
BiomeSettings.getInstance(project).configurationMode != ConfigurationMode.DISABLED

override fun isActionOnSaveEnabledAccordingToStoredState() = BiomeSettings.getInstance(project).formatOnSave

override fun isApplicableAccordingToUiState(configurable: BiomeConfigurable): Boolean =
!configurable.disabledConfiguration.isSelected

override fun isActionOnSaveEnabledAccordingToUiState(configurable: BiomeConfigurable) =
configurable.runFormatOnSaveCheckBox.isSelected

override fun setActionOnSaveEnabled(configurable: BiomeConfigurable, enabled: Boolean) {
configurable.runFormatOnSaveCheckBox.isSelected = enabled
}

override fun getActionLinks() = listOf(createGoToPageInSettingsLink(BiomeConfigurable.CONFIGURABLE_ID))

override fun getCommentAccordingToUiState(configurable: BiomeConfigurable): ActionOnSaveComment? {
if (!isSaveActionApplicable) return ActionOnSaveComment.info(BiomeBundle.message("biome.on.save.comment.disabled"))

val biomePackage = BiomePackage(project)
val version = runWithModalProgressBlocking(project, BiomeBundle.message("biome.version")) {
biomePackage.versionNumber()
}
return ActionInfo.defaultComment(version, configurable.runForFilesField.text.trim(), isActionOnSaveEnabled)
}

override fun getCommentAccordingToStoredState(): ActionOnSaveComment? {
if (!isSaveActionApplicable) return ActionOnSaveComment.info(BiomeBundle.message("biome.on.save.comment.disabled"))

val biomePackage = BiomePackage(project)
val settings = BiomeSettings.getInstance(project)
val version = runWithModalProgressBlocking(project, BiomeBundle.message("biome.version")) {
biomePackage.versionNumber()
}

return ActionInfo.defaultComment(version, settings.filePattern, isActionOnSaveEnabled)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.ide.actionsOnSave.ActionOnSaveInfoProvider
class BiomeOnSaveInfoProvider : ActionOnSaveInfoProvider() {
override fun getActionOnSaveInfos(context: ActionOnSaveContext):
List<ActionOnSaveInfo> = listOf(
BiomeOnSaveFormatActionInfo(context),
BiomeOnSaveApplySafeFixesActionInfo(context),
BiomeOnSaveApplyUnsafeFixesActionInfo(context)
)
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<notificationGroup id="Biome" displayType="BALLOON"/>
<platform.lsp.serverSupportProvider
implementation="com.github.biomejs.intellijbiome.lsp.BiomeLspServerSupportProvider"/>
<iconProvider implementation="com.github.biomejs.intellijbiome.BiomeConfigIconProvider"/>
<projectConfigurable id="Settings.Biome"
bundle="messages.BiomeBundle"
groupId="language"
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages/BiomeBundle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name=Biome
biome.settings.name=Biome
biome.on.save.comment.disabled=Biome integration disabled
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.
Expand Down

0 comments on commit ddd0222

Please sign in to comment.