ruby/src/org/netbeans/modules/ruby/RubyRenameHandler.java
author enebo@netbeans.org
Sun, 08 Dec 2013 12:53:22 -0600
changeset 4556 e276e451257b
parent 4555 3773928e70d0
permissions -rw-r--r--
Remove some fixed todo's and moved one to jruby-parser as an issue: https://github.com/jruby/jruby-parser/issues/29. removed a bunch of todos I don't understand (RFE's and mixture of things which do not seem to exist anymore)
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    31  * Microsystems, Inc. All Rights Reserved.
    32  *
    33  * If you wish your version of this file to be governed by only the CDDL
    34  * or only the GPL Version 2, indicate your decision by adding
    35  * "[Contributor] elects to include this software in this distribution
    36  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    37  * single choice of license, a recipient has the option to distribute
    38  * your version of this file under either the CDDL, the GPL Version 2 or
    39  * to extend the choice of license to its licensees as provided above.
    40  * However, if you add GPL Version 2 code and therefore, elected the GPL
    41  * Version 2 license, then the option applies only if the new code is
    42  * made subject to such option by the copyright holder.
    43  */
    44 package org.netbeans.modules.ruby;
    45 
    46 import java.util.Collections;
    47 import java.util.HashSet;
    48 import java.util.Set;
    49 
    50 import org.jrubyparser.ast.Node;
    51 import org.jrubyparser.ast.ILocalVariable;
    52 import org.netbeans.modules.csl.api.InstantRenamer;
    53 import org.netbeans.modules.csl.api.OffsetRange;
    54 import org.netbeans.modules.csl.spi.ParserResult;
    55 import org.netbeans.modules.ruby.lexer.LexUtilities;
    56 import org.openide.util.NbBundle;
    57 
    58 /**
    59  * Handles renaming of local elements.
    60  */
    61 public class RubyRenameHandler implements InstantRenamer {
    62     @Override
    63     public boolean isRenameAllowed(ParserResult info, int caretOffset, String[] explanationRetValue) {
    64         Node root = AstUtilities.getRoot(info);
    65 
    66         if (root == null) {
    67             explanationRetValue[0] = NbBundle.getMessage(RubyRenameHandler.class, "NoRenameWithErrors");
    68             return false;
    69         }
    70 
    71         Node closest = root.getNodeAt(AstUtilities.getAstOffset(info, caretOffset));
    72         if (closest == null) return false;
    73         if (closest instanceof ILocalVariable) return true;  // All local block/method vars can be renamed
    74 
    75         switch (closest.getNodeType()) {
    76         case INSTASGNNODE: case INSTVARNODE: case CLASSVARDECLNODE: case CLASSVARNODE: case CLASSVARASGNNODE:
    77         case GLOBALASGNNODE: case GLOBALVARNODE: case CONSTDECLNODE: case CONSTNODE: case DEFNNODE: case DEFSNODE:
    78         case FCALLNODE: case CALLNODE: case VCALLNODE: case COLON2NODE: case COLON3NODE: case ALIASNODE:
    79         case SYMBOLNODE: // TODO - what about the string arguments in an alias node? Gotta check those
    80             return true;
    81         }
    82 
    83         return false;
    84     }
    85 
    86     @Override
    87     public Set<OffsetRange> getRenameRegions(ParserResult info, int caretOffset) {
    88         Node variable = AstUtilities.findNodeAtOffset(info, caretOffset);
    89         if (variable == null || !(variable instanceof ILocalVariable)) return Collections.emptySet();
    90 
    91         Set<OffsetRange> regions = new HashSet<OffsetRange>();
    92 
    93         for (ILocalVariable occurrence: ((ILocalVariable) variable).getOccurrences()) {
    94             OffsetRange range = LexUtilities.getLexerOffsets(info, 
    95                     AstUtilities.offsetRangeFor(occurrence.getNamePosition()));
    96 
    97             if (range != OffsetRange.NONE) regions.add(range);
    98         }
    99 
   100         return regions;
   101     }
   102 }