Skip to content

Commit

Permalink
Complete file paths in strings that aren't closed yet.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4866 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
alloy committed Nov 1, 2010
1 parent 01c4c4e commit 9b3f0a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/irb/ext/completion.rb
Expand Up @@ -113,15 +113,28 @@ def results
end

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
if m = (methods || methods_of_object(root))
format_methods(receiver, m, filter)
end
elsif root[TYPE] == :string_literal && root[VALUE][TYPE] == :string_content
# in the form of: "~/code/
expand_path(source)
else
match_methods_vars_or_consts_in_scope(root)
end
result.sort.uniq if result
end
end

def expand_path(source)
tokens = Ripper.lex(source)
path = tokens[0][2]
string_open = tokens[1][2]
end_with_slash = path.length > 1 && path.end_with?('/')
path = File.expand_path(path)
path << '/' if end_with_slash
Dir.glob("#{path}*", File::FNM_CASEFOLD).map { |f| "#{string_open}#{f}" }
end

def unwind_callstack(root, stack = [])
if root[TYPE] == :call
Expand Down
7 changes: 7 additions & 0 deletions spec/ext/completion_spec.rb
Expand Up @@ -249,6 +249,13 @@ def a_local_method; end
end
end

it "completes file paths" do
complete("'/").should == Dir.glob('/*').sort.map { |f| "'#{f}" }
complete("'#{ROOT}/lib/../Ra").should == ["'#{File.join(ROOT, "Rakefile")}"]
complete("%{#{ROOT}/li").should == ['LICENSE', 'lib'].map { |f| "%{#{File.join(ROOT, f)}" }
complete("\"#{ROOT}/lib/").should == ['irb', 'irb.rb'].map { |f| "\"#{File.join(ROOT, 'lib', f)}" }
end

it "does not crash when trying to complete garbage" do
complete("/").should == nil
complete("./Rake").should == nil
Expand Down

0 comments on commit 9b3f0a4

Please sign in to comment.