I need to develop an Eclipse plugin that can "color" the same occurrence of a variable/value/tag in XML as the JAVA editor does.
I'm using the default XML Editor from eclipse, and am currently able to put a grey background on the selected words with the following code :
for (Point p : offsets){
TextPresentation t = new TextPresentation();
t.replaceStyleRange(new StyleRange( (int)p.getX(),
(int)(p.getY() - p.getX()),
null,
Color.win32_new(null, 0xDDDDDD)));
fText.changeTextPresentation(t, true);
}
My problem is that I can't recover the default style if the user tries to select another variable/tag/value. The text will not set its natural coloring after loosing the focus. For the moment, I am using hard-coded RGB values to set the defaults colors, BUT it is only "working" if the user kept the Eclipse default theme (white theme).
Is there a way to ask the document for a complete syntax coloring re-validation ?
Thanks for reading.
I found an answer by myself. Here it is :
before changing the style of the selection, you should first save the current style. Use a similar structure:
private ArrayList<Point> offsets = new ArrayList<Point>();
private ArrayList<Color> foregroundgColor = new ArrayList<Color>();
Then you put all the styles/offsets of the occurences in this structure, in a loop statement :
offsets.add(new Point(i,j));
fgColor.add(fText.getTextWidget().getStyleRangeAtOffset(i).foreground);
You can now apply the "highlighting" (grey background on the occurences) :
for (Point p : offsets){
TextPresentation t = new TextPresentation();
t.replaceStyleRange(new StyleRange( (int)p.getX(),
(int)(p.getY() - p.getX()),
null,
Color.win32_new(null, 0xDDDDDD)));
fText.changeTextPresentation(t, true);
}
Finally, when the selected occurences loses the focus, you restore the default styles :
for (int i = 0; i < offsets.size(); i++){
Point p = offsets.get(i);
TextPresentation t = new TextPresentation();
t.replaceStyleRange(new StyleRange( (int)p.getX(),
(int)(p.getY() - p.getX()),
fgColor.get(i),
null));
fText.changeTextPresentation(t, true);
}
offsets.clear();
fgColor.clear();