zero-sum cleanup
authorenebo@netbeans.org
Sat, 19 Apr 2014 16:26:21 -0500
changeset 4557123da219e4f8
parent 4556 e276e451257b
child 4558 6d64ad69ba83
zero-sum cleanup
ruby/src/org/netbeans/modules/ruby/RubyCodeCompleter.java
     1.1 --- a/ruby/src/org/netbeans/modules/ruby/RubyCodeCompleter.java	Sun Dec 08 12:53:22 2013 -0600
     1.2 +++ b/ruby/src/org/netbeans/modules/ruby/RubyCodeCompleter.java	Sat Apr 19 16:26:21 2014 -0500
     1.3 @@ -2211,69 +2211,59 @@
     1.4          caretOffset = AstUtilities.boundCaretOffset(info, caretOffset);
     1.5  
     1.6          Node root = AstUtilities.getRoot(info);
     1.7 -
     1.8 -        if (root == null) {
     1.9 -            return null;
    1.10 -        }
    1.11 +        if (root == null) return null;
    1.12  
    1.13          AstPath path = new AstPath(root, caretOffset);
    1.14          Node closest = path.leaf();
    1.15          if (closest == null) return null;
    1.16  
    1.17 -        // TODO: Look for a unique global variable -- this requires looking at the index
    1.18 -        if (prefix.startsWith("$")) return null;
    1.19 -        // TODO: Look for a unique class variable -- this requires looking at superclasses and other class parts
    1.20 -        if (prefix.startsWith("@@")) return null;
    1.21 -        // TODO: Look for a unique instance variable -- this requires looking at superclasses and other class parts
    1.22 -        if (prefix.startsWith("@")) return null;
    1.23 +        // TODO: Look for a unique {global,class,instance} variable -- this requires looking at the index
    1.24 +        if (prefix.startsWith("$") || prefix.startsWith("@")) return null;
    1.25  
    1.26 -            // Look for a local variable in the given scope
    1.27 -                Node method = AstUtilities.findLocalScope(closest, path);
    1.28 -                Map<String, Node> variables = new HashMap<String, Node>();
    1.29 -                addLocals(method, variables);
    1.30 +        // Look for a local variable in the given scope
    1.31 +        Node method = AstUtilities.findLocalScope(closest, path);
    1.32 +        Map<String, Node> variables = new HashMap<String, Node>();
    1.33 +        addLocals(method, variables);
    1.34  
    1.35 -                List<Node> applicableBlocks = AstUtilities.getApplicableBlocks(path, false);
    1.36 -                for (Node block : applicableBlocks) {
    1.37 -                    addDynamic(block, variables);
    1.38 +        for (Node block : AstUtilities.getApplicableBlocks(path, false)) {
    1.39 +            addDynamic(block, variables);
    1.40 +        }
    1.41 +                
    1.42 +        // See if we have any name suggestions
    1.43 +        String suggestions = (String) params.get(ATTR_DEFAULTS);
    1.44 +
    1.45 +        // Check the suggestions
    1.46 +        if (suggestions != null && !suggestions.isEmpty()) {
    1.47 +            String[] names = suggestions.split(",");
    1.48 +
    1.49 +            for (String suggestion : names) {
    1.50 +                if (!variables.containsKey(suggestion)) return suggestion;
    1.51 +            }
    1.52 +
    1.53 +            // Try some variations of the name
    1.54 +            for (String suggestion : names) {
    1.55 +                for (int number = 2; number < 5; number++) {
    1.56 +                    String name = suggestion + number;
    1.57 +
    1.58 +                    if (!variables.containsKey(name)) return name;
    1.59                  }
    1.60 -                
    1.61 -                // See if we have any name suggestions
    1.62 -                String suggestions = (String)params.get(ATTR_DEFAULTS);
    1.63 +            }
    1.64 +        }
    1.65  
    1.66 -                // Check the suggestions
    1.67 -                if ((suggestions != null) && (suggestions.length() > 0)) {
    1.68 -                    String[] names = suggestions.split(",");
    1.69 +        // Try the prefix
    1.70 +        if (!prefix.isEmpty() && !variables.containsKey(prefix)) return prefix;
    1.71  
    1.72 -                    for (String suggestion : names) {
    1.73 -                        if (!variables.containsKey(suggestion)) {
    1.74 -                            return suggestion;
    1.75 -                        }
    1.76 -                    }
    1.77 +        // TODO: What's the right algorithm for uniqueifying a variable name in Ruby?
    1.78 +        // For now, will just append a number
    1.79 +        if (isEmpty(prefix)) prefix = "var";
    1.80  
    1.81 -                    // Try some variations of the name
    1.82 -                    for (String suggestion : names) {
    1.83 -                        for (int number = 2; number < 5; number++) {
    1.84 -                            String name = suggestion + number;
    1.85 +        for (int number = 1; number < 15; number++) {
    1.86 +            String name = number == 1 ? prefix : (prefix + number);
    1.87  
    1.88 -                            if (name.length() > 0 && !variables.containsKey(name)) return name;
    1.89 -                        }
    1.90 -                    }
    1.91 -                }
    1.92 +            if (!variables.containsKey(name)) return name;
    1.93 +        }
    1.94  
    1.95 -                // Try the prefix
    1.96 -                if (prefix.length() > 0 && !variables.containsKey(prefix)) return prefix;
    1.97 -
    1.98 -                // TODO: What's the right algorithm for uniqueifying a variable name in Ruby?
    1.99 -                // For now, will just append a number
   1.100 -                if (isEmpty(prefix)) prefix = "var";
   1.101 -
   1.102 -                for (int number = 1; number < 15; number++) {
   1.103 -                    String name = (number == 1) ? prefix : (prefix + number);
   1.104 -
   1.105 -                    if ((name.length() > 0) && !variables.containsKey(name)) return name;
   1.106 -                }
   1.107 -
   1.108 -            return null;
   1.109 +        return null;
   1.110      }
   1.111  
   1.112      @Override