Can I modify `*.groovy` file icon when certain criteria are met?

Answered

Hello there.

I've been trying hard the whole day today to figure out a solution to my use case. No luck, that's why I'm asking the community here.

I am developing a plugin that adds inspections, templates and other stuff to support the Spock framework.

The plugin operates over Groovy files as Spock specifications are Groovy classes that extend the spock.lang.Specification class.

I wanted to change the standard Groovy file icons in the Project view to my plugin's icon only in case a Groovy class extends the spock.lang.Specification.

Is there a way to achieve that? A similar thing works for XML files where a Maven icon and validation rules are used for pom.xml. Another example is yaml files vs. application.yml that gets a Spring icon.

Registering a new fileType in the plugin.xml overrides icons for all Groovy files. Registering a fileTypeDetector did  nothing - the type detector was only called for gradlew and a few other files, but never for a *.groovy file.

I am out of ideas :( Any help would be greatly appreciated.

0
9 comments

Hej Pavel,

I think the IconProvider extension point (com.intellij.iconProvider) might be what you are looking for.

1

Hey Marco. Oh My God, you're a lifesaver.

This was literally a one-liner Kotlin function since there is a function to detect Spock Specifications in the Groovy plugin. Thanks so much!!

For future reference, this is how it looks:

<idea-plugin>
    <extensions defaultExtensionNs="com.intellij">
        <iconProvider implementation="spock.SpecificationIconProvider"/>
    </extensions>
</idea-plugin>

 

package spock

class SpecificationIconProvider : IconProvider() {
    override fun getIcon(psiElement: PsiElement, flags: Int): Icon? =
        if (psiElement is GrClassDefinition && psiElement.isSpockSpecification()) SpockIcon.specification else null
}
0

Don't you, by any chance, know if I can do something similar with File templates?

For instance, I have a Spock Specification.groovy.ft defined, but the template uses the Groovy icon in the Settings.

The only way I've found is to register my own spock fileType . This seems like an overkill though, since the plugin does not need its own type. Moreover, any file with a spock extension would get the Spock icon, which I do not want because such files are not valid Specifications anyway and the Spock framework will fail to execute them.

0

Hej Pavel,

great, I am glad that it works for you and that I could help.

I am not sure about the file template icon issue. My assumption is that it is simply derived from the ‘base type’ (inferred from the file extension specified for the file template). Thus, I too would think it would need a dedicated file type.

0

Thanks anyway, Marco. Will stick with the custom file type workaround for now.

Have a wonderful Christmas!

0

You are welcome, Pavel.

Have a wonderful Christmas too and a good start into 2024 (it can only get better, I guess ;-).

1

Hi Pavel,

Marco is right. The icon is determined based on the file type.

I think that changing the file type is an overkill in your case, and I suggest requesting a File Templates improvement that allows for defining custom icons: https://youtrack.jetbrains.com/issues/IDEA

0

Hi Karol,

Thanks for confirming.

I too think the custom type is an overkill. However, it is the only option so I have used it for now as a workaround. Anyway, will request an improvement as per your suggestion. Thanks!

0

Please sign in to leave a comment.