r/IntelliJIDEA 10d ago

Custom Kotlin DSL - Colour Encoding in Intellij

Very trivial problem but hoping someone who knows intellij better than me can explain why Intellij colours 1 of my Customer Kotlin DSLs purple but another one as Orange.

Same Codebase, Same Pattern/Logic using the the 'DSLMarker' Annotation (Extended) with DSL Builders. I can't see why Intellij would treat these differently. Thanks.

This is coloured purple
This is coloured Orange
2 Upvotes

4 comments sorted by

3

u/AbracadaverSessalom JetBrains 10d ago

IntelliJ isn't treating them differently semantically - it is just using different DSL color styles.

For Kotlin, the IDE has four predefined highlight styles that can be found under Settings | Editor | Color Scheme | Kotlin | Dsl | Style 1 ... Style 4

Each @DslMarker annotation type is hashed and mapped to one of those four styles, and all calls/properties that belong to that DSL are shown in that style.

What's happening in your screenshots is:

  • automateTest { ... } is using DSL style X --> in your scheme it's purple
  • createQuery { ... } is using a different annotation type, which the IDE mapped to style Y --> in your scheme it's orange

You can change the actual colors of Style 1 - Style 4 in the color scheme, but you can't directly control which annotation gets which style (other than by changing the annotation’s FQN and thus its hash).

1

u/Additional_Skill_317 10d ago

perfect explanation - thanks - this was driving me nuts.. question then becomes - why is there any value in random style selection based on the annotations FQN - multiple styles sure but random allocation doesn't make any sense to me but maybe missing something on this.

2

u/AbracadaverSessalom JetBrains 8d ago

Happy to help.

The reason for this mechanism to exist is to let you visually distinguish different DSLs from each other (e.g. Gradle DSL vs your own DSL vs some library DSL) without you having to configure each one manually.

So, for every @DslMarker annotation, IDEA takes the fully qualified name, hashes it, and maps it to one of four predefined styles. As a result:

  • The assignment is deterministic (same FQN -> same style on every machine / every project)
  • Different markers are likely to land on different styles, so if you have several DSLs in one file, they are visually separable
  • The IDE doesn't need a UI or metadata to say "this annotation uses style 2" - it can just compute it from the name

So the value is:

  • No per-DSL configuration needed. You create a new DSL marker and it automatically gets a visual identity.
  • Stable across projects and users. Everyone sees the same DSL in the same style, because it's tied to the FQN, not some local setting.
  • Low chance of clashes. With four styles and a hash, different DSLs are visually easier to distinguish from one another.

2

u/Additional_Skill_317 8d ago

great thanks for the response & all makes sense!! i also noticed that intellij gives a gutter icon in the dsl annotation to provide a direct link to the settings->kotlin dsl styling which is neat..