instant rename
authorMarek Fukala <mfukala@netbeans.org>
Tue, 15 Jan 2013 16:36:31 +0100
changeset 17936d6a12c4895fc
parent 17935 793b6a07a49b
child 17937 e95c277d7c2b
instant rename
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java
o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrInstantRenamer.java
     1.1 --- a/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java	Tue Jan 15 16:03:36 2013 +0100
     1.2 +++ b/o.n.antlr.editor/src/org/netbeans/modules/antlr/editor/AntlrCslLanguage.java	Tue Jan 15 16:36:31 2013 +0100
     1.3 @@ -46,6 +46,7 @@
     1.4  import org.netbeans.modules.csl.api.CodeCompletionHandler;
     1.5  import org.netbeans.modules.csl.api.DeclarationFinder;
     1.6  import org.netbeans.modules.csl.api.Formatter;
     1.7 +import org.netbeans.modules.csl.api.InstantRenamer;
     1.8  import org.netbeans.modules.csl.api.OccurrencesFinder;
     1.9  import org.netbeans.modules.csl.api.SemanticAnalyzer;
    1.10  import org.netbeans.modules.csl.api.StructureScanner;
    1.11 @@ -150,7 +151,10 @@
    1.12      public boolean hasFormatter() {
    1.13          return true;
    1.14      }
    1.15 -    
    1.16 -    
    1.17 -    
    1.18 +
    1.19 +    @Override
    1.20 +    public InstantRenamer getInstantRenamer() {
    1.21 +        return new AntlrInstantRenamer();
    1.22 +    }
    1.23 +        
    1.24  }
     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/AntlrInstantRenamer.java	Tue Jan 15 16:36:31 2013 +0100
     2.3 @@ -0,0 +1,118 @@
     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.HashSet;
    2.48 +import java.util.Set;
    2.49 +import org.netbeans.api.lexer.Token;
    2.50 +import org.netbeans.api.lexer.TokenHierarchy;
    2.51 +import org.netbeans.api.lexer.TokenSequence;
    2.52 +import static org.netbeans.modules.antlr.editor.AntlrTokenId.RULE_REF;
    2.53 +import static org.netbeans.modules.antlr.editor.AntlrTokenId.TOKEN_REF;
    2.54 +import org.netbeans.modules.csl.api.ColoringAttributes;
    2.55 +import org.netbeans.modules.csl.api.InstantRenamer;
    2.56 +import org.netbeans.modules.csl.api.OffsetRange;
    2.57 +import org.netbeans.modules.csl.spi.ParserResult;
    2.58 +
    2.59 +/**
    2.60 + *
    2.61 + * @author marekfukala
    2.62 + */
    2.63 +public class AntlrInstantRenamer implements InstantRenamer {
    2.64 +
    2.65 +    @Override
    2.66 +    public boolean isRenameAllowed(ParserResult info, int caretOffset, String[] explanationRetValue) {
    2.67 +         //uses just lexer
    2.68 +        TokenHierarchy<?> tokenHierarchy = info.getSnapshot().getTokenHierarchy();
    2.69 +        TokenSequence<AntlrTokenId> ts = tokenHierarchy.tokenSequence(AntlrTokenId.language());
    2.70 +        int diff = ts.move(caretOffset);
    2.71 +
    2.72 +        Token<AntlrTokenId> curr = null;
    2.73 +        if (diff == 0) {
    2.74 +            if (ts.movePrevious()) {
    2.75 +                curr = ts.token();
    2.76 +            }
    2.77 +        } else {
    2.78 +            if (ts.moveNext()) {
    2.79 +                curr = ts.token();
    2.80 +            }
    2.81 +        }
    2.82 +        return curr != null && (curr.id() == AntlrTokenId.TOKEN_REF || curr.id() == AntlrTokenId.RULE_REF);
    2.83 +    }
    2.84 +
    2.85 +    @Override
    2.86 +    public Set<OffsetRange> getRenameRegions(ParserResult info, int caretOffset) {
    2.87 +        Set<OffsetRange> regions = new HashSet<OffsetRange>();
    2.88 +        //uses just lexer
    2.89 +        TokenHierarchy<?> tokenHierarchy = info.getSnapshot().getTokenHierarchy();
    2.90 +        TokenSequence<AntlrTokenId> ts = tokenHierarchy.tokenSequence(AntlrTokenId.language());
    2.91 +        int diff = ts.move(caretOffset);
    2.92 +
    2.93 +        Token<AntlrTokenId> curr = null;
    2.94 +        if (diff == 0) {
    2.95 +            if (ts.movePrevious()) {
    2.96 +                curr = ts.token();
    2.97 +            }
    2.98 +        } else {
    2.99 +            if (ts.moveNext()) {
   2.100 +                curr = ts.token();
   2.101 +            }
   2.102 +        }
   2.103 +        if (curr != null) {
   2.104 +            switch (curr.id()) {
   2.105 +                case TOKEN_REF:
   2.106 +                case RULE_REF:
   2.107 +                    ts.moveStart();
   2.108 +                    while (ts.moveNext()) {
   2.109 +                        Token<AntlrTokenId> token = ts.token();
   2.110 +                        if (token.id() == curr.id() && Utils.equals(token.text(), curr.text(), false, false)) {
   2.111 +                            regions.add(new OffsetRange(ts.offset(), ts.offset() + token.length()));
   2.112 +                        }
   2.113 +                    }
   2.114 +                    break;                    
   2.115 +            }
   2.116 +            
   2.117 +        }
   2.118 +        return regions;
   2.119 +    }
   2.120 +    
   2.121 +}