Skip to main content
summaryrefslogtreecommitdiffstats
blob: c238b75470584d0c36660e12e0cb6fb5c0a5ec86 (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
/*******************************************************************************
 * 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * 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.services.RoomGrammarAccess;
import org.eclipse.etrice.dctools.ast.DCUtil;
import org.eclipse.etrice.dctools.fsm.ast.DCLanguage;
import org.eclipse.etrice.ui.behavior.fsm.Activator;
import org.eclipse.etrice.ui.behavior.fsm.actioneditor.preferences.PreferenceConstants;
import org.eclipse.jface.preference.IPreferenceStore;
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.inject.Inject;

/**
 * @author Henrik Rentz-Reichert
 * 
 */
public class RoomSemanticHighlightingCalculator extends BaseSemanticHighlighter {
	
	@Inject RoomGrammarAccess grammar;
	@Inject RoomValueConverterService converterService;
	@Inject DCUtil detailCodeUtil;
	DCLanguage language = null;
	
	public RoomSemanticHighlightingCalculator() {
	}
	
	@Override
	public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
		if (resource == null || resource.getParseResult() == null)
			return;
		
		if (language==null) {
			setLanguage();
		}

		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()) {
					HighlightingAstVisitor.highlight(node, acceptor, detailCodeUtil);
				}
			}

		}
	}

	private void setLanguage() {
		IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
		String lang = preferenceStore.getString(PreferenceConstants.EDITOR_LANGUAGE);
		if (lang.equals(PreferenceConstants.JAVA_LANGUAGE)) {
			language = DCLanguage.JAVA_LANGUAGE;
		}
		else {
			language = DCLanguage.CPP_LANGUAGE;
		}
		detailCodeUtil.setLanguage(language);
	}
}

Back to the top