Skip to main content
summaryrefslogtreecommitdiffstats
blob: fff548b742b79314e8671532110691fc156339a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 * Copyright (c) 2014 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.ui.highlight;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.common.ui.highlight.BaseSemanticHighlighter;
import org.eclipse.etrice.core.converter.RoomValueConverterService;
import org.eclipse.etrice.core.fsm.fSM.DetailCode;
import org.eclipse.etrice.core.services.RoomGrammarAccess;
import org.eclipse.etrice.core.ui.util.UIExpressionUtil;
import org.eclipse.etrice.core.ui.util.UIExpressionUtil.ExpressionCache;
import org.eclipse.etrice.expressions.detailcode.IDetailExpressionProvider;
import org.eclipse.etrice.expressions.ui.highlight.ExpressionRuleFactory;
import org.eclipse.etrice.expressions.ui.highlight.TargetLanguageRuleFactory;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.CancelIndicator;

import com.google.common.collect.Iterables;
import com.google.inject.Inject;

/**
 * @author Henrik Rentz-Reichert
 * 
 */
public class RoomSemanticHighlightingCalculator extends BaseSemanticHighlighter {

	@Inject
	RoomGrammarAccess grammar;
	
	@Inject
	RoomValueConverterService converterService;
	
	@Override
	public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
		if (resource == null || resource.getParseResult() == null)
			return;

		ExpressionCache expressionCache = new ExpressionCache();
		INode root = resource.getParseResult().getRootNode();
		for (INode node : root.getAsTreeIterable()) {
			if(cancelIndicator.isCanceled()) 
				break;
			
			super.provideHighlightingFor(node, resource, acceptor);	
			
			EObject obj = node.getGrammarElement();
			if (obj instanceof RuleCall) {
				RuleCall ruleCall = (RuleCall) obj;
				if(ruleCall.getRule() == grammar.getAnnotationRule()){
					acceptor.addPosition(node.getOffset(), node.getLength(), RoomHighlightingConfiguration.HL_ANNOTATION_ID);
				}
				else if(ruleCall.getRule() == grammar.getCC_STRINGRule()) {
					detailCodeHighlight(node, acceptor, expressionCache);
				}
			}

		}
	}
	
	protected void detailCodeHighlight(INode node, IHighlightedPositionAcceptor acceptor, ExpressionCache cache) {		
		final String text = converterService.getCC_StringConverter().stripDelim(node.getText());
		final int offset = node.getOffset() + converterService.getCC_StringConverter().getDelim().length();
		
		DetailCode dc = null;
		if(node.getParent().getSemanticElement() instanceof DetailCode) {
			dc = (DetailCode) node.getParent().getSemanticElement();
		}
		IDetailExpressionProvider exprProvider = UIExpressionUtil.getExpressionProvider(dc, null, cache);
		XtextHighlightStyles styles = new XtextHighlightStyles();
		RuleBasedScanner scanner = new RuleBasedScanner();
		scanner.setRules(Iterables.toArray(Iterables.concat(
				TargetLanguageRuleFactory.getGeneralLiteralRules(styles),
				ExpressionRuleFactory.getInitialExpressionRules(exprProvider, styles),
				TargetLanguageRuleFactory.getGeneralKeywordRules(styles))
		, IRule.class));
		scanner.setRange(new Document(text), 0, text.length());
		
		IToken lastToken = null;
		while(lastToken != Token.EOF) {
			lastToken = scanner.nextToken();
			if(lastToken != null && lastToken.getData() != null) {
				acceptor.addPosition(offset + scanner.getTokenOffset(), scanner.getTokenLength(), (String) lastToken.getData());
			}
		}
		
	}
	
}

Back to the top