Skip to main content
summaryrefslogtreecommitdiffstats
blob: dc8c8ec0c4ffe609e75487971903abdad5a9bae0 (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
/*******************************************************************************
 * Copyright (c) 2011 BestSolution.at and others.
 * 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:
 *     Christoph Caks <ccaks@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.fx.ide.css.ui.highlighting;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.nodemodel.ICompositeNode;
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.eclipse.fx.ide.css.cssDsl.CssTok;
import org.eclipse.fx.ide.css.cssDsl.ElementSelector;
import org.eclipse.fx.ide.css.cssDsl.FuncTok;
import org.eclipse.fx.ide.css.cssDsl.IdentifierTok;
import org.eclipse.fx.ide.css.cssDsl.StringTok;
import org.eclipse.fx.ide.css.cssDsl.SymbolTok;
import org.eclipse.fx.ide.css.cssDsl.URLType;
import org.eclipse.fx.ide.css.cssDsl.css_declaration;
import org.eclipse.fx.ide.css.cssDsl.simple_selector;

public class CssDslHighlightingCalculator implements ISemanticHighlightingCalculator {
	
	@Override
	public void provideHighlightingFor(XtextResource resource,
			IHighlightedPositionAcceptor acceptor) {
		if( resource == null ) {
			return;
		}
		
		TreeIterator<Object> it = EcoreUtil.getAllContents(resource, true);
		
		while( it.hasNext() ) {
			Object o = it.next();
			
			if (o instanceof ElementSelector) {
				final ICompositeNode n = NodeModelUtils.getNode((EObject)o);
				acceptor.addPosition(n.getOffset(), n.getLength(), CssDslHighlightingConfiguration.ELEMENT);
			}
			else if (o instanceof IdentifierTok) {
				final ICompositeNode n = NodeModelUtils.getNode((EObject)o);
				acceptor.addPosition(n.getOffset(), n.getLength(), CssDslHighlightingConfiguration.DEFAULT_ID);
			}
			else if( o instanceof css_declaration ) {
				css_declaration dec = (css_declaration) o;
				if( dec.getProperty() != null && dec.getProperty().getName() != null && dec.getProperty().getName().trim().length() > 0 ) {
					ICompositeNode n = NodeModelUtils.getNode(dec);
					if( n.hasChildren() ) {
						acceptor.addPosition(n.getFirstChild().getOffset(), n.getFirstChild().getLength(), CssDslHighlightingConfiguration.DECLARATIONNAME);
					}	
				}
			} 
			else if( o instanceof simple_selector ) {
				final ICompositeNode n = NodeModelUtils.getNode((EObject)o);
				acceptor.addPosition(n.getOffset(), n.getLength(), CssDslHighlightingConfiguration.SELECTOR);
			}
			else if (o instanceof URLType) {
				final URLType url = (URLType) o;
				final ICompositeNode n = NodeModelUtils.getNode(url);
				acceptor.addPosition(n.getOffset(), 4, CssDslHighlightingConfiguration.FUNCTION);
				acceptor.addPosition(n.getOffset()+4, n.getLength()-5, CssDslHighlightingConfiguration.URL);
				acceptor.addPosition(n.getOffset() + n.getLength() - 1, 1, CssDslHighlightingConfiguration.FUNCTION);
			}
			else if (o instanceof FuncTok) {
				final FuncTok funcTok = (FuncTok) o;
				final ICompositeNode n = NodeModelUtils.getNode(funcTok);
				
				int nameLength = funcTok.getName().getName().length();
				acceptor.addPosition(n.getOffset(), nameLength + 1, CssDslHighlightingConfiguration.FUNCTION);
				
				for (CssTok tok : ((FuncTok) o).getParams()) {
					if (tok instanceof SymbolTok) {
						if (",".equals(((SymbolTok) tok).getSymbol())) {
							ICompositeNode colonNode = NodeModelUtils.getNode(tok);
							acceptor.addPosition(colonNode.getOffset(), colonNode.getLength(), CssDslHighlightingConfiguration.FUNCTION);
						}
					}
				}
				
				acceptor.addPosition(n.getOffset() + n.getLength() - 1, 1, CssDslHighlightingConfiguration.FUNCTION);
			}
			else if (o instanceof StringTok) {
				final ICompositeNode n = NodeModelUtils.getNode((EObject)o);
				
				acceptor.addPosition(n.getOffset(), n.getLength(), CssDslHighlightingConfiguration.STRING_ID);
			}
		}
	}
}

Back to the top