Skip to content
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

Enable whereami to receive two arguments #2229

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/pry/code.rb
Expand Up @@ -173,17 +173,20 @@ def before(lineno, lines = 1)
end
end

# Remove all lines except for the +lines+ on either side of and including
# +lineno+.
# Remove all lines except for the +before_lines+ before and +after_lines+ after
# and including +lineno+.
#
# @param [Integer] lineno
# @param [Integer] lines
# @param [Integer] before_lines
# @param [Integer] after_lines
# @return [Code]
def around(lineno, lines = 1)
def around(lineno, before_lines = 1, after_lines = nil)
return self unless lineno

after_lines ||= before_lines

select do |loc|
loc.lineno >= lineno - lines && loc.lineno <= lineno + lines
loc.lineno >= lineno - before_lines && loc.lineno <= lineno + after_lines
end
end

Expand Down
10 changes: 6 additions & 4 deletions lib/pry/commands/whereami.rb
Expand Up @@ -22,13 +22,14 @@ class << self
group 'Context'

banner <<-'BANNER'
Usage: whereami [-qn] [LINES]
Usage: whereami [-qn] [-m|-c|-f|LINES...]

Describe the current location. If you use `binding.pry` inside a method then
whereami will print out the source for that method.

If a number is passed, then LINES lines before and after the current line will be
shown instead of the method itself.
shown instead of the method itself. Two numbers can be passed to specify lines
before and after the current line to be shown independently.

The `-q` flag can be used to suppress error messages in the case that there's
no code to show. This is used by pry in the default before_session hook to show
Expand Down Expand Up @@ -150,7 +151,8 @@ def default_code
end

def code_window
Pry::Code.from_file(@file).around(@line, window_size)
before_lines, after_lines = window_size
Pry::Code.from_file(@file).around(@line, before_lines, after_lines)
end

def method_code
Expand Down Expand Up @@ -193,7 +195,7 @@ def window_size
if args.empty?
pry_instance.config.default_window_size
else
args.first.to_i
args.slice(0, 2).map(&:to_i)
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion spec/code_spec.rb
Expand Up @@ -305,11 +305,17 @@ def bound_method
expect(subject.around(2).lines).to eql(%W[1\n 2\n 3\n])
end

context "and we specify how many lines to select" do
context "and we specify how many lines to select by an integer" do
it "selects more than 1 line around" do
expect(subject.around(4, 2).lines).to eql(%W[2\n 3\n 4\n 5\n 6\n])
end
end

context "and we specify how many lines to select by two integers" do
it "selects before and after lines around independently" do
expect(subject.around(4, 2, 3).lines).to eql(%W[2\n 3\n 4\n 5\n 6\n 7\n])
end
end
end
end

Expand Down