I just have found a solution.
Generally, yes, it's possible to hightlight some text in existing TextEditor(JavaEditor) without subclassing it.
Goal can be achived by using markers or textEditor annotations (dont confuse with java annotations).
I chosed first way because i needed to track highlighting dynamicly, depending on source changes.
IF u interested a way how it can be done, read this post.
First, you need to connect to compilationParticipant extension point:
<extension
point="org.eclipse.jdt.core.compilationParticipant">
<compilationParticipant
class="org.example.markers.exampleCompilationParticipant"
createsProblems="true"
id="test"
modifiesEnvironment="false">
</compilationParticipant>
</extension>
Then you should create myCompilationParticipant and override reconcile and isActive methods like this:
public class exampleCompilationParticipant extends CompilationParticipant{
public static final String MY_MARKER = "com.example.myannotation";
@Override
public boolean isActive(IJavaProject project) {
return true;
}
@Override
public void reconcile(ReconcileContext context)
Thirdly, you need to use extension points again:
to create annotation type that have custom parameters
and to connect marker with this annotation type
It should be looked like following:
<extension point="org.eclipse.ui.editors.markerAnnotationSpecification"
id="myAnnotationSpecification" name="myAnnotation">
<specification annotationType="com.example.myannotation"
label="Test marker annotation"
overviewRulerPreferenceKey="example.ruler"
overviewRulerPreferenceValue="false"
contributesToHeader = "false"
colorPreferenceKey="example.color"
colorPreferenceValue="0,255,0"
textPreferenceKey="example.text"
textPreferenceValue="false"
verticalRulerPreferenceKey="example.vertical"
verticalRulerPreferenceValue="true"
textStylePreferenceKey="example.style"
textStylePreferenceValue="NONE"
highlightPreferenceValue = "true"
highlightPreferenceKey="example.highlight">
</specification>
</extension>
<extension point="org.eclipse.ui.editors.annotationTypes">
<type
name="com.example.myannotation"
markerType="com.example.myannotation"/>
</extension>
And now you can create IMarkers in exampleCompilationParticipant's reconcile that would be colored:
public static final String MY_MARKER = "com.example.myannotation";
...
public void reconcile(ReconcileContext context){
super.reconcile(context);
ASTNode root = null;
IResource res = context.getWorkingCopy().getResource();
try {
res.deleteMarkers(MY_MARKER , true, IProject.DEPTH_INFINITE);
root = context.getAST8();
} catch (CoreException e) {
System.out.println("got Ast8 exception");
return;
}
AssignmentsVisitor visitor = new AssignmentsVisitor();
root.accept(visitor);
for(Assignment assignment : visitor.getAssignments()){
try {
IMarker marker = res.createMarker(MY_MARKER);
int start = assignment.getLeftHandSide().getStartPosition();
int end = assignment.getLeftHandSide().getStartPosition() + assignment.getLeftHandSide().getLength();
marker.setAttribute(IMarker.CHAR_START, start);
marker.setAttribute(IMarker.CHAR_END, end);
} catch (CoreException e) {
System.out.println("Marking assignments exeption");
}
}
}
Hope this would help someone.
[Updated on: Tue, 28 March 2017 07:24]
Report message to a moderator