Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Don't break when trying to complete a string or garbage. Fix for MacR…

…uby ticket #943.

Also add TODO about completing file paths in strings.
  • Loading branch information...
commit 9d512958bc2bddb52ca76f0baaac238b64c4c995 1 parent 421e265
@alloy authored
Showing with 22 additions and 19 deletions.
  1. +1 −0  TODO
  2. +15 −18 lib/irb/ext/completion.rb
  3. +6 −1 spec/ext/completion_spec.rb
View
1  TODO
@@ -1,3 +1,4 @@
+* Complete file paths in strings (for require etc)
* Write docs for using a as library. Probably right after creating a Cocoa client.
* Add specs for irb/driver/socket, and possibly for bin/dietrb
* Configurable history file? (:HISTORY_FILE) Configurable number of saved history lines? (:SAVE_HISTORY)
View
33 lib/irb/ext/completion.rb
@@ -89,11 +89,10 @@ def results
call = (source[-1,1] == '.')
receiver = source = source[0..-2] if call
- if sexp = Ripper::SexpBuilder.new(source).parse
- # [:program, [:stmts_add, [:stmts_new], [x, …]]]
- # ^
- root = sexp[1][2]
-
+ # root node:
+ # [:program, [:stmts_add, [:stmts_new], [x, …]]]
+ # ^
+ if (sexp = Ripper::SexpBuilder.new(source).parse) && root = sexp[1][2]
# [:call, [:hash, nil], :".", [:@ident, x, …]]
if root[TYPE] == :call
call = true
@@ -113,17 +112,14 @@ def results
end
end
- if call
- if results = (methods || methods_of_object(root))
- format_methods(receiver, results, filter)
- else
- # this is mainly because MacRuby currently has a problem with local_variables,
- # normally I'd prefer to let the program die so the user might report it
- []
- end
- else
- match_methods_vars_or_consts_in_scope(root)
- end.sort.uniq
+ result = if call
+ if m = (methods || methods_of_object(root))
+ format_methods(receiver, m, filter)
+ end
+ else
+ match_methods_vars_or_consts_in_scope(root)
+ end
+ result.sort.uniq if result
end
end
@@ -140,7 +136,7 @@ def unwind_callstack(root, stack = [])
def match_methods_vars_or_consts_in_scope(symbol)
var = symbol[VALUE]
filter = var[VALUE]
- case var[TYPE]
+ result = case var[TYPE]
when :@ident
local_variables + instance_methods + RESERVED_DOWNCASE_WORDS
when :@ivar
@@ -154,7 +150,8 @@ def match_methods_vars_or_consts_in_scope(symbol)
else
constants + RESERVED_UPCASE_WORDS
end
- end.grep(/^#{Regexp.quote(filter)}/)
+ end
+ (result && filter) ? result.grep(/^#{Regexp.quote(filter)}/) : result
end
def format_methods(receiver, methods, filter)
View
7 spec/ext/completion_spec.rb
@@ -248,4 +248,9 @@ def a_local_method; end
complete(word[0..-2]).should include(word)
end
end
-end
+
+ it "does not crash when trying to complete garbage" do
+ complete("/").should == nil
+ complete("./Rake").should == nil
+ end
+end

0 comments on commit 9d51295

Please sign in to comment.
Something went wrong with that request. Please try again.