Commit 16b0e07e authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ custom highlighting, both on predefined keywords and based on semantics (events only), #149

parent aaf5102d
Loading
Loading
Loading
Loading
+34 −1
Original line number Diff line number Diff line
@@ -5,6 +5,12 @@ package org.etsi.mts.tdl.ui;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.xtext.ui.editor.hover.IEObjectHoverProvider;
import org.eclipse.xtext.ui.editor.syntaxcoloring.AbstractAntlrTokenToAttributeIdMapper;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfiguration;
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.etsi.mts.tdl.ui.highlighting.TDLtxHighlightingCalculator;
import org.etsi.mts.tdl.ui.highlighting.TDLtxHighlightingConfiguration;
import org.etsi.mts.tdl.ui.highlighting.TDLtxTokenToAttributeIdMapper;
import org.etsi.mts.tdl.ui.hover.TDLtxHoverProvider;

/**
@@ -20,6 +26,33 @@ public class TDLtxUiModule extends AbstractTDLtxUiModule {
		return TDLtxHoverProvider.class;
	}

	//@Override
    public Class<? extends IHighlightingConfiguration> bindIHighlightingConfiguration() {
        return TDLtxHighlightingConfiguration.class;
    }

    public Class<? extends AbstractAntlrTokenToAttributeIdMapper> bindAbstractAntlrTokenToAttributeIdMapper() {
        return TDLtxTokenToAttributeIdMapper.class;
    }
    
    public Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator() {
		return TDLtxHighlightingCalculator.class;
	}

//    @Override
//    public void configure(Binder binder) {
//        super.configure(binder);
//        binder.bind(TokenTypeToStringMapper.class).to(TDLtxTokenToAttributeIdMapper.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;
//            }
//        });
//    }
    
//	@Override
//	public Class<? extends IResourceSetProvider> bindIResourceSetProvider() {
//		//potential optimisation based on https://alexruizlog.blogspot.com/2012/07/make-your-xtext-based-editor-300-times.html?m=1#!
+188 −0
Original line number Diff line number Diff line
package org.etsi.mts.tdl.ui.highlighting;
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.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.ui.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.etsi.mts.tdl.ActionBehaviour;
import org.etsi.mts.tdl.ActionReference;
import org.etsi.mts.tdl.AnyValue;
import org.etsi.mts.tdl.Assertion;
import org.etsi.mts.tdl.Assignment;
import org.etsi.mts.tdl.ComponentInstance;
import org.etsi.mts.tdl.ComponentType;
import org.etsi.mts.tdl.DataInstance;
import org.etsi.mts.tdl.DataInstanceUse;
import org.etsi.mts.tdl.DataType;
import org.etsi.mts.tdl.Function;
import org.etsi.mts.tdl.GateInstance;
import org.etsi.mts.tdl.GateReference;
import org.etsi.mts.tdl.GateType;
import org.etsi.mts.tdl.InlineAction;
import org.etsi.mts.tdl.Interaction;
import org.etsi.mts.tdl.Member;
import org.etsi.mts.tdl.Parameter;
import org.etsi.mts.tdl.Quiescence;
import org.etsi.mts.tdl.Target;
import org.etsi.mts.tdl.tdlPackage;
import org.etsi.mts.tdl.structuredobjectives.Event;
import org.etsi.mts.tdl.structuredobjectives.EventReference;
import org.etsi.mts.tdl.structuredobjectives.StructuredObjectivesPackage;
import org.etsi.mts.tdl.structuredobjectives.impl.StructuredTestObjectiveImpl;
import org.etsi.mts.tdl.TestConfiguration;
import org.etsi.mts.tdl.TestDescription;
import org.etsi.mts.tdl.TimerOperation;
import org.etsi.mts.tdl.VariableUse;
import org.etsi.mts.tdl.VerdictAssignment;
import org.etsi.mts.tdl.Wait;

public class TDLtxHighlightingCalculator implements  ISemanticHighlightingCalculator  {


//    @Override
    public void provideHighlightingFor( XtextResource resource, IHighlightedPositionAcceptor acceptor ) {
    	if( resource == null ) {
            return;
        }

        Iterator<EObject> iter = EcoreUtil.getAllContents(resource, true);
        while(iter.hasNext()) {
            EObject current = iter.next();
            if (current instanceof Event) {
                INode name = getFirstFeatureNode( current, StructuredObjectivesPackage.eINSTANCE.getEvent().getEStructuralFeature("name"));
                highlightNode( name, TDLtxHighlightingConfiguration.KEYWORD_ID, acceptor);
            } else if (current instanceof EventReference) {
                INode node = getFirstFeatureNode( current, StructuredObjectivesPackage.eINSTANCE.getEventReference_Event());
                highlightNode( node, TDLtxHighlightingConfiguration.KEYWORD_ID, acceptor);
            	  
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateInstance_Type());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof GateInstance) {
//                INode name = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateInstance().getEStructuralFeature("name"));
//                highlightNode( name, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateInstance_Type());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof GateType) {
//                INode name = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateType().getEStructuralFeature("name"));
//                highlightNode( name, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                List<INode> nodes = getAllFeatureNodes( current, tdlPackage.eINSTANCE.getGateType_DataType());
//                for (INode node : nodes) {
//                	highlightNode( node, TDLtxHighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//                }
//            } else if (current instanceof GateReference) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateReference_Component());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode gate = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getGateReference_Gate());
//                highlightNode( gate, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof TestConfiguration) {
//                INode name = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTestConfiguration().getEStructuralFeature("name"));
//                highlightNode( name, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTestDescription_TestConfiguration());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof ComponentInstance) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getComponentInstance().getEStructuralFeature("name"));
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
////                INode gate = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getComponentInstance_Gate());
////                highlightNode( gate, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode type = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getComponentInstance_Type());
//                highlightNode( type, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof ComponentType) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getComponentType().getEStructuralFeature("name"));
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof DataInstance) {
//                INode name = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getDataInstance().getEStructuralFeature("name"));
//                highlightNode( name, TDLtxHighlightingConfiguration.DATA_LAYOUT, acceptor);
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getDataInstance_DataType());
//                highlightNode( node, TDLtxHighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof DataType) {
//            	INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getDataType().getEStructuralFeature("name"));
//              	highlightNode( node, TDLtxHighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof Parameter) {
//            	INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getParameter_DataType());
//              	highlightNode( node, TDLtxHighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof Function) {
//            	INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getFunction_ReturnType());
//              	highlightNode( node, TDLtxHighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof Function) {
//            	INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getFunction_ReturnType());
//              	highlightNode( node, TDLtxHighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof DataInstanceUse) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getDataInstanceUse_DataInstance());
//                highlightNode( node, TDLtxHighlightingConfiguration.DATA_LAYOUT, acceptor);
//            } else if (current instanceof AnyValue) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getAnyValue_DataType());
//                highlightNode( node, TDLtxHighlightingConfiguration.DATATYPE_LAYOUT, acceptor);
//            } else if (current instanceof VerdictAssignment) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getVerdictAssignment_Verdict());
//                highlightNode( node, TDLtxHighlightingConfiguration.BEHAVIOUR_LAYOUT, acceptor);
//            } else if (current instanceof Assertion) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getAssertion_Otherwise());
//                highlightNode( node, TDLtxHighlightingConfiguration.BEHAVIOUR_LAYOUT, acceptor);
//            } else if (current instanceof Interaction) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getInteraction_SourceGate());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof Target) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTarget_TargetGate());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof Quiescence) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getQuiescence_GateReference());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//                INode component = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTimeOperation_ComponentInstance());
//                highlightNode( component, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof Wait) {
//                INode component = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTimeOperation_ComponentInstance());
//                highlightNode( component, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof VariableUse) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getVariableUse_ComponentInstance());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof ActionBehaviour) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getActionBehaviour_ComponentInstance());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof TimerOperation) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTimerOperation_ComponentInstance());
//                highlightNode( node, TDLtxHighlightingConfiguration.CONFIGURATION_LAYOUT, acceptor);
//            } else if (current instanceof TestDescription) {
//                INode node = getFirstFeatureNode( current, tdlPackage.eINSTANCE.getTestDescription_TestConfiguration());
//                highlightNode( node, TDLtxHighlightingConfiguration.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 org.etsi.mts.tdl.ui.highlighting;
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 TDLtxHighlightingConfiguration extends DefaultHighlightingConfiguration {

	public static final String EXTRA_KEYWORD_LAYOUT = "extra_keyword";
	private static final RGB   EXTRA_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(EXTRA_KEYWORD_LAYOUT, "Additional Keywords", extraKeywordLayoutTextStyle());
        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 behaviourLayoutTextStyle() {
        TextStyle textStyle = new TextStyle();
        textStyle.setStyle(SWT.BOLD);
        textStyle.setColor(BEHAVIOUR_COLOUR);
        return textStyle;
    }
    public TextStyle extraKeywordLayoutTextStyle() {
        TextStyle textStyle = new TextStyle();
        textStyle.setStyle(SWT.BOLD);
        textStyle.setColor(EXTRA_KEYWORD_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
+46 −0
Original line number Diff line number Diff line
package org.etsi.mts.tdl.ui.highlighting;
import java.util.HashSet;

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

public class TDLtxTokenToAttributeIdMapper extends DefaultAntlrTokenToAttributeIdMapper {

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

    public TDLtxTokenToAttributeIdMapper() {
        extraKeywords.add("'calls'");
        extraKeywords.add("'receives'");
        extraKeywords.add("'sends'");
        extraKeywords.add("'triggers'");
        extraKeywords.add("'times'");
        extraKeywords.add("'out'");
        extraKeywords.add("'starts'");
        extraKeywords.add("'stops'");
        extraKeywords.add("'waits'");
        extraKeywords.add("'returns'");
//        configurationKeywords.add("'creates'");
//        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 (extraKeywords.contains(tokenName)) {
            return TDLtxHighlightingConfiguration.KEYWORD_ID;
        }
        if (configurationKeywords.contains(tokenName)) {
            return TDLtxHighlightingConfiguration.KEYWORD_ID;
        }
        return super.calculateId(tokenName, tokenType);
    }

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

}