Class: Pry::Command::Whereami
Constant Summary
Constants inherited
from Pry::Command
VOID_VALUE
Helpers::DocumentationHelpers::YARD_TAGS
Helpers::Text::COLORS
Class Attribute Summary collapse
#args, #opts
Attributes inherited from Pry::Command
#arg_string, #captures, #command_block, #command_set, #context, #eval_string, #hooks, #output, #pry_instance, #target
Instance Method Summary
collapse
#call, #complete, doc, #help, inherited, #slop, source, source_file, source_line, source_location, source_object, #subcommands
#_pry_, #after_hooks, banner, #before_hooks, #block, #call_safely, #call_with_hooks, #check_for_command_collision, #command_name, command_name, #command_options, command_regex, #commands, #complete, convert_to_regex, default_options, #description, doc, #find_hooks, group, inspect, #interpolate_string, #match, match_score, matches?, #name, name, #normalize_method_args, options, #pass_block, #process_line, #run, #source, source, source_file, source_line, state, #state, subclass, #target_self, #tokenize, #use_unpatched_symbol, #void
#get_comment_content, get_comment_content, process_comment_markup, #process_comment_markup, process_rdoc, #process_rdoc, #process_yardoc, process_yardoc, process_yardoc_tag, #process_yardoc_tag, #strip_comments_from_c_code, strip_comments_from_c_code, #strip_leading_whitespace, strip_leading_whitespace
#c_method?, #c_module?, #command?, #module_with_yard_docs?, #real_method_object?
#bold, #default, #indent, #no_color, #no_pager, #strip_color, #with_line_numbers
#absolute_index_number, #absolute_index_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #unindent
#method_object, method_object, method_options, #method_options
#colorize_code, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?
Constructor Details
Returns a new instance of Whereami.
8
9
10
11
12
|
# File 'lib/pry/commands/whereami.rb', line 8
def initialize(*)
super
@method_code = nil
end
|
Class Attribute Details
.method_size_cutoff ⇒ Object
Returns the value of attribute method_size_cutoff.
15
16
17
|
# File 'lib/pry/commands/whereami.rb', line 15
def method_size_cutoff
@method_size_cutoff
end
|
Instance Method Details
#bad_option_combination? ⇒ Boolean
83
84
85
86
|
# File 'lib/pry/commands/whereami.rb', line 83
def bad_option_combination?
[opts.present?(:m), opts.present?(:f),
opts.present?(:c), args.any?].count(true) > 1
end
|
#class_code ⇒ Object
171
172
173
174
175
176
177
178
|
# File 'lib/pry/commands/whereami.rb', line 171
def class_code
@class_code ||=
begin
mod = @method ? Pry::WrappedModule(@method.owner) : target_class
idx = mod.candidates.find_index { |v| expand_path(v.source_file) == @file }
idx && Pry::Code.from_module(mod, idx)
end
end
|
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/pry/commands/whereami.rb', line 63
def code
@code ||= if opts.present?(:m)
method_code || raise(CommandError, "Cannot find method code.")
elsif opts.present?(:c)
class_code || raise(CommandError, "Cannot find class code.")
elsif opts.present?(:f)
Pry::Code.from_file(@file)
elsif args.any?
code_window
else
default_code
end
end
|
#code? ⇒ Boolean
77
78
79
80
81
|
# File 'lib/pry/commands/whereami.rb', line 77
def code?
!!code
rescue MethodSource::SourceNotFoundError
false
end
|
#code_window ⇒ Object
152
153
154
|
# File 'lib/pry/commands/whereami.rb', line 152
def code_window
Pry::Code.from_file(@file).around(@line, window_size)
end
|
#default_code ⇒ Object
144
145
146
147
148
149
150
|
# File 'lib/pry/commands/whereami.rb', line 144
def default_code
if method_code && small_method?
method_code
else
code_window
end
end
|
#expand_path(filename) ⇒ Object
185
186
187
188
189
190
|
# File 'lib/pry/commands/whereami.rb', line 185
def expand_path(filename)
return unless filename
return filename if Pry.eval_path == filename
File.expand_path(filename)
end
|
#handle_internal_binding ⇒ Object
132
133
134
135
136
137
138
|
# File 'lib/pry/commands/whereami.rb', line 132
def handle_internal_binding
if top_level?
output.puts "At the top level."
else
output.puts "Inside #{Pry.view_clip(target_self)}."
end
end
|
88
89
90
|
# File 'lib/pry/commands/whereami.rb', line 88
def location
"#{@file}:#{@line} #{@method && @method.name_with_owner}"
end
|
124
125
126
|
# File 'lib/pry/commands/whereami.rb', line 124
def marker
!opts.present?(:n) && @line
end
|
#method_code ⇒ Object
156
157
158
159
160
|
# File 'lib/pry/commands/whereami.rb', line 156
def method_code
return @method_code if @method_code
@method_code = Pry::Code.from_method(@method) if valid_method?
end
|
#nothing_to_do? ⇒ Boolean
116
117
118
|
# File 'lib/pry/commands/whereami.rb', line 116
def nothing_to_do?
opts.quiet? && (internal_binding?(target) || !code?)
end
|
#options(opt) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/pry/commands/whereami.rb', line 55
def options(opt)
opt.on :q, :quiet, "Don't display anything in case of an error"
opt.on :n, :"no-line-numbers", "Do not display line numbers"
opt.on :m, :method, "Show the complete source for the current method."
opt.on :c, :class, "Show the complete source for the current class or module."
opt.on :f, :file, "Show the complete source for the current file."
end
|
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/pry/commands/whereami.rb', line 92
def process
if bad_option_combination?
raise CommandError, "Only one of -m, -c, -f, and LINES may be specified."
end
return if nothing_to_do?
if internal_binding?(target)
handle_internal_binding
return
end
set_file_and_dir_locals(@file)
pretty_code = code.with_line_numbers(use_line_numbers?)
.with_marker(marker)
.highlighted
pry_instance..page(
"\n#{bold('From:')} #{location}:\n\n" + pretty_code + "\n"
)
end
|
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/pry/commands/whereami.rb', line 44
def setup
if target.respond_to?(:source_location)
file, @line = target.source_location
@file = expand_path(file)
else
@file = expand_path(target.eval('__FILE__'))
@line = target.eval('__LINE__')
end
@method = Pry::Method.from_binding(target)
end
|
#small_method? ⇒ Boolean
140
141
142
|
# File 'lib/pry/commands/whereami.rb', line 140
def small_method?
@method.source_range.count < self.class.method_size_cutoff
end
|
This either returns the target_self
or it returns the class of target_self
if target_self
is not a class.
165
166
167
168
169
|
# File 'lib/pry/commands/whereami.rb', line 165
def target_class
return Pry::WrappedModule(target_self) if target_self.is_a?(Module)
Pry::WrappedModule(target_self.class)
end
|
#top_level? ⇒ Boolean
128
129
130
|
# File 'lib/pry/commands/whereami.rb', line 128
def top_level?
target_self == Pry.main
end
|
#use_line_numbers? ⇒ Boolean
120
121
122
|
# File 'lib/pry/commands/whereami.rb', line 120
def use_line_numbers?
!opts.present?(:n)
end
|
#valid_method? ⇒ Boolean
180
181
182
183
|
# File 'lib/pry/commands/whereami.rb', line 180
def valid_method?
@method && @method.source? && expand_path(@method.source_file) == @file &&
@method.source_range.include?(@line)
end
|
#window_size ⇒ Object
192
193
194
195
196
197
198
|
# File 'lib/pry/commands/whereami.rb', line 192
def window_size
if args.empty?
pry_instance.config.default_window_size
else
args.first.to_i
end
end
|