StyledText cannot change style with setStyleRanges()

151 Views Asked by At

The StyledText widget shows a piece of Java code, and it uses a JavaLineStyler for syntax highlighting. However, I would like to show some tokens as selected using

codeViewer.setStyleRanges(sranges); //sranges is an array of StyleRange

But looks like the second round style change is prevented by the following code snippet in the StyledText widget source.

public void setStyleRanges(StyleRange[] ranges) {
checkWidget();
if (isListening(ST.LineGetStyle)) return;
if (ranges == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
setStyleRanges(0, 0, null, ranges, true);

}

Not sure how to keep the syntax highlighting alive and then show some tokens selected.

1

There are 1 best solutions below

0
On

You can't use a LineStyleListener and setStyleRanges at the same time, you must use all one or the other.

You could look at the JFace TextViewer, SourceViewer and TextPresentation classes which provide ways to set styles on StyledText, these use setStyleRanges.