# HG changeset patch # User enebo@netbeans.org # Date 1397942781 18000 # Node ID 123da219e4f87821c714859dfe5fb0148c764aa4 # Parent e276e451257b70fdf00f9baf1cf7b551b481e2ac zero-sum cleanup diff -r e276e451257b -r 123da219e4f8 ruby/src/org/netbeans/modules/ruby/RubyCodeCompleter.java --- a/ruby/src/org/netbeans/modules/ruby/RubyCodeCompleter.java Sun Dec 08 12:53:22 2013 -0600 +++ b/ruby/src/org/netbeans/modules/ruby/RubyCodeCompleter.java Sat Apr 19 16:26:21 2014 -0500 @@ -2211,69 +2211,59 @@ caretOffset = AstUtilities.boundCaretOffset(info, caretOffset); Node root = AstUtilities.getRoot(info); - - if (root == null) { - return null; - } + if (root == null) return null; AstPath path = new AstPath(root, caretOffset); Node closest = path.leaf(); if (closest == null) return null; - // TODO: Look for a unique global variable -- this requires looking at the index - if (prefix.startsWith("$")) return null; - // TODO: Look for a unique class variable -- this requires looking at superclasses and other class parts - if (prefix.startsWith("@@")) return null; - // TODO: Look for a unique instance variable -- this requires looking at superclasses and other class parts - if (prefix.startsWith("@")) return null; + // TODO: Look for a unique {global,class,instance} variable -- this requires looking at the index + if (prefix.startsWith("$") || prefix.startsWith("@")) return null; - // Look for a local variable in the given scope - Node method = AstUtilities.findLocalScope(closest, path); - Map variables = new HashMap(); - addLocals(method, variables); + // Look for a local variable in the given scope + Node method = AstUtilities.findLocalScope(closest, path); + Map variables = new HashMap(); + addLocals(method, variables); - List applicableBlocks = AstUtilities.getApplicableBlocks(path, false); - for (Node block : applicableBlocks) { - addDynamic(block, variables); + for (Node block : AstUtilities.getApplicableBlocks(path, false)) { + addDynamic(block, variables); + } + + // See if we have any name suggestions + String suggestions = (String) params.get(ATTR_DEFAULTS); + + // Check the suggestions + if (suggestions != null && !suggestions.isEmpty()) { + String[] names = suggestions.split(","); + + for (String suggestion : names) { + if (!variables.containsKey(suggestion)) return suggestion; + } + + // Try some variations of the name + for (String suggestion : names) { + for (int number = 2; number < 5; number++) { + String name = suggestion + number; + + if (!variables.containsKey(name)) return name; } - - // See if we have any name suggestions - String suggestions = (String)params.get(ATTR_DEFAULTS); + } + } - // Check the suggestions - if ((suggestions != null) && (suggestions.length() > 0)) { - String[] names = suggestions.split(","); + // Try the prefix + if (!prefix.isEmpty() && !variables.containsKey(prefix)) return prefix; - for (String suggestion : names) { - if (!variables.containsKey(suggestion)) { - return suggestion; - } - } + // TODO: What's the right algorithm for uniqueifying a variable name in Ruby? + // For now, will just append a number + if (isEmpty(prefix)) prefix = "var"; - // Try some variations of the name - for (String suggestion : names) { - for (int number = 2; number < 5; number++) { - String name = suggestion + number; + for (int number = 1; number < 15; number++) { + String name = number == 1 ? prefix : (prefix + number); - if (name.length() > 0 && !variables.containsKey(name)) return name; - } - } - } + if (!variables.containsKey(name)) return name; + } - // Try the prefix - if (prefix.length() > 0 && !variables.containsKey(prefix)) return prefix; - - // TODO: What's the right algorithm for uniqueifying a variable name in Ruby? - // For now, will just append a number - if (isEmpty(prefix)) prefix = "var"; - - for (int number = 1; number < 15; number++) { - String name = (number == 1) ? prefix : (prefix + number); - - if ((name.length() > 0) && !variables.containsKey(name)) return name; - } - - return null; + return null; } @Override