Repository navigation
Support ls command from irb #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| require 'objspace' | ||
| require 'pp' | ||
| require "irb" | ||
|
|
||
| require_relative 'frame_info' | ||
| require_relative 'color' | ||
|
|
@@ -15,6 +16,7 @@ def self.current | |
| end | ||
| end | ||
|
|
||
| extend IRB::ExtendCommandBundle | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This module injects irb commands as methods. Pretty handy 👍 |
||
| include Color | ||
|
|
||
| attr_reader :location, :thread, :mode, :id | ||
|
|
@@ -579,6 +581,26 @@ def wait_next_action | |
| else | ||
| raise "unsupported frame operation: #{arg.inspect}" | ||
| end | ||
| event! :result, nil | ||
| when :irb | ||
| cmd, arg_expr = *args | ||
|
|
||
| raise "unsupported irb command: #{cmd}" unless cmd == :ls | ||
|
|
||
| begin | ||
| # most irb commands require irb workplace or irb session present in the environment to work | ||
| init_irb_context | ||
|
|
||
| if arg_expr | ||
| arg = frame_eval(arg_expr) | ||
| self.class.send("irb_#{cmd}", arg) | ||
| else | ||
| self.class.send("irb_#{cmd}") | ||
| end | ||
| ensure | ||
| reset_irb_context | ||
| end | ||
|
|
||
| event! :result, nil | ||
| when :show | ||
| type = args.shift | ||
|
|
@@ -658,5 +680,16 @@ def to_s | |
| str += " (not under control)" unless self.mode | ||
| str | ||
| end | ||
|
|
||
| def init_irb_context | ||
| IRB.init_config(nil) | ||
| workspace = IRB::WorkSpace.new(current_frame.binding) | ||
| irb = IRB::Irb.new(workspace, nil) | ||
| IRB.conf[:MAIN_CONTEXT] = irb.context | ||
| end | ||
|
|
||
| def reset_irb_context | ||
| IRB.conf[:MAIN_CONTEXT] = nil | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this is needed, but just in case. |
||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative '../support/test_case' | ||
|
|
||
| module DEBUGGER__ | ||
| class LsTest < TestCase | ||
| def program | ||
| <<~RUBY | ||
| 1| class Foo | ||
| 2| def initialize | ||
| 3| @var = "foobar" | ||
| 4| end | ||
| 5| | ||
| 6| def bar; end | ||
| 7| def self.baz; end | ||
| 8| end | ||
| 9| | ||
| 10| foo = Foo.new | ||
| 11| | ||
| 12| binding.b | ||
| RUBY | ||
| end | ||
|
|
||
| def test_ls_lists_local_variables | ||
| debug_code(program, remote: false) do | ||
| type 'c' | ||
| type 'ls' | ||
| assert_line_text(/locals.*: _ foo/) | ||
| type 'c' | ||
| end | ||
| end | ||
|
|
||
| def test_ls_lists_object_info | ||
| debug_code(program, remote: false) do | ||
| type 'c' | ||
| type 'ls foo' | ||
| assert_line_text([ | ||
| /Foo#methods.*: bar/, | ||
| /instance variables.*: @var/ | ||
| ]) | ||
| type 'c' | ||
| end | ||
| end | ||
|
|
||
| def test_ls_lists_class_info | ||
| debug_code(program, remote: false) do | ||
| type 'c' | ||
| type 'ls Foo' | ||
| assert_line_text( | ||
| [ | ||
| /Class#methods.*: allocate/, | ||
| /Foo\.methods.*: baz/, | ||
| ] | ||
| ) | ||
| type 'c' | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason it can't run on remote console either. I think we can sort it out later.