views:

270

answers:

1

I need to write a text editor which can merge arbitrary text styles from several sources (in my case: spell checker, style hints like repeated words, links and other markup) using SWT StyledText. I examined the the standard ways to do it:

  1. I could install a modify listener and create the styles for the whole text for each modification. That's pretty slow but accurate.

  2. I could use a LineStyleListener. This means I'll have to redraw the text myself (for example in the case of repeated words because some of them will be outside the current edit range) plus the editor doesn't cache the styles, so this API gets called much more often then one would expect.

  3. I could use a background reconciler like the IDE does. This means the styling lags behind the edits which is bad from a user perspective.

The icing of the cake is that I need hierarchical styles, so I have to reimplement the StyleRange API.

Has anyone seen a better solution? Can someone direct me to some examples which do more than highlight keywords?

+1  A: 

I used this example as a starting point for an editor which got fairly advanced. It helped me to understand multi-line styling, and implemenent a reg-ex based styling engine for my editor.

The StyleRange API doesn't really provide a way of doing intersections like merging style 1 which goes from index 1 to index 20 and style 2 ranging from 10 to 14. Something like this.

If this is what you mean with 'hierarchical styles', there's a solution in JFace: org.eclipse.jface.text.TextPresentation

You can add all your styles with mergeStyleRange(.), and use getAllStyleRangeIterator() to get the result. Very useful.

the.duckman