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.
Color Scheme SettingsJavaScript:
- this, super
- var, let, const
- import, export, require, from
- debugger
- null, undefined
- function
TypeScript:
- private, public, protected
Java:
- private, public, protected
- this, super
- static, final
PHP:
- private, public, protected
- $this, self
- static, final
- use, namespace
- function
- class, extends
- JDK 1.8
- IntelliJ IDEA with Gradle
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:
- Open the file
JavaAnnotator.java
. - Create a new
TextAttributesKey
at the top namedCLASS
(or whatever you want) and assign it a new TextAttributes with a key of your choice, for instanceJAVA.MYCUSTOM_CLASS
. - Note that in order to not override existing TextAttributeKeys provided by the IDE, it is recommended to add a custom identifier that will most likely not be used by the IDE or other plugins. The recommended denomination would be
<language>.<identifier>_<keyword>
- Select the fallback attributes key, e.g. the color to take if the user didn't select a color. Here it would be the
JAVA_KEYWORD
key. - Implement the
getKeywordKind
method. This method will parse the editor text and annotate the keywords with the relevant TextAttributesKey. Here it would return theCLASS
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:
- Open the file
JavaColorSettings.java
- Add a new
TextAttributesKey
like before, but instead of creating a new one, we will reuse the one created in theJavaAnnotator
. - Add a new
AttributesDescriptor
in theJAVA_ATTRIBUTES
field. This will be the new entry in the Java Color Scheme page. - Optional: Add a new "tag" in the
createAdditionalHlAttrs
method and assign it theTextAttributesKey
. Here it would becustom_class
for instance. - Optional: Change the
demoText
and add an example text of your custom keyword, wrapped by the new "tag" you created previously. Here, it would be something likepublic <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
.
- Create your new Annotator, like for example
KotlinAnnotator
- Implement the methods by taking into example
JavaAnnotator
- Create a new ColorSettings page and do the same.
- Register your new annotator and colorSettingsPage into the
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.