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
|
/*******************************************************************************
* Copyright (c) 2011 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:
* Juergen Haug
*
*******************************************************************************/
package org.eclipse.etrice.core.common.ui.highlight;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.common.converter.BaseConverterService;
import org.eclipse.etrice.core.common.converter.CCStringConverter;
import org.eclipse.etrice.core.common.converter.CCStringIndentation;
import org.eclipse.etrice.core.common.services.BaseGrammarAccess;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.syntaxcoloring.DefaultHighlightingConfiguration;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.xbase.lib.Pair;
import com.google.inject.Inject;
public class BaseSemanticHighlighter implements ISemanticHighlightingCalculator {
@Inject
BaseGrammarAccess grammar;
@Inject
BaseConverterService converterService;
@Override
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
if (resource == null || resource.getParseResult() == null)
return;
INode root = resource.getParseResult().getRootNode();
for (INode node : root.getAsTreeIterable()) {
if(cancelIndicator.isCanceled())
break;
provideHighlightingFor(node, resource, acceptor);
}
}
protected void provideHighlightingFor(INode node, XtextResource resource, IHighlightedPositionAcceptor acceptor) {
EObject obj = node.getGrammarElement();
if (obj instanceof RuleCall) {
AbstractRule rule = ((RuleCall) obj).getRule();
if(rule == grammar.getCC_STRINGRule()) {
ccStringHighlight(node, acceptor, converterService.getCC_StringConverter());
}
else if(rule == grammar.getNumberLiteralRule()) {
acceptor.addPosition(node.getOffset(), node.getLength() , DefaultHighlightingConfiguration.NUMBER_ID);
}
}
}
protected void ccStringHighlight(INode node, IHighlightedPositionAcceptor acceptor, CCStringConverter converter) {
final String delim = converter.getDelim();
if (node.getText() == null || node.getText().length() < delim.length() * 2)
return;
acceptor.addPosition(node.getOffset(), delim.length(), DefaultHighlightingConfiguration.STRING_ID);
acceptor.addPosition(node.getEndOffset() - delim.length(), delim.length(), DefaultHighlightingConfiguration.STRING_ID);
int offset = node.getOffset() + delim.length();
List<Pair<Integer, Integer>> lineStartLength = new CCStringIndentation(converter.stripDelim(node.getText())).highlight();
for (Pair<Integer, Integer> offsetLength : lineStartLength) {
acceptor.addPosition(offset + offsetLength.getKey(), offsetLength.getValue(), BaseHighlightingConfig.HL_CCSTRING);
}
}
}
|