Class: Pry::Command::Whereami

Inherits:
Pry::ClassCommand show all
Defined in:
lib/pry/commands/whereami.rb

Constant Summary

Constants inherited from Pry::Command

VOID_VALUE

Constants included from Helpers::Text

Helpers::Text::COLORS

Class Attribute Summary collapse

Attributes inherited from Pry::ClassCommand

#args, #opts

Attributes inherited from Pry::Command

#_pry_, #arg_string, #captures, #command_block, #command_set, #context, #eval_string, #hooks, #output, #target

Instance Method Summary collapse

Methods inherited from Pry::ClassCommand

#call, #complete, doc, #help, inherited, #slop, source, source_file, source_line, source_location, source_object, #subcommands

Methods inherited from Pry::Command

#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, #correct_arg_arity, default_options, #dependencies_met?, #description, doc, #find_hooks, group, hooks, inspect, #interpolate_string, #match, match_score, matches?, #name, name, options, #pass_block, #process_line, #run, source, #source, source_file, source_line, source_location, #source_location, #state, subclass, #target_self, #text, #tokenize, #use_unpatched_symbol, #void

Methods included from Pry::CodeObject::Helpers

#c_method?, #c_module?, #command?, #module_with_yard_docs?, #real_method_object?

Methods included from Helpers::DocumentationHelpers

#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

Methods included from Helpers::Text

#bold, #default, #indent, #no_color, #no_pager, #strip_color, #with_line_numbers

Methods included from Helpers::CommandHelpers

#absolute_index_number, absolute_index_number, #absolute_index_range, absolute_index_range, #command_error, command_error, #get_method_or_raise, get_method_or_raise, #internal_binding?, internal_binding?, #one_index_number, one_index_number, #one_index_range, one_index_range, #one_index_range_or_number, one_index_range_or_number, #restrict_to_lines, restrict_to_lines, #set_file_and_dir_locals, set_file_and_dir_locals, #temp_file, temp_file, #unindent, unindent

Methods included from Helpers::OptionsHelpers

#method_object, method_object, #method_options, method_options

Methods included from Helpers::BaseHelpers

#colorize_code, #command_dependencies_met?, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Methods included from Platform

#jruby?, #jruby_19?, #known_engines, #linux?, #mac_osx?, #mri?, #mri_19?, #mri_2?, #rbx?, #windows?, #windows_ansi?

Constructor Details

#initializeWhereami

Returns a new instance of Whereami



4
5
6
7
8
# File 'lib/pry/commands/whereami.rb', line 4

def initialize(*)
  super

  @method_code = nil
end

Class Attribute Details

.method_size_cutoffObject

Returns the value of attribute method_size_cutoff



11
12
13
# File 'lib/pry/commands/whereami.rb', line 11

def method_size_cutoff
  @method_size_cutoff
end

Instance Method Details

#bad_option_combination?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/pry/commands/whereami.rb', line 74

def bad_option_combination?
  [opts.present?(:m), opts.present?(:f),
   opts.present?(:c), args.any?].count(true) > 1
end

#class_codeObject (private)



161
162
163
164
165
166
167
168
# File 'lib/pry/commands/whereami.rb', line 161

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

#codeObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pry/commands/whereami.rb', line 54

def code
  @code ||= if opts.present?(:m)
              method_code or raise CommandError, "Cannot find method code."
            elsif opts.present?(:c)
              class_code or 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

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/pry/commands/whereami.rb', line 68

def code?
  !!code
rescue MethodSource::SourceNotFoundError
  false
end

#code_windowObject (private)



141
142
143
# File 'lib/pry/commands/whereami.rb', line 141

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

#default_codeObject (private)



133
134
135
136
137
138
139
# File 'lib/pry/commands/whereami.rb', line 133

def default_code
  if method_code && small_method?
    method_code
  else
    code_window
  end
end

#expand_path(f) ⇒ Object (private)



175
176
177
178
179
180
181
182
183
# File 'lib/pry/commands/whereami.rb', line 175

def expand_path(f)
  return if !f

  if Pry.eval_path == f
    f
  else
    File.expand_path(f)
  end
end

#handle_internal_bindingObject (private)



121
122
123
124
125
126
127
# File 'lib/pry/commands/whereami.rb', line 121

def handle_internal_binding
  if top_level?
    output.puts "At the top level."
  else
    output.puts "Inside #{Pry.view_clip(target_self)}."
  end
end

#locationObject



79
80
81
# File 'lib/pry/commands/whereami.rb', line 79

def location
  "#{@file} @ line #{@line} #{@method && @method.name_with_owner}"
end

#markerObject (private)



113
114
115
# File 'lib/pry/commands/whereami.rb', line 113

def marker
  !opts.present?(:n) && @line
end

#method_codeObject (private)



145
146
147
148
149
150
151
# File 'lib/pry/commands/whereami.rb', line 145

def method_code
  return @method_code if @method_code

  if valid_method?
    @method_code = Pry::Code.from_method(@method)
  end
end

#nothing_to_do?Boolean (private)

Returns:

  • (Boolean)


105
106
107
# File 'lib/pry/commands/whereami.rb', line 105

def nothing_to_do?
  opts.quiet? && (internal_binding?(target) || !code?)
end

#options(opt) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/pry/commands/whereami.rb', line 46

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

#processObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pry/commands/whereami.rb', line 83

def process
  if bad_option_combination?
    raise CommandError, "Only one of -m, -c, -f, and  LINES may be specified."
  end

  if nothing_to_do?
    return
  elsif internal_binding?(target)
    handle_internal_binding
    return
  end

  set_file_and_dir_locals(@file)

  out = "\n#{bold('From:')} #{location}:\n\n" <<
    code.with_line_numbers(use_line_numbers?).with_marker(marker).highlighted << "\n"

  _pry_.pager.page out
end

#setupObject



40
41
42
43
44
# File 'lib/pry/commands/whereami.rb', line 40

def setup
  @file = expand_path(target.eval('__FILE__'))
  @line = target.eval('__LINE__')
  @method = Pry::Method.from_binding(target)
end

#small_method?Boolean (private)

Returns:

  • (Boolean)


129
130
131
# File 'lib/pry/commands/whereami.rb', line 129

def small_method?
  @method.source_range.count < self.class.method_size_cutoff
end

#target_classPry::WrappedModule (private)

This either returns the target_self or it returns the class of target_self if target_self is not a class.

Returns:



156
157
158
159
# File 'lib/pry/commands/whereami.rb', line 156

def target_class
  target_self.is_a?(Module) ? Pry::WrappedModule(target_self) :
    Pry::WrappedModule(target_self.class)
end

#top_level?Boolean (private)

Returns:

  • (Boolean)


117
118
119
# File 'lib/pry/commands/whereami.rb', line 117

def top_level?
  target_self == Pry.main
end

#use_line_numbers?Boolean (private)

Returns:

  • (Boolean)


109
110
111
# File 'lib/pry/commands/whereami.rb', line 109

def use_line_numbers?
  !opts.present?(:n)
end

#valid_method?Boolean (private)

Returns:

  • (Boolean)


170
171
172
173
# File 'lib/pry/commands/whereami.rb', line 170

def valid_method?
  @method && @method.source? && expand_path(@method.source_file) == @file &&
    @method.source_range.include?(@line)
end

#window_sizeObject (private)



185
186
187
188
189
190
191
# File 'lib/pry/commands/whereami.rb', line 185

def window_size
  if args.empty?
    _pry_.config.default_window_size
  else
    args.first.to_i
  end
end