mark occurrences for tokens and rules
authorMarek Fukala <mfukala@netbeans.org>
Tue, 15 Jan 2013 11:19:25 +0100
changeset 179305a5600b5542a
parent 17929 f47cd4b5c58f
child 17931 a01a143ef99d
mark occurrences for tokens and rules
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrOccurrencesFinder.java
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/Utils.java
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/fontsColors.xml
     1.1 --- a/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java	Tue Jan 15 11:00:41 2013 +0100
     1.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java	Tue Jan 15 11:19:25 2013 +0100
     1.3 @@ -43,6 +43,7 @@
     1.4  
     1.5  import org.netbeans.core.spi.multiview.MultiViewElement;
     1.6  import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
     1.7 +import org.netbeans.modules.csl.api.OccurrencesFinder;
     1.8  import org.netbeans.modules.csl.api.SemanticAnalyzer;
     1.9  import org.netbeans.modules.csl.api.StructureScanner;
    1.10  import org.netbeans.modules.csl.spi.DefaultLanguageConfig;
    1.11 @@ -111,5 +112,15 @@
    1.12      public SemanticAnalyzer getSemanticAnalyzer() {
    1.13          return new AntlrSemanticAnalyzer();
    1.14      }
    1.15 +
    1.16 +    @Override
    1.17 +    public boolean hasOccurrencesFinder() {
    1.18 +        return true;
    1.19 +    }
    1.20 +
    1.21 +    @Override
    1.22 +    public OccurrencesFinder getOccurrencesFinder() {
    1.23 +        return new AntlrOccurrencesFinder();
    1.24 +    }
    1.25      
    1.26  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrOccurrencesFinder.java	Tue Jan 15 11:19:25 2013 +0100
     2.3 @@ -0,0 +1,135 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * If you wish your version of this file to be governed by only the CDDL
    2.31 + * or only the GPL Version 2, indicate your decision by adding
    2.32 + * "[Contributor] elects to include this software in this distribution
    2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.34 + * single choice of license, a recipient has the option to distribute
    2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.36 + * to extend the choice of license to its licensees as provided above.
    2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.38 + * Version 2 license, then the option applies only if the new code is
    2.39 + * made subject to such option by the copyright holder.
    2.40 + *
    2.41 + * Contributor(s):
    2.42 + *
    2.43 + * Portions Copyrighted 2013 Sun Microsystems, Inc.
    2.44 + */
    2.45 +package org.netbeans.modules.antlr.editor;
    2.46 +
    2.47 +import java.util.Collections;
    2.48 +import java.util.HashMap;
    2.49 +import java.util.Map;
    2.50 +import org.netbeans.api.lexer.Token;
    2.51 +import org.netbeans.api.lexer.TokenHierarchy;
    2.52 +import org.netbeans.api.lexer.TokenSequence;
    2.53 +import org.netbeans.modules.csl.api.ColoringAttributes;
    2.54 +import org.netbeans.modules.csl.api.OccurrencesFinder;
    2.55 +import org.netbeans.modules.csl.api.OffsetRange;
    2.56 +import org.netbeans.modules.parsing.api.Snapshot;
    2.57 +import org.netbeans.modules.parsing.spi.Scheduler;
    2.58 +import org.netbeans.modules.parsing.spi.SchedulerEvent;
    2.59 +
    2.60 +/**
    2.61 + *
    2.62 + * @author marekfukala
    2.63 + */
    2.64 +public class AntlrOccurrencesFinder extends OccurrencesFinder<NbAntlrParserResult> {
    2.65 +
    2.66 +    private int caretDocumentPosition;
    2.67 +    private Map<OffsetRange, ColoringAttributes> occurrencesMap = new HashMap<OffsetRange, ColoringAttributes>();
    2.68 +
    2.69 +    @Override
    2.70 +    public void setCaretPosition(int position) {
    2.71 +        caretDocumentPosition = position;
    2.72 +    }
    2.73 +
    2.74 +    @Override
    2.75 +    public Map<OffsetRange, ColoringAttributes> getOccurrences() {
    2.76 +        return occurrencesMap;
    2.77 +    }
    2.78 +
    2.79 +    @Override
    2.80 +    public void run(NbAntlrParserResult result, SchedulerEvent event) {
    2.81 +        if (caretDocumentPosition == -1) {
    2.82 +            return;
    2.83 +        }
    2.84 +
    2.85 +        //uses just lexer
    2.86 +        TokenHierarchy<?> tokenHierarchy = result.getSnapshot().getTokenHierarchy();
    2.87 +        TokenSequence<AntlrTokenId> ts = tokenHierarchy.tokenSequence(AntlrTokenId.language());
    2.88 +        int diff = ts.move(caretDocumentPosition);
    2.89 +
    2.90 +        Token<AntlrTokenId> curr = null;
    2.91 +        if (diff == 0) {
    2.92 +            if (ts.movePrevious()) {
    2.93 +                curr = ts.token();
    2.94 +            }
    2.95 +        } else {
    2.96 +            if (ts.moveNext()) {
    2.97 +                curr = ts.token();
    2.98 +            }
    2.99 +        }
   2.100 +        if (curr == null) {
   2.101 +            return;
   2.102 +        }
   2.103 +
   2.104 +        switch (curr.id()) {
   2.105 +            case TOKEN_REF:
   2.106 +            case RULE_REF:
   2.107 +                break;
   2.108 +            default:
   2.109 +                return;
   2.110 +        }
   2.111 +
   2.112 +        occurrencesMap.clear();
   2.113 +
   2.114 +        //we are on a token_ref or rule_ref token
   2.115 +        ts.moveStart();
   2.116 +        while (ts.moveNext()) {
   2.117 +            Token<AntlrTokenId> token = ts.token();
   2.118 +            if (token.id() == curr.id() && Utils.equals(token.text(), curr.text(), false, false)) {
   2.119 +                occurrencesMap.put(new OffsetRange(ts.offset(), ts.offset() + token.length()), ColoringAttributes.MARK_OCCURRENCES);
   2.120 +            }
   2.121 +        }
   2.122 +
   2.123 +    }
   2.124 +
   2.125 +    @Override
   2.126 +    public int getPriority() {
   2.127 +        return 100;
   2.128 +    }
   2.129 +
   2.130 +    @Override
   2.131 +    public Class<? extends Scheduler> getSchedulerClass() {
   2.132 +        return null;
   2.133 +    }
   2.134 +
   2.135 +    @Override
   2.136 +    public void cancel() {
   2.137 +    }
   2.138 +}
     3.1 --- a/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/Utils.java	Tue Jan 15 11:00:41 2013 +0100
     3.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/Utils.java	Tue Jan 15 11:19:25 2013 +0100
     3.3 @@ -46,6 +46,7 @@
     3.4  import org.antlr.runtime.tree.Tree;
     3.5  import org.netbeans.modules.antlr.editor.gen.ANTLRv3Parser;
     3.6  import org.netbeans.modules.csl.api.OffsetRange;
     3.7 +import org.openide.util.Parameters;
     3.8  
     3.9  /**
    3.10   *
    3.11 @@ -53,6 +54,25 @@
    3.12   */
    3.13  public class Utils {
    3.14   
    3.15 +     /** @param optimized - first sequence is lowercase, one call to Character.toLowerCase() only*/
    3.16 +    public static boolean equals(CharSequence text1, CharSequence text2, boolean ignoreCase, boolean optimized) {
    3.17 +        Parameters.notNull("text1", text1);
    3.18 +        Parameters.notNull("text2", text2);
    3.19 +        if (text1.length() != text2.length()) {
    3.20 +            return false;
    3.21 +        } else {
    3.22 +            //compare content
    3.23 +            for (int i = 0; i < text1.length(); i++) {
    3.24 +                char ch1 = ignoreCase && !optimized ? Character.toLowerCase(text1.charAt(i)) : text1.charAt(i);
    3.25 +                char ch2 = ignoreCase ? Character.toLowerCase(text2.charAt(i)) : text2.charAt(i);
    3.26 +                if (ch1 != ch2) {
    3.27 +                    return false;
    3.28 +                }
    3.29 +            }
    3.30 +            return true;
    3.31 +        }
    3.32 +    }
    3.33 +    
    3.34      /**
    3.35       * Returns a pointer to the start and end of the token image in the
    3.36       * underlaying stream. The token.getStopIndex() points to the last character
     4.1 --- a/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/fontsColors.xml	Tue Jan 15 11:00:41 2013 +0100
     4.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/fontsColors.xml	Tue Jan 15 11:19:25 2013 +0100
     4.3 @@ -69,4 +69,7 @@
     4.4           <font style="bold" />
     4.5      </fontcolor>
     4.6      
     4.7 +    <!--mark occurrences:-->
     4.8 +    <fontcolor name="mark-occurrences" bgColor="ECEBA3"/>
     4.9 +    
    4.10  </fontscolors>