apply specific color/style to specific text in an EditorTextField

I have created a text field using EditorTextField. In the I need to apply a particualar color to specific text.
For example: below is my content

<me:name attri1="" attri2="">
    some java code goes here
</me:name>

i need  <me:name> and </me:name>, to be green
any java keyword between these tas to be blue
and so on...

Please suggest how to achieve this.

3 comments
Comment actions Permalink

There are two ways to implement this: the easy one and the right one.

The easy one is to use EditorTextField.getEditor().getMarkupModel().addRangeHighlighter() to highlight specific parts of text in the editor. You'll need to implement your own parsing, and your own logic for updating the highlighting when the user changes the text, but if all you need is those two types of highighting, this may be good enough.

The right approach is to put an actual XML file in your EditorTextField, by using PsiFileFactory.createFileFromText() to create a PSI file and then calling editorTextField.setDocument(PsiDocumentManager.getInstance(myProject).getDocument(psiFile)). You'll also need to set up language injection so that the contents of the <me:name> XML tag is interpreted as Java code, using the <injectionConfig> extension in your plugin.xml. This is somewhat more involved, but the benefit is that IntelliJ IDEA will take care of the parsing and updating for you, and you'll also get features like code completion and error highlighting in your EditorTextField for free.

Please let me know if you need more information.

0
Comment actions Permalink

Hi,

I my very new to intellij, please help me with more details..... thank you..

Below is my code:

Settings page:

<extensions defaultExtensionNs="com.intellij">
    <!-- Add your extensions here -->
      <applicationConfigurable instance="org.myPackage.settings.MyClass1"/>
      <applicationService serviceImplementation="org.myPackage.settings.MyClass2"
                          serviceInterface="org.myPackage.settings.MyClass2"/>
  </extensions>

from this settings page on click of a button i open a dialog (JDialog subclassed). My Editor text fields is one of the members of this dialog.
Attached is a sample of text that will go into my EditorTextField. I have to treat the content in this editor as a string. Somewhere I will have a list of
java key words, list of velocity directives and list of tag names (my:class, my:name etc). I have to define a particular color for each of them and apply them to the editor.



Attachment(s):
editor field content.png
0
Comment actions Permalink

Find the words that you need in your text and, for every word, call editorTextField.getEditor().getMarkupModel().addRangeHighlighter() passing the start and end offset of the word in your text, 0 as layer, your text attributes and HighlighterTargetArea.EXACT_RANGE as the last parameter. When the user changes the text, remove all highlighters with getMarkupModel().removeAllHighlighters() and then parse the text again.

0

Please sign in to leave a comment.