PageRenderTime 83ms CodeModel.GetById 44ms app.highlight 12ms RepoModel.GetById 24ms app.codeStats 0ms

/scss.editor/src/org/netbeans/modules/languages/scss/parser/ScssSemanticAnalyzer.java

http://scss-editor.googlecode.com/
Java | 241 lines | 88 code | 29 blank | 124 comment | 7 complexity | b09ee5acb9a01b98f3fbe8213d1c3c96 MD5 | raw file
  1package org.netbeans.modules.languages.scss.parser;
  2
  3import java.util.HashMap;
  4import java.util.Map;
  5import org.netbeans.modules.csl.api.ColoringAttributes;
  6import org.netbeans.modules.csl.api.OffsetRange;
  7import org.netbeans.modules.csl.api.SemanticAnalyzer;
  8import java.util.Set;
  9import java.util.regex.Matcher;
 10import java.util.regex.Pattern;
 11import javax.swing.text.Document;
 12import org.netbeans.modules.parsing.spi.Parser.Result;
 13import org.netbeans.modules.parsing.spi.Scheduler;
 14import org.netbeans.modules.parsing.spi.SchedulerEvent;
 15import javax.swing.text.StyledDocument;
 16import org.antlr.runtime.Token;
 17import org.antlr.runtime.tree.CommonTree;
 18import org.openide.text.NbDocument;
 19
 20/**
 21 *
 22 * @author marsaultj
 23 */
 24public class ScssSemanticAnalyzer extends SemanticAnalyzer {
 25
 26    private boolean cancelled;
 27    private Document doc;
 28
 29    private static final Pattern INTERPOLATION = Pattern.compile("[^\\\\]\\{\\$#?[a-zA-Z0-9\\.\\(\\)]*\\}?");
 30
 31    private static final Pattern VAR = Pattern.compile("#\\{(.+?)\\}");
 32    private Map<OffsetRange, Set<ColoringAttributes>> semanticHighlights;
 33
 34//    @Override
 35//    public Map<OffsetRange, ColoringAttributes> getHighlights() {
 36//	return semanticHighlights;
 37//    }
 38    public void cancel() {
 39	cancelled = true;
 40    }
 41
 42//    public void run(CompilationInfo ci) throws Exception {
 43    @Override
 44    public void run(Result result, SchedulerEvent se) {
 45
 46	final Map<OffsetRange, Set<ColoringAttributes>> highlights = new HashMap<OffsetRange, Set<ColoringAttributes>>();
 47	semanticHighlights = null;
 48
 49//	OffsetRange range = null;
 50	if (cancelled) {
 51	    return;
 52	}
 53
 54	System.out.println("CSSSemanticAnalyzer run");
 55	doc = result.getSnapshot().getSource().getDocument(false);
 56
 57
 58//	    bag.clear();
 59//	    HighlightsSequence.EMPTY;
 60//	OffsetsBag newBag = new OffsetsBag(doc, true);
 61
 62	NBScssParser.ScssEditorParserResult pResult = (NBScssParser.ScssEditorParserResult) result;
 63	CommonTree tree = pResult.getTree();
 64
 65	for (int i = 0; i < tree.getChildCount(); i++) {
 66	    if (tree.getChild(i) != null && tree.getChild(i).getText() != null) {
 67
 68		CommonTree node = ((CommonTree) tree.getChild(i));
 69		//node.
 70		Token token = node.token;
 71
 72		int startRef = NbDocument.findLineOffset((StyledDocument) doc, token.getLine() - 1) + token.getCharPositionInLine();
 73		String str = node.getText();
 74		System.out.println("INTERPOLATION.matcher(str)");
 75		Matcher matcher = INTERPOLATION.matcher(str);
 76		while (matcher.find()) {
 77		    System.out.println("INTERPOLATION found in str " + str);
 78		    int start = startRef + matcher.start();
 79		    int end = startRef + matcher.end();
 80		    System.out.println(" start = <" + start + ">");
 81		    System.out.println(" end = <" + end + ">");
 82		    OffsetRange range = new OffsetRange(start, end);
 83		    highlights.put(range, ColoringAttributes.CUSTOM2_SET);
 84		    OffsetRange varRange = new OffsetRange(start + 2, end - 1);
 85		    highlights.put(varRange, ColoringAttributes.FIELD_SET);
 86		}
 87//		matcher = VAR.matcher(str);
 88//		while (matcher.find()) {
 89//		    System.out.println("VAR found in str " + str);
 90//		    int start = startRef + matcher.start();
 91//		    int end = startRef + matcher.end();
 92//		    System.out.println(" start = <" + start + ">");
 93//		    System.out.println(" end = <" + end + ">");
 94//		    OffsetRange range = new OffsetRange(start, end);
 95//		    highlights.put(range, ColoringAttributes.FIELD_SET);
 96//		}
 97//		if (node.getType() == ScssLexer.IM_SELECTOR) {
 98//		    System.out.println("CSSSemanticAnalyzer run IM_SELECTOR");
 99//		    highlights.put(this.addHighlight(node), ColoringAttributes.METHOD_SET);
100//		    System.out.println("CSSSemanticAnalyzer run IM_SELECTOR fin");
101//
102//		}
103//		if (node.getType() == ScssLexer.IM_PROPERTY) {
104//		    System.out.println("CSSSemanticAnalyzer run IM_PROPERTY");
105//		    highlights.put(this.addHighlight(node), ColoringAttributes.CUSTOM1_SET);
106//		    System.out.println("CSSSemanticAnalyzer run IM_PROPERTY fin");
107//		}
108//		System.out.println("CSSSemanticAnalyzer run VAR before");
109//		if (node.getType() == ScssLexer.IM_VAR) {
110//		    System.out.println("CSSSemanticAnalyzer run VAR");
111//		    highlights.put(this.addHighlight(node), ColoringAttributes.FIELD_SET);
112//		    System.out.println("CSSSemanticAnalyzer run VAR fin");
113//		}
114		//TODO repaint vars
115
116	    }
117
118	}
119
120
121//	final Map<OffsetRange, ColoringAttributes> highlights = new HashMap<OffsetRange, ColoringAttributes>();
122//
123//	//XXX fixthis - the css parser always parses the whole css content!
124//	ParserResult presult = ci.getEmbeddedResults(Css.CSS_MIME_TYPE).iterator().next();
125//	final TranslatedSource source = presult.getTranslatedSource();
126//	SimpleNode root = ((CSSParserResult) presult).root();
127//
128//	if (root == null) {
129//	    //serious error in the source, no results
130//	    semanticHighlights = highlights;
131//	    return;
132//	}
133//
134//	NodeVisitor visitor = new NodeVisitor() {
135//
136//	    //XXX using the ColoringAttributes.YYY java specific codes should
137//	    //be changed to something more meaningful
138//	    public void visit(SimpleNode node) {
139//		if (node.kind() == CSSParserTreeConstants.JJTELEMENTNAME || node.kind() == CSSParserTreeConstants.JJT_CLASS || node.kind() == CSSParserTreeConstants.JJTPSEUDO || node.kind() == CSSParserTreeConstants.JJTHASH || node.kind() == CSSParserTreeConstants.JJTATTRIB) {
140//		    AstOffsetRange range = new AstOffsetRange(node.startOffset(), node.endOffset(), source);
141//		    //filter virtual nodes
142//		    if (!range.isEmpty()) {
143//			highlights.put(range, ColoringAttributes.METHOD);
144//		    }
145//		} else if (node.kind() == CSSParserTreeConstants.JJTPROPERTY) {
146//		    //check vendor speficic property
147//		    AstOffsetRange range = new AstOffsetRange(node.startOffset(), node.endOffset(), source);
148//
149//		    if (!range.isEmpty()) { //filter virtual nodes
150//
151//			String propertyName = node.image().trim();
152//			if (CssAnalyser.isVendorSpecificProperty(propertyName)) {
153//			    //special highlight for vend. spec. properties
154//			    highlights.put(range, ColoringAttributes.PARAMETER_USE);
155//			} else {
156//			    highlights.put(range, ColoringAttributes.PARAMETER);
157//			}
158//		    }
159//		}
160//	    }
161//	};
162//
163//	root.visitChildren(visitor);
164//        for (int i = 0; i < sheet.getCssRules().getLength(); i++) {
165//            if (cancelled) {
166//                return;
167//            }
168//
169//            CSSRule rule = sheet.getCssRules().item(i);
170//            if (rule.getType() == CSSRule.STYLE_RULE) {
171//                CSSStyleRule styleRule = (CSSStyleRule) rule;
172//
173//                CSSStyleDeclaration declaration = styleRule.getStyle();
174//                System.out.println("rule " + styleRule.getCssText() + ": " + declaration.getCssText());
175//
176//                //TODO compute the range - need to hack the parser so it provides offsets
177//                OffsetRange range = new OffsetRange(0, 20);
178//                highlights.put(range, ColoringAttributes.PARAMETER);
179//            }
180//        }
181	System.out.println("highlights size " + highlights.size());
182	semanticHighlights = highlights;
183    }
184
185    private OffsetRange addHighlight(CommonTree node) {
186	CommonTree firstChild = (CommonTree) node.getChild(0);
187	Token token = firstChild.token;
188
189	System.out.println(" IMAGINERY_TOKEN = <" + node.getText() + ">");
190	System.out.println(" subToken.getLine() = <" + token.getLine() + ">");
191	System.out.println(" subToken.getCharPositionInLine() = <" + token.getCharPositionInLine() + ">");
192	int start = NbDocument.findLineOffset((StyledDocument) doc, token.getLine() - 1) + token.getCharPositionInLine();
193	int stop = start + node.getText().length();
194
195	System.out.println(" start = <" + start + ">");
196	System.out.println(" stop = <" + stop + ">");
197	OffsetRange range = new OffsetRange(start, stop);
198	return range;
199//	bag.addHighlight(start, stop, attribute);
200    }
201
202    @Override
203    public int getPriority() {
204	return 102;
205
206
207    }
208
209    @Override
210    public Class<? extends Scheduler> getSchedulerClass() {
211	return Scheduler.EDITOR_SENSITIVE_TASK_SCHEDULER;
212
213    }
214
215    @Override
216    public Map getHighlights() {
217	System.out.println("getHighlights");
218	return semanticHighlights;
219    }
220//    public static class AstOffsetRange extends OffsetRange {
221//
222//        private TranslatedSource source;
223//
224//        public AstOffsetRange(int start, int end, TranslatedSource source) {
225//            super(start, end);
226//            this.source = source;
227//        }
228//
229//        public int getStart() {
230//            return source == null ? super.getStart() : source.getLexicalOffset(super.getStart());
231//        }
232//
233//        public int getEnd() {
234//            return source == null ? super.getEnd() : source.getLexicalOffset(super.getEnd());
235//        }
236//
237//        public boolean isEmpty() {
238//            return getEnd() - getStart() == 0;
239//        }
240//    }
241}