From b9ea02471eb217e88a2cf105640f25ab3a856546 Mon Sep 17 00:00:00 2001 From: taka-ebato Date: Fri, 31 Dec 2021 21:21:43 +0900 Subject: [PATCH 1/2] Enable whereami to receive two args --- lib/pry/code.rb | 13 ++++++++----- lib/pry/commands/whereami.rb | 5 +++-- spec/code_spec.rb | 8 +++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/pry/code.rb b/lib/pry/code.rb index 1fb206c48..d213f97cc 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -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 diff --git a/lib/pry/commands/whereami.rb b/lib/pry/commands/whereami.rb index f18026e44..1ed6f23af 100644 --- a/lib/pry/commands/whereami.rb +++ b/lib/pry/commands/whereami.rb @@ -150,7 +150,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 @@ -193,7 +194,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 diff --git a/spec/code_spec.rb b/spec/code_spec.rb index 4ae2ca0b4..fda5916e7 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -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 From 2bb91fe53e7f3b65bde131b34726cb4fb589a660 Mon Sep 17 00:00:00 2001 From: taka-ebato Date: Sun, 9 Jan 2022 15:28:46 +0900 Subject: [PATCH 2/2] Updated the doc of whereami related to receiving an extra param --- lib/pry/commands/whereami.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pry/commands/whereami.rb b/lib/pry/commands/whereami.rb index 1ed6f23af..5936c9a97 100644 --- a/lib/pry/commands/whereami.rb +++ b/lib/pry/commands/whereami.rb @@ -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