Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Assignments coloring(How to do assigment coloring without writing my own JavaEditor?)
Assignments coloring [message #1757914] Tue, 21 March 2017 19:19 Go to next message
Igor Adamovich is currently offline Igor AdamovichFriend
Messages: 6
Registered: March 2017
Junior Member
Hello.
I am currently writing some AST analyzing and following problem appears:
I need to color some ASTNode.ASSIGNMENTs in existing JavaEditor (change background color of assignment expression). Is it possible to do that or i should write my own JavaEditor?

I have looked in TextEditor annotation painter direction. If i understand right, it is necessary to have SourceViewer or similar but such objects can be accessed only in TextEditor child.

Another direction is to color text through SourceViewer.getTextWidget. It returns styledText that can be colored manually. But again, i need to have SourceViewer.

Am i missing somthing? Is it possible to color ASTNode.ASSIGNMENTs without battling with JavaEditor?

[Updated on: Tue, 28 March 2017 07:22]

Report message to a moderator

Re: Assigments coloring [message #1758341 is a reply to message #1757914] Mon, 27 March 2017 19:54 Go to previous message
Igor Adamovich is currently offline Igor AdamovichFriend
Messages: 6
Registered: March 2017
Junior Member
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

Previous Topic:what is org.eclipse.jdt.core.compiler.codegen.methodParameters?
Next Topic:Different bahaviour of code completion in Junit and in editor of JDT codeAssist
Goto Forum:
  


Current Time: Mon Oct 14 02:37:00 GMT 2024

Powered by FUDForum. Page generated in 0.03381 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top