Help with Editor Selection + Color/Font Scheme

I am trying to write a little plugin that takes the current editor selection and copies that snippet of code as html to the clipboard.
(The built-in action always create a complete html document. Later I also like the pluging to be configurable, so that it uses any formatting tags applicable to a certain blog/forum software.)
I managed to access the Editor, EditorColorsScheme, Document, SelectionModel, and MarkupModel.
Now I am just stuck. Where do I go from here?
The MarkupModel and the RangeHighlighters looked promising, but I don't think they are used for regular syntax highlighting?!
I can get the plain text from the selection model, but that does not help me in parsing the text and associating colors and fonts.

Can I somehow get the PSI subtree that matches the selection and then get the TextAttributes using the current color scheme?

Argl... any suggestions?

12 comments
Comment actions Permalink

PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()).getLanguage().getSyntaxHighlighter()
will return SytaxHighlighter object capable to return TextAttributeKey for
every lexem in the file along with Lexer object to get lexems. One could
convert TextAttrubuteKey to TextAttribute given EditorColorsScheme.

-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"

I am trying to write a little plugin that takes the current editor
selection and copies that snippet of code as html to the clipboard.

(The built-in action always create a complete html document. Later I
also like the pluging to be configurable, so that it uses any
formatting tags applicable to a certain blog/forum software.)

I managed to access the Editor, EditorColorsScheme, Document,
SelectionModel, and MarkupModel.

Now I am just stuck. Where do I go from here?

The MarkupModel and the RangeHighlighters looked promising, but I
don't think they are used for regular syntax highlighting?!

I can get the plain text from the selection model, but that does not
help me in parsing the text and associating colors and fonts.

Can I somehow get the PSI subtree that matches the selection and then
get the TextAttributes using the current color scheme?

Argl... any suggestions?




0
Comment actions Permalink

Thanks a lot Maxim!
I have not yet tried the Lexer part of your answer. (Because I had already resorted to visiting the PSI tree and getting the token type of each PsiJavaToken - will change that, soon.)
Yet the SyntaxHighlighter solution works very well.
Two minor questions, though:
- The identifier for method declarations is just of token type "IDENTIFIER", so that syntaxHighlighter.getTokenHighlights() always returns an empty array, even if I have configured special appearance for method declarations. The built-in html export has the same problem. Is there any way I can get the 'real' color/font for method declarations?
- Why does syntaxHighlighter.getTokenHighlights() return an array? Should I just loop through it from start to end, and let 'later' TextAttributes properties override earlier ones?

0
Comment actions Permalink

Two minor questions, though:

- The identifier for method declarations is just of token type
"IDENTIFIER", so that syntaxHighlighter.getTokenHighlights() always
returns an empty array, even if I have configured special appearance
for method declarations. The built-in html export has the same
problem. Is there any way I can get the 'real' color/font for method
declarations?

We have 2 pass highlighting scheme. Base lexing highlighting is performed
with SyntaxHighlighter and those are merged with RangeHighlighters later
when painting is performed.

- Why does syntaxHighlighter.getTokenHighlights() return an array?
Should I just loop through it from start to end, and let 'later'
TextAttributes properties override earlier ones?

Exactly.

Actually, it seems you'll have to implement whole Editor painting algorithm
to get desired results which seems to be not that easy task. We could probably
provide more straightforward API for this if really necessary.


0
Comment actions Permalink

those are merged with RangeHighlighters later when
painting is performed.
...
Actually, it seems you'll have to implement whole Editor
painting algorithm to get desired results which seems to
be not that easy task. We could probably provide more
straightforward API for this if really necessary.


Na, it's not really necessary - it would be just nice ;)
IMHO at least the built in HTML export should use the code style as it is configured and not some stripped down version with only a little highlighting.
Yet if you have some additional tips how to continue I would start to tackle that "not so easy task".
How does the editor create/use the RangeHighlighters? I tried editor.getMarkupModel().getAllHighlighters() but that just returns an empty model.

(BTW I have switched to using the highlighting lexer instead of psi visitor and it's much simpler now.)

0
Comment actions Permalink

How does the editor create/use the RangeHighlighters? I tried
editor.getMarkupModel().getAllHighlighters() but that just returns an
empty model.

There's also editor.getDocument().getMarkupModel() to share between all the
editors viewing same document. All source code level highlighting goes there.
All presentation level highlighting like those in CtrlShiftF7 goes to editor's
MarkupModel.


0
Comment actions Permalink

Thanks again, from a first look in the debugger that looks very promising: I even found the 'bold' text attribute for my method declaration :)
Ah great: I wanted to ask about how to differentiate between additional syntax highlighting and warnings - but I've found getLayer() method and HighlighterLayer interface.

Looks very nice - I'll call it quits for today and continue tomorrow...

0
Comment actions Permalink

That works really nice, now additional highlighting of members and declarations is also preserved - my html export is even better than the built-in export ;)

What I haven't managed to achieve is to use the default editor background color. Have tried lots of things, but nothing seems to return the configured background color:

 white
editor.getColorsScheme().getAttributes(TextAttributesKey.find("BACKGROUND")) => null
editor.get[Content]Component().getBackground() => grey/white]]>

One more tip?

BTW: I think I have discovered a new bug while playing with colors: IDEA-817 - caret row background color is ignored.

0
Comment actions Permalink

Hm, I think the general question is, how to get access to the Colors&Fonts/General settings and in which order to apply them.

I just noticed that the following code does return the color configured as "background":
colorsScheme.getColor(com.intellij.openapi.editor.colors.ColorKey.find("BACKGROUND"))
I just had my background configured using "default text".
Still I can't find a ColorKey for "default text"?!

0
Comment actions Permalink

Please find publicly available keys in EditorColors and CodeInsightColors

-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"

Hm, I think the general question is, how to get access to the
Colors&Fonts/General settings and in which order to apply them.

I just noticed that the following code does return the color
configured as "background":

colorsScheme.getColor(com.intellij.openapi.editor.colors.ColorKey.find
("BACKGROUND"))
I just had my background configured using "default text".
Still I can't find a ColorKey for "default text"?!




0
Comment actions Permalink

And HighlighterColors also
-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"

Please find publicly available keys in EditorColors and
CodeInsightColors

-------------------
Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"

>> Hm, I think the general question is, how to get access to the
>> Colors&Fonts/General settings and in which order to apply them.
>>
>> I just noticed that the following code does return the color
>> configured as "background":
>>
>> colorsScheme.getColor(com.intellij.openapi.editor.colors.ColorKey.fin
>> d
>> ("BACKGROUND"))
>> I just had my background configured using "default text".
>> Still I can't find a ColorKey for "default text"?!



0
Comment actions Permalink

I'm getting really, really close to the original editor appearance. Still, it seems I am always one little detail away:

How to get underline decorations (e.g. javadoc tags)?
The very strange fact is that all TextAttributes (from both SyntaxHighlighter and MarkupModel) I process return EffectType.LINE_UNDERSCORE!

0
Comment actions Permalink

EffectType is only valid (and should be applied) when non-null effect color
is returned. Otherwise its just arbitrary value taken from the enum pattern
which is indeed is LINE_UNDERSCORE in most cases.

-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"

I'm getting really, really close to the original editor appearance.
Still, it seems I am always one little detail away:

How to get underline decorations (e.g. javadoc tags)?

The very strange fact is that all TextAttributes (from both
SyntaxHighlighter and MarkupModel) I process return
EffectType.LINE_UNDERSCORE!




0

Please sign in to leave a comment.