Commit 8010d553 authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ added custom colouring

parent 5c1f4071
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
@@ -3,7 +3,22 @@
 */
package de.ugoe.cs.swe.ui;

import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.xtext.builder.nature.NatureAddingEditorCallback;
import org.eclipse.xtext.ide.editor.model.TokenTypeToStringMapper;
import org.eclipse.xtext.ide.editor.syntaxcoloring.AbstractAntlrTokenToAttributeIdMapper;
import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.eclipse.xtext.ui.editor.IXtextEditorCallback;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfiguration;

import com.google.common.base.Predicate;
import com.google.inject.Binder;
import com.google.inject.name.Names;

import de.ugoe.cs.swe.ui.syntaxcoloring.TTCN3HighlightingCalculator;
import de.ugoe.cs.swe.ui.syntaxcoloring.TTCN3HighlightingConfiguration;
import de.ugoe.cs.swe.ui.syntaxcoloring.TTCN3TokenToAttributeIdMapper;

/**
 * Use this class to register components to be used within the IDE.
@@ -12,4 +27,32 @@ public class TTCN3UiModule extends de.ugoe.cs.swe.ui.AbstractTTCN3UiModule {
	public TTCN3UiModule(AbstractUIPlugin plugin) {
		super(plugin);
	}
	//@Override
    public Class<? extends IHighlightingConfiguration> bindIHighlightingConfiguration() {
        return TTCN3HighlightingConfiguration.class;
    }
    public Class<? extends AbstractAntlrTokenToAttributeIdMapper> bindAbstractAntlrTokenToAttributeIdMapper() {
        return TTCN3TokenToAttributeIdMapper.class;
    }
	public Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator() {
		return TTCN3HighlightingCalculator.class;
	}
    @Override
    public void configure(Binder binder) {
        super.configure(binder);
        binder.bind(TokenTypeToStringMapper.class).to(TTCN3TokenToAttributeIdMapper.class);
        binder.bind(Predicate.class).annotatedWith(Names.named("ePackageUriFilter")).toInstance(new Predicate<String>() {
//            @Override
            public boolean apply(String input) {
                if (input.startsWith("http://www.eclipse.org/emf") && !input.equals(EcorePackage.eNS_URI))
                    return true;
                return false;
            }
        });
    }
    
	public Class<? extends IXtextEditorCallback> bindIXtextEditorCallback() {
	    return NatureAddingEditorCallback.class;
	}

}
+182 −0
Original line number Diff line number Diff line
package de.ugoe.cs.swe.ui.syntaxcoloring;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.CancelIndicator;

public class TTCN3HighlightingCalculator implements ISemanticHighlightingCalculator {


	@Override
	public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor,
			CancelIndicator cancelIndicator) {
    	if( resource == null ) {
            return;
        }
        INode root = resource.getParseResult().getRootNode();
        for(INode node : root.getAsTreeIterable()) {   
            EObject grammarElement = node.getGrammarElement(); 
            if(grammarElement instanceof RuleCall) {
                RuleCall rc = (RuleCall)grammarElement;
                AbstractRule r = rc.getRule();
                if (r instanceof TerminalRule) {
                	// It tests whether the node represents a name of an element (class, method, parameter).
//                	EObject c = grammarElement.eContainer();
//                	if(r.getName().equals("ID") && c instanceof Assignment && ((Assignment)c).getFeature().equals("name")) {
//	                	INode p = node.getParent();
//	                	if (p != null && p.getGrammarElement() instanceof RuleCall) {
//	                		rc = (RuleCall)p.getGrammarElement();
//	                		AbstractRule r = rc.getRule();
//	                		
//	                		// It tests whether the parent node represents a method.                        
//	                		if(r.getName().equals("Method")) {
//	                			acceptor.addPosition(node.getOffset(), node.getLength(), ExampleHighlightingConfiguration.METHOD_ID);
//	                		}
//	                	}
//	                }
                	if (r.getName().endsWith("KEYWORD")) {
                		highlightNode( node, TTCN3HighlightingConfiguration.KEYWORD_LAYOUT, acceptor);
                	}
                }
			   
            }
        }

        Iterator<EObject> iter = EcoreUtil.getAllContents(resource, true);
        while(iter.hasNext()) {
            EObject current = iter.next();
//            System.out.println(current.eClass().getName());
//            if (current instanceof GateInstance) {
//                INode name = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateInstance().getEStructuralFeature("name"));
//                highlightNode( name, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateInstance_Type());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof GateType) {
//                INode name = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateType().getEStructuralFeature("name"));
//                highlightNode( name, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                List<INode> nodes = getAllFeatureNodes( current, tdlPackage.eINSTANCE.getGateType_DataType());
//                for (INode node : nodes) {
//                	highlightNode( node, TTCN3HighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//                }
//            } else if (current instanceof GateReference) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateReference_Component());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode gate = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateReference_Gate());
//                highlightNode( gate, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof TestConfiguration) {
//                INode name = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTestConfiguration().getEStructuralFeature("name"));
//                highlightNode( name, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTestDescription_TestConfiguration());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof ComponentInstance) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getComponentInstance().getEStructuralFeature("name"));
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
////                INode gate = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getComponentInstance_Gate());
////                highlightNode( gate, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode type = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getComponentInstance_Type());
//                highlightNode( type, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof ComponentType) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getComponentType().getEStructuralFeature("name"));
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof DataInstance) {
//                INode name = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getDataInstance().getEStructuralFeature("name"));
//                highlightNode( name, TTCN3HighlightingConfiguration.DATA_LAYOUT, acceptor);
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getDataInstance_DataType());
//                highlightNode( node, TTCN3HighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof DataType) {
//            	INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getDataType().getEStructuralFeature("name"));
//              	highlightNode( node, TTCN3HighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof Parameter) {
//            	INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getParameter_DataType());
//              	highlightNode( node, TTCN3HighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof Function) {
//            	INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getFunction_ReturnType());
//              	highlightNode( node, TTCN3HighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof Function) {
//            	INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getFunction_ReturnType());
//              	highlightNode( node, TTCN3HighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof DataInstanceUse) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getDataInstanceUse_DataInstance());
//                highlightNode( node, TTCN3HighlightingConfiguration.DATA_LAYOUT, acceptor);
//            } else if (current instanceof AnyValue) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getAnyValue_DataType());
//                highlightNode( node, TTCN3HighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof VerdictAssignment) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getVerdictAssignment_Verdict());
//                highlightNode( node, TTCN3HighlightingConfiguration.BEHAVIOUR_LAYOUT, acceptor);
//            } else if (current instanceof Assertion) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getAssertion_Otherwise());
//                highlightNode( node, TTCN3HighlightingConfiguration.BEHAVIOUR_LAYOUT, acceptor);
//            } else if (current instanceof Interaction) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getInteraction_SourceGate());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof Target) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTarget_TargetGate());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof Quiescence) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getQuiescence_GateReference());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode component = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTimeOperation_ComponentInstance());
//                highlightNode( component, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof Wait) {
//                INode component = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTimeOperation_ComponentInstance());
//                highlightNode( component, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof VariableUse) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getVariableUse_ComponentInstance());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof ActionBehaviour) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getActionBehaviour_ComponentInstance());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof TimerOperation) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTimerOperation_ComponentInstance());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof TestDescription) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTestDescription_TestConfiguration());
//                highlightNode( node, TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            }

        }
    }

    private void highlightNode( INode node, String id, IHighlightedPositionAcceptor acceptor ) {
        if( node == null )
            return;
        if( node instanceof ILeafNode ) {
            acceptor.addPosition( node.getOffset(), node.getLength(), id );
        } else {
            for( ILeafNode leaf : node.getLeafNodes() ) {
                if( !leaf.isHidden() ) {
                    acceptor.addPosition( leaf.getOffset(), leaf.getLength(), id );
                }
            }
        }
    }

    public INode getFirstFeatureNode( EObject semantic, EStructuralFeature feature ) {
        if( feature == null )
            return NodeModelUtils.findActualNodeFor( semantic );
        List<INode> nodes = NodeModelUtils.findNodesForFeature( semantic, feature );
        if( !nodes.isEmpty() )
            return nodes.get( 0 );
        return null;
    }    
    public List<INode> getAllFeatureNodes( EObject semantic, EStructuralFeature feature ) {
        List<INode> nodes = NodeModelUtils.findNodesForFeature( semantic, feature );
        if( !nodes.isEmpty() )
            return nodes;
        return null;
    }

}
 No newline at end of file
+62 −0
Original line number Diff line number Diff line
package de.ugoe.cs.swe.ui.syntaxcoloring;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.xtext.ui.editor.syntaxcoloring.DefaultHighlightingConfiguration;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfigurationAcceptor;
import org.eclipse.xtext.ui.editor.utils.TextStyle;

public class TTCN3HighlightingConfiguration extends DefaultHighlightingConfiguration {

	public static final String KEYWORD_LAYOUT = "keywords";
	private static final RGB   KEYWORD_COLOUR = new RGB(150, 100, 100);
	public static final String BEHAVIOUR_LAYOUT = "behaviour";
	private static final RGB   BEHAVIOUR_COLOUR = new RGB(150, 140, 120);
	public static final String CONFIGURATION_LAYOUT = "configuration";
    private static final RGB   CONFIGURATION_COLOUR = new RGB(50, 40, 120);
	public static final String DATA_LAYOUT = "data";
	private static final RGB   DATA_COLOUR = new RGB(110, 140, 150);
	public static final String DATATYPE_LAYOUT = "datatype";
    private static final RGB   DATATYPE_COLOUR = new RGB(150, 40, 150);

    @Override
    public void configure(IHighlightingConfigurationAcceptor acceptor) {
        super.configure(acceptor);
        acceptor.acceptDefaultHighlighting(KEYWORD_LAYOUT, "Keywords", keywordLayoutTextStyle());
        acceptor.acceptDefaultHighlighting(BEHAVIOUR_LAYOUT, "Behaviour", behaviourLayoutTextStyle());
        acceptor.acceptDefaultHighlighting(CONFIGURATION_LAYOUT, "Configuration", configurationLayoutTextStyle());
        acceptor.acceptDefaultHighlighting(DATA_LAYOUT, "Data", dataLayoutTextStyle());
        acceptor.acceptDefaultHighlighting(DATATYPE_LAYOUT, "Data Type", dataTypeLayoutTextStyle());
    }

    public TextStyle keywordLayoutTextStyle() {
        TextStyle textStyle = new TextStyle();
        textStyle.setStyle(SWT.BOLD);
        textStyle.setColor(KEYWORD_COLOUR);
        return textStyle;
    }
    public TextStyle behaviourLayoutTextStyle() {
        TextStyle textStyle = new TextStyle();
        textStyle.setStyle(SWT.BOLD);
        textStyle.setColor(BEHAVIOUR_COLOUR);
        return textStyle;
    }
    public TextStyle configurationLayoutTextStyle() {
        TextStyle textStyle = new TextStyle();
        textStyle.setStyle(SWT.BOLD);
        textStyle.setColor(CONFIGURATION_COLOUR);
        return textStyle;
    }
    public TextStyle dataLayoutTextStyle() {
        TextStyle textStyle = new TextStyle();
        textStyle.setStyle(SWT.ITALIC);
        textStyle.setColor(DATA_COLOUR);
        return textStyle;
    }
    public TextStyle dataTypeLayoutTextStyle() {
        TextStyle textStyle = new TextStyle();
        textStyle.setStyle(SWT.ITALIC | SWT.UNDERLINE_SINGLE);
        textStyle.setColor(DATATYPE_COLOUR);
        return textStyle;
    }

}
 No newline at end of file
+56 −0
Original line number Diff line number Diff line
package de.ugoe.cs.swe.ui.syntaxcoloring;
import java.util.HashSet;

import org.eclipse.xtext.ide.editor.syntaxcoloring.DefaultAntlrTokenToAttributeIdMapper;


public class TTCN3TokenToAttributeIdMapper extends DefaultAntlrTokenToAttributeIdMapper {

    private HashSet<String> layoutKeywords = new HashSet<String>();
    private HashSet<String> configurationKeywords = new HashSet<String>();

    public TTCN3TokenToAttributeIdMapper() {
        layoutKeywords.add("'template'");
        layoutKeywords.add("'function'");
        layoutKeywords.add("'type'");
        layoutKeywords.add("'testcase'");
        layoutKeywords.add("'send'");
        layoutKeywords.add("'receive'");
//        layoutKeywords.add("'times'");
//        layoutKeywords.add("'out'");
//        layoutKeywords.add("'start'");
//        layoutKeywords.add("'stop'");
//        layoutKeywords.add("'waits'");
//        layoutKeywords.add("'quiet'");
//        layoutKeywords.add("'set verdict'");
//        layoutKeywords.add("'assert'");
//        layoutKeywords.add("'otherwise'");
//        layoutKeywords.add("'returned'");
//        layoutKeywords.add("'from'");
//        layoutKeywords.add("'assigned'");
//        configurationKeywords.add("'connect'");
//        configurationKeywords.add("'create'");
//        configurationKeywords.add("'uses'");
        
        //        for (Color color : Color.values()) {
        //            System.out.println(color.getLiteral() + "::" + color.getName());
        //            layoutKeywords.add("'" + color.getName().toLowerCase() + "'");
        //        }
    }

    @Override
    protected String calculateId(String tokenName, int tokenType) {
        if (layoutKeywords.contains(tokenName)) {
            return TTCN3HighlightingConfiguration.BEHAVIOUR_LAYOUT;
        }
        if (configurationKeywords.contains(tokenName)) {
            return TTCN3HighlightingConfiguration.CONFIGURATION_LAYOUT;
        }
        return super.calculateId(tokenName, tokenType);
    }

    public String getId(int tokenType) {
        return getMappedValue(tokenType);
    }

}