This is a Jetbrains plugin showcasing a solution to the problem of custom syntax highlighters (https://youtrack.jetbrains.com/issue/IDEABKL-5473)
This plugin adds more Color Scheme settings for JavaScript, TypeScript and Java, allowing a finer customization of the language's keywords.
JavaScript:
TypeScript:
Java:
You can simplify development process thanks to Intellij's Gradle plugin. Install the plugin, restart the IDE and you will be prompted with a window asking if you want to import the project as a Gradle project. After that IntelliJ will download the Gradle Wrapper and the tasks will appear in the Gradle Tool Panel on the right.
Import the project from the build.gradle
file and develop as normal. Make sure you select JDK 8 in the import wizard. The other defaults are fine. You can run the above mentioned CLI Gradle tasks directly in the Gradle Tool Window, which expands from the right side of the screen. To debug, find "runIde
" in the list, right-click it, and choose Run/Debug.
Once you've done developing, you can test the plugin on a real IDE instead of the sandbox. To do so, first select the buildPlugin
task in the Gradle Tool Window. If everything went fine, it would create a new build of the plugin named <PluginName>-<version>.jar
inside the build/distributions
folder.
Then open the plugins page (Settings > Plugin) and select Install plugin from disk
, then select the jar file and restart the IDE. If all worked well you should see your additions in the Color Scheme Settings.
If you want to add new keywords to the provided languages, here's a quick tutorial:
Let's say for instance that I want to highlight differently the "class" keyword in Java:
JavaAnnotator.java
.TextAttributesKey
at the top named CLASS
(or whatever you want) and assign it a new TextAttributes with a key of your choice, for instance JAVA.MYCUSTOM_CLASS
.<language>.<identifier>_<keyword>
JAVA_KEYWORD
key.getKeywordKind
method. This method will parse the editor text and annotate the keywords with the relevant TextAttributesKey. Here it would return the CLASS
key if it finds the text "class".We're done with the annotator. Here we already have all occurrences of the "class" word annotated with our brand new TextAttributesKey and we can already change their color by modifying the color scheme's .icls
file. But of course it would be easier to have it in the Color Scheme Settings. Here's how:
JavaColorSettings.java
TextAttributesKey
like before, but instead of creating a new one, we will reuse the one created in the JavaAnnotator
.AttributesDescriptor
in the JAVA_ATTRIBUTES
field. This will be the new entry in the Java Color Scheme page.createAdditionalHlAttrs
method and assign it the TextAttributesKey
. Here it would be custom_class
for instance.demoText
and add an example text of your custom keyword, wrapped by the new "tag" you created previously. Here, it would be something like public <custom_class>class</custom_class> <class>Foo</class>
That's it! Now in the Java Additions page you will see your new entry, with the fallback you chose, and optionally the code representing the addition with preview in realtime. Once you click "OK" your Java code will be annotated with your new colors!
Adding more languages is almost the same process as adding new keywords, instead that you would have to create a new Annotator
and a new ColorSettingsPage
.
KotlinAnnotator
JavaAnnotator
plugin.xml
file.PS: Be aware that even though your annotators are bundled with your plugin, the languages they extend are plugin-dependant, meaning that your KotlinAnnotator will only work if the Kotlin plugin is installed in your users IDEs.