Module: Pry::Helpers::CommandHelpers

Extended by:
CommandHelpers
Includes:
OptionsHelpers
Included in:
CodeObject, Command, Command::Cat::AbstractFormatter, Command::CodeCollector, Editor, CommandHelpers
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb

Instance Method Summary collapse

Methods included from OptionsHelpers

method_object, method_options

Instance Method Details

#absolute_index_number(line_number, array_length) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 115

def absolute_index_number(line_number, array_length)
  if line_number >= 0
    line_number
  else
    [array_length + line_number, 0].max
  end
end

#absolute_index_range(range_or_number, array_length) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 123

def absolute_index_range(range_or_number, array_length)
  case range_or_number
  when Range
    a = absolute_index_number(range_or_number.begin, array_length)
    b = absolute_index_number(range_or_number.end, array_length)
  else
    a = b = absolute_index_number(range_or_number, array_length)
  end

  Range.new(a, b)
end

#get_method_or_raise(method_name, context, opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 31

def get_method_or_raise(method_name, context, opts = {})
  method = Pry::Method.from_str(method_name, context, opts)
  if !method && method_name
    raise Pry::MethodNotFound, "method '#{method_name}' could not be found."
  end

  (opts[:super] || 0).times do
    if method.super
      method = method.super
    else
      raise Pry::MethodNotFound,
            "'#{method.name_with_owner}' has no super method"
    end
  end

  if !method || (!method_name && internal_binding?(context))
    raise Pry::MethodNotFound,
          'no method name given, and context is not a method'
  end

  set_file_and_dir_locals(method.source_file)
  method
end

#internal_binding?(context) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 21

def internal_binding?(context)
  method_name = context.eval("::Kernel.__method__").to_s
  # class_eval is here because of http://jira.codehaus.org/browse/JRUBY-6753
  %w[__binding__ __pry__ class_eval].include?(method_name)
  # TODO: codehaus is dead, there was no test for this and the
  # description for the commit doesn't exist. Probably a candidate for
  # removal so we have a chance to introduce a regression and document it
  # properly.
end

#one_index_number(line_number) ⇒ Object



97
98
99
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 97

def one_index_number(line_number)
  line_number > 0 ? line_number - 1 : line_number
end

#one_index_range(range) ⇒ Object

convert a 1-index range to a 0-indexed one



102
103
104
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 102

def one_index_range(range)
  Range.new(one_index_number(range.begin), one_index_number(range.end))
end

#one_index_range_or_number(range_or_number) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 106

def one_index_range_or_number(range_or_number)
  case range_or_number
  when Range
    one_index_range(range_or_number)
  else
    one_index_number(range_or_number)
  end
end

#restrict_to_lines(content, lines) ⇒ String

Restrict a string to the given range of lines (1-indexed)

Parameters:

  • content (String)

    The string.

  • lines (Range, Integer)

    The line(s) to restrict it to.

Returns:

  • (String)

    The resulting string.



92
93
94
95
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 92

def restrict_to_lines(content, lines)
  line_range = one_index_range_or_number(lines)
  Array(content.lines.to_a[line_range]).join
end

#set_file_and_dir_locals(file_name, pry = pry_instance, ctx = target) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 135

def set_file_and_dir_locals(file_name, pry = pry_instance, ctx = target)
  return if !ctx || !file_name

  pry.last_file = File.expand_path(file_name)
  pry.inject_local("_file_", pry.last_file, ctx)

  pry.last_dir = File.dirname(pry.last_file)
  pry.inject_local("_dir_", pry.last_dir, ctx)
end

#temp_file(ext = '.rb') ⇒ String

Open a temp file and yield it to the block, closing it after

Returns:

  • (String)

    The path of the temp file



14
15
16
17
18
19
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 14

def temp_file(ext = '.rb')
  file = Tempfile.new(['pry', ext])
  yield(file)
ensure
  file.close(true)
end

#unindent(dirty_text, left_padding = 0) ⇒ String

Remove any common leading whitespace from every line in ‘text`. This can be used to make a HEREDOC line up with the left margin, without sacrificing the indentation level of the source code.

Examples:

opt.banner(unindent(<<-USAGE))
  Lorem ipsum dolor sit amet, consectetur adipisicing elit,
  sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    "Ut enim ad minim veniam."
USAGE

Parameters:

  • dirty_text (String)

    The text from which to remove indentation

Returns:

  • (String)

    the text with indentation stripped



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/helpers/command_helpers.rb', line 68

def unindent(dirty_text, left_padding = 0)
  text = dirty_text.sub(/\A[ \t]+\z/, '') # Empty blank lines.

  # Find the longest common whitespace to all indented lines. Ignore lines
  # containing just -- or ++ as these seem to be used by comment authors
  # as delimeters.
  scanned_text = text.scan(/^[ \t]*(?!--\n|\+\+\n)(?=[^ \t\n])/)
  margin = scanned_text.inject do |current_margin, next_indent|
    if next_indent.start_with?(current_margin)
      current_margin
    elsif current_margin.start_with?(next_indent)
      next_indent
    else
      ''
    end
  end

  text.gsub(/^#{margin}/, ' ' * left_padding)
end