super-smart-mod 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/pry-0.16.0/CHANGELOG.md +1211 -0
- data/pry-0.16.0/LICENSE +25 -0
- data/pry-0.16.0/README.md +469 -0
- data/pry-0.16.0/bin/pry +13 -0
- data/pry-0.16.0/lib/pry/basic_object.rb +10 -0
- data/pry-0.16.0/lib/pry/block_command.rb +22 -0
- data/pry-0.16.0/lib/pry/class_command.rb +194 -0
- data/pry-0.16.0/lib/pry/cli.rb +211 -0
- data/pry-0.16.0/lib/pry/code/code_file.rb +114 -0
- data/pry-0.16.0/lib/pry/code/code_range.rb +73 -0
- data/pry-0.16.0/lib/pry/code/loc.rb +105 -0
- data/pry-0.16.0/lib/pry/code.rb +357 -0
- data/pry-0.16.0/lib/pry/code_object.rb +197 -0
- data/pry-0.16.0/lib/pry/color_printer.rb +66 -0
- data/pry-0.16.0/lib/pry/command.rb +520 -0
- data/pry-0.16.0/lib/pry/command_set.rb +418 -0
- data/pry-0.16.0/lib/pry/command_state.rb +36 -0
- data/pry-0.16.0/lib/pry/commands/amend_line.rb +103 -0
- data/pry-0.16.0/lib/pry/commands/bang.rb +24 -0
- data/pry-0.16.0/lib/pry/commands/bang_pry.rb +21 -0
- data/pry-0.16.0/lib/pry/commands/cat/abstract_formatter.rb +32 -0
- data/pry-0.16.0/lib/pry/commands/cat/exception_formatter.rb +90 -0
- data/pry-0.16.0/lib/pry/commands/cat/file_formatter.rb +77 -0
- data/pry-0.16.0/lib/pry/commands/cat/input_expression_formatter.rb +48 -0
- data/pry-0.16.0/lib/pry/commands/cat.rb +70 -0
- data/pry-0.16.0/lib/pry/commands/cd.rb +48 -0
- data/pry-0.16.0/lib/pry/commands/change_inspector.rb +34 -0
- data/pry-0.16.0/lib/pry/commands/change_prompt.rb +51 -0
- data/pry-0.16.0/lib/pry/commands/clear_screen.rb +20 -0
- data/pry-0.16.0/lib/pry/commands/code_collector.rb +182 -0
- data/pry-0.16.0/lib/pry/commands/disable_pry.rb +31 -0
- data/pry-0.16.0/lib/pry/commands/easter_eggs.rb +101 -0
- data/pry-0.16.0/lib/pry/commands/edit/exception_patcher.rb +29 -0
- data/pry-0.16.0/lib/pry/commands/edit/file_and_line_locator.rb +47 -0
- data/pry-0.16.0/lib/pry/commands/edit.rb +225 -0
- data/pry-0.16.0/lib/pry/commands/exit.rb +47 -0
- data/pry-0.16.0/lib/pry/commands/exit_all.rb +33 -0
- data/pry-0.16.0/lib/pry/commands/exit_program.rb +27 -0
- data/pry-0.16.0/lib/pry/commands/find_method.rb +199 -0
- data/pry-0.16.0/lib/pry/commands/fix_indent.rb +23 -0
- data/pry-0.16.0/lib/pry/commands/help.rb +171 -0
- data/pry-0.16.0/lib/pry/commands/hist.rb +182 -0
- data/pry-0.16.0/lib/pry/commands/import_set.rb +27 -0
- data/pry-0.16.0/lib/pry/commands/jump_to.rb +33 -0
- data/pry-0.16.0/lib/pry/commands/list_inspectors.rb +42 -0
- data/pry-0.16.0/lib/pry/commands/ls/config.rb +54 -0
- data/pry-0.16.0/lib/pry/commands/ls/constants.rb +75 -0
- data/pry-0.16.0/lib/pry/commands/ls/formatter.rb +55 -0
- data/pry-0.16.0/lib/pry/commands/ls/globals.rb +50 -0
- data/pry-0.16.0/lib/pry/commands/ls/grep.rb +23 -0
- data/pry-0.16.0/lib/pry/commands/ls/instance_vars.rb +40 -0
- data/pry-0.16.0/lib/pry/commands/ls/interrogatable.rb +24 -0
- data/pry-0.16.0/lib/pry/commands/ls/jruby_hacks.rb +55 -0
- data/pry-0.16.0/lib/pry/commands/ls/local_names.rb +37 -0
- data/pry-0.16.0/lib/pry/commands/ls/local_vars.rb +47 -0
- data/pry-0.16.0/lib/pry/commands/ls/ls_entity.rb +65 -0
- data/pry-0.16.0/lib/pry/commands/ls/methods.rb +55 -0
- data/pry-0.16.0/lib/pry/commands/ls/methods_helper.rb +50 -0
- data/pry-0.16.0/lib/pry/commands/ls/self_methods.rb +34 -0
- data/pry-0.16.0/lib/pry/commands/ls.rb +114 -0
- data/pry-0.16.0/lib/pry/commands/nesting.rb +29 -0
- data/pry-0.16.0/lib/pry/commands/play.rb +113 -0
- data/pry-0.16.0/lib/pry/commands/pry_backtrace.rb +30 -0
- data/pry-0.16.0/lib/pry/commands/pry_version.rb +21 -0
- data/pry-0.16.0/lib/pry/commands/raise_up.rb +38 -0
- data/pry-0.16.0/lib/pry/commands/reload_code.rb +74 -0
- data/pry-0.16.0/lib/pry/commands/reset.rb +22 -0
- data/pry-0.16.0/lib/pry/commands/ri.rb +75 -0
- data/pry-0.16.0/lib/pry/commands/save_file.rb +63 -0
- data/pry-0.16.0/lib/pry/commands/shell_command.rb +75 -0
- data/pry-0.16.0/lib/pry/commands/shell_mode.rb +30 -0
- data/pry-0.16.0/lib/pry/commands/show_doc.rb +84 -0
- data/pry-0.16.0/lib/pry/commands/show_info.rb +234 -0
- data/pry-0.16.0/lib/pry/commands/show_input.rb +22 -0
- data/pry-0.16.0/lib/pry/commands/show_source.rb +57 -0
- data/pry-0.16.0/lib/pry/commands/stat.rb +44 -0
- data/pry-0.16.0/lib/pry/commands/switch_to.rb +29 -0
- data/pry-0.16.0/lib/pry/commands/toggle_color.rb +28 -0
- data/pry-0.16.0/lib/pry/commands/watch_expression/expression.rb +43 -0
- data/pry-0.16.0/lib/pry/commands/watch_expression.rb +108 -0
- data/pry-0.16.0/lib/pry/commands/whereami.rb +205 -0
- data/pry-0.16.0/lib/pry/commands/wtf.rb +95 -0
- data/pry-0.16.0/lib/pry/config/attributable.rb +22 -0
- data/pry-0.16.0/lib/pry/config/lazy_value.rb +29 -0
- data/pry-0.16.0/lib/pry/config/memoized_value.rb +34 -0
- data/pry-0.16.0/lib/pry/config/value.rb +24 -0
- data/pry-0.16.0/lib/pry/config.rb +321 -0
- data/pry-0.16.0/lib/pry/control_d_handler.rb +28 -0
- data/pry-0.16.0/lib/pry/core_extensions.rb +144 -0
- data/pry-0.16.0/lib/pry/editor.rb +157 -0
- data/pry-0.16.0/lib/pry/env.rb +18 -0
- data/pry-0.16.0/lib/pry/exception_handler.rb +48 -0
- data/pry-0.16.0/lib/pry/exceptions.rb +73 -0
- data/pry-0.16.0/lib/pry/forwardable.rb +27 -0
- data/pry-0.16.0/lib/pry/helpers/base_helpers.rb +71 -0
- data/pry-0.16.0/lib/pry/helpers/command_helpers.rb +146 -0
- data/pry-0.16.0/lib/pry/helpers/documentation_helpers.rb +84 -0
- data/pry-0.16.0/lib/pry/helpers/options_helpers.rb +34 -0
- data/pry-0.16.0/lib/pry/helpers/platform.rb +55 -0
- data/pry-0.16.0/lib/pry/helpers/table.rb +121 -0
- data/pry-0.16.0/lib/pry/helpers/text.rb +118 -0
- data/pry-0.16.0/lib/pry/helpers.rb +8 -0
- data/pry-0.16.0/lib/pry/history.rb +153 -0
- data/pry-0.16.0/lib/pry/hooks.rb +180 -0
- data/pry-0.16.0/lib/pry/indent.rb +414 -0
- data/pry-0.16.0/lib/pry/input/simple_stdio.rb +13 -0
- data/pry-0.16.0/lib/pry/input_completer.rb +283 -0
- data/pry-0.16.0/lib/pry/input_lock.rb +129 -0
- data/pry-0.16.0/lib/pry/inspector.rb +39 -0
- data/pry-0.16.0/lib/pry/last_exception.rb +61 -0
- data/pry-0.16.0/lib/pry/method/disowned.rb +67 -0
- data/pry-0.16.0/lib/pry/method/patcher.rb +131 -0
- data/pry-0.16.0/lib/pry/method/weird_method_locator.rb +222 -0
- data/pry-0.16.0/lib/pry/method.rb +599 -0
- data/pry-0.16.0/lib/pry/object_path.rb +91 -0
- data/pry-0.16.0/lib/pry/output.rb +136 -0
- data/pry-0.16.0/lib/pry/pager.rb +249 -0
- data/pry-0.16.0/lib/pry/prompt.rb +214 -0
- data/pry-0.16.0/lib/pry/pry_class.rb +371 -0
- data/pry-0.16.0/lib/pry/pry_instance.rb +663 -0
- data/pry-0.16.0/lib/pry/repl.rb +326 -0
- data/pry-0.16.0/lib/pry/repl_file_loader.rb +79 -0
- data/pry-0.16.0/lib/pry/ring.rb +89 -0
- data/pry-0.16.0/lib/pry/slop/LICENSE +20 -0
- data/pry-0.16.0/lib/pry/slop/commands.rb +190 -0
- data/pry-0.16.0/lib/pry/slop/option.rb +210 -0
- data/pry-0.16.0/lib/pry/slop.rb +672 -0
- data/pry-0.16.0/lib/pry/syntax_highlighter.rb +26 -0
- data/pry-0.16.0/lib/pry/system_command_handler.rb +17 -0
- data/pry-0.16.0/lib/pry/testable/evalable.rb +24 -0
- data/pry-0.16.0/lib/pry/testable/mockable.rb +22 -0
- data/pry-0.16.0/lib/pry/testable/pry_tester.rb +88 -0
- data/pry-0.16.0/lib/pry/testable/utility.rb +34 -0
- data/pry-0.16.0/lib/pry/testable/variables.rb +52 -0
- data/pry-0.16.0/lib/pry/testable.rb +68 -0
- data/pry-0.16.0/lib/pry/version.rb +5 -0
- data/pry-0.16.0/lib/pry/warning.rb +20 -0
- data/pry-0.16.0/lib/pry/wrapped_module/candidate.rb +145 -0
- data/pry-0.16.0/lib/pry/wrapped_module.rb +382 -0
- data/pry-0.16.0/lib/pry.rb +148 -0
- data/super-smart-mod.gemspec +12 -0
- metadata +182 -0
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Pry
|
|
4
|
+
##
|
|
5
|
+
# Pry::Indent is a class that can be used to indent a number of lines
|
|
6
|
+
# containing Ruby code similar as to how IRB does it (but better). The class
|
|
7
|
+
# works by tokenizing a string using CodeRay and then looping over those
|
|
8
|
+
# tokens. Based on the tokens in a line of code that line (or the next one)
|
|
9
|
+
# will be indented or un-indented by correctly.
|
|
10
|
+
#
|
|
11
|
+
class Indent
|
|
12
|
+
include Helpers::BaseHelpers
|
|
13
|
+
|
|
14
|
+
# Raised if {#module_nesting} would not work.
|
|
15
|
+
class UnparseableNestingError < StandardError; end
|
|
16
|
+
|
|
17
|
+
# @return [String] String containing the spaces to be inserted before the next line.
|
|
18
|
+
attr_reader :indent_level
|
|
19
|
+
|
|
20
|
+
# @return [String] String containing the spaces for the current line.
|
|
21
|
+
attr_reader :last_indent_level
|
|
22
|
+
|
|
23
|
+
# @return [Array<String>] The stack of open tokens.
|
|
24
|
+
attr_reader :stack
|
|
25
|
+
|
|
26
|
+
# The amount of spaces to insert for each indent level.
|
|
27
|
+
SPACES = ' '.freeze
|
|
28
|
+
|
|
29
|
+
# Hash containing all the tokens that should increase the indentation
|
|
30
|
+
# level. The keys of this hash are open tokens, the values the matching
|
|
31
|
+
# tokens that should prevent a line from being indented if they appear on
|
|
32
|
+
# the same line.
|
|
33
|
+
OPEN_TOKENS = {
|
|
34
|
+
'def' => 'end',
|
|
35
|
+
'class' => 'end',
|
|
36
|
+
'module' => 'end',
|
|
37
|
+
'do' => 'end',
|
|
38
|
+
'if' => 'end',
|
|
39
|
+
'unless' => 'end',
|
|
40
|
+
'while' => 'end',
|
|
41
|
+
'until' => 'end',
|
|
42
|
+
'for' => 'end',
|
|
43
|
+
'case' => 'end',
|
|
44
|
+
'begin' => 'end',
|
|
45
|
+
'[' => ']',
|
|
46
|
+
'{' => '}',
|
|
47
|
+
'(' => ')'
|
|
48
|
+
}.freeze
|
|
49
|
+
|
|
50
|
+
# Which tokens can either be open tokens, or appear as modifiers on
|
|
51
|
+
# a single-line.
|
|
52
|
+
SINGLELINE_TOKENS = %w[if while until unless rescue].freeze
|
|
53
|
+
|
|
54
|
+
# Which tokens can be followed by an optional "do" keyword.
|
|
55
|
+
OPTIONAL_DO_TOKENS = %w[for while until].freeze
|
|
56
|
+
|
|
57
|
+
# Collection of token types that should be ignored. Without this list
|
|
58
|
+
# keywords such as "class" inside strings would cause the code to be
|
|
59
|
+
# indented incorrectly.
|
|
60
|
+
#
|
|
61
|
+
# :pre_constant and :preserved_constant are the CodeRay 0.9.8 and 1.0.0
|
|
62
|
+
# classifications of "true", "false", and "nil".
|
|
63
|
+
IGNORE_TOKENS = %i[space content string method ident
|
|
64
|
+
constant pre_constant predefined_constant].freeze
|
|
65
|
+
|
|
66
|
+
# Tokens that indicate the end of a statement (i.e. that, if they appear
|
|
67
|
+
# directly before an "if" indicates that that if applies to the same line,
|
|
68
|
+
# not the next line)
|
|
69
|
+
#
|
|
70
|
+
# :reserved and :keywords are the CodeRay 0.9.8 and 1.0.0 respectively
|
|
71
|
+
# classifications of "super", "next", "return", etc.
|
|
72
|
+
STATEMENT_END_TOKENS = IGNORE_TOKENS + %i[regexp integer float
|
|
73
|
+
keyword delimiter reserved
|
|
74
|
+
instance_variable
|
|
75
|
+
class_variable global_variable]
|
|
76
|
+
|
|
77
|
+
# Collection of tokens that should appear dedented even though they
|
|
78
|
+
# don't affect the surrounding code.
|
|
79
|
+
MIDWAY_TOKENS = %w[when else elsif ensure rescue].freeze
|
|
80
|
+
|
|
81
|
+
# Clean the indentation of a fragment of ruby.
|
|
82
|
+
#
|
|
83
|
+
# @param [String] str
|
|
84
|
+
# @return [String]
|
|
85
|
+
def self.indent(str)
|
|
86
|
+
new.indent(str)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Get the module nesting at the given point in the given string.
|
|
90
|
+
#
|
|
91
|
+
# NOTE If the line specified contains a method definition, then the nesting
|
|
92
|
+
# at the start of the method definition is used. Otherwise the nesting from
|
|
93
|
+
# the end of the line is used.
|
|
94
|
+
#
|
|
95
|
+
# @param [String] str The ruby code to analyze
|
|
96
|
+
# @param [Fixnum] line_number The line number (starting from 1)
|
|
97
|
+
# @return [Array<String>]
|
|
98
|
+
def self.nesting_at(str, line_number)
|
|
99
|
+
indent = new
|
|
100
|
+
lines = str.split("\n")
|
|
101
|
+
n = line_number - 1
|
|
102
|
+
to_indent = lines[0...n] << (lines[n] || "").split("def").first(1)
|
|
103
|
+
indent.indent(to_indent.join("\n") << "\n")
|
|
104
|
+
indent.module_nesting
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def initialize(pry_instance = Pry.new)
|
|
108
|
+
@pry_instance = pry_instance
|
|
109
|
+
reset
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# reset internal state
|
|
113
|
+
def reset
|
|
114
|
+
@stack = []
|
|
115
|
+
@indent_level = String.new # rubocop:disable Style/EmptyLiteral
|
|
116
|
+
@last_indent_level = @indent_level
|
|
117
|
+
@heredoc_queue = []
|
|
118
|
+
@close_heredocs = {}
|
|
119
|
+
@string_start = nil
|
|
120
|
+
@awaiting_class = false
|
|
121
|
+
@module_nesting = []
|
|
122
|
+
self
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Indents a string and returns it. This string can either be a single line
|
|
126
|
+
# or multiple ones.
|
|
127
|
+
#
|
|
128
|
+
# @example
|
|
129
|
+
# str = <<TXT
|
|
130
|
+
# class User
|
|
131
|
+
# attr_accessor :name
|
|
132
|
+
# end
|
|
133
|
+
# TXT
|
|
134
|
+
#
|
|
135
|
+
# # This would result in the following being displayed:
|
|
136
|
+
# #
|
|
137
|
+
# # class User
|
|
138
|
+
# # attr_accessor :name
|
|
139
|
+
# # end
|
|
140
|
+
# #
|
|
141
|
+
# puts Pry::Indent.new.indent(str)
|
|
142
|
+
#
|
|
143
|
+
# @param [String] input The input string to indent.
|
|
144
|
+
# @return [String] The indented version of +input+.
|
|
145
|
+
#
|
|
146
|
+
def indent(input)
|
|
147
|
+
output = ''
|
|
148
|
+
prefix = indent_level
|
|
149
|
+
|
|
150
|
+
input.lines.each do |line|
|
|
151
|
+
if in_string?
|
|
152
|
+
tokens = tokenize("#{open_delimiters_line}\n#{line}")
|
|
153
|
+
tokens = tokens.drop_while do |token, _type|
|
|
154
|
+
!(token.is_a?(String) && token.include?("\n"))
|
|
155
|
+
end
|
|
156
|
+
previously_in_string = true
|
|
157
|
+
else
|
|
158
|
+
tokens = tokenize(line)
|
|
159
|
+
previously_in_string = false
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
before, after = indentation_delta(tokens)
|
|
163
|
+
|
|
164
|
+
before.times { prefix.sub! SPACES, '' }
|
|
165
|
+
new_prefix = prefix + SPACES * after
|
|
166
|
+
|
|
167
|
+
line = prefix + line.lstrip unless previously_in_string
|
|
168
|
+
|
|
169
|
+
output += line
|
|
170
|
+
|
|
171
|
+
@last_indent_level = prefix
|
|
172
|
+
prefix = new_prefix
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
@indent_level = prefix
|
|
176
|
+
output
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# Get the indentation for the start of the next line.
|
|
180
|
+
#
|
|
181
|
+
# This is what's used between the prompt and the cursor in pry.
|
|
182
|
+
#
|
|
183
|
+
# @return String The correct number of spaces
|
|
184
|
+
#
|
|
185
|
+
def current_prefix
|
|
186
|
+
in_string? ? '' : indent_level
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Get the change in indentation indicated by the line.
|
|
190
|
+
#
|
|
191
|
+
# By convention, you remove indent from the line containing end tokens,
|
|
192
|
+
# but add indent to the line *after* that which contains the start tokens.
|
|
193
|
+
#
|
|
194
|
+
# This method returns a pair, where the first number is the number of closings
|
|
195
|
+
# on this line (i.e. the number of indents to remove before the line) and the
|
|
196
|
+
# second is the number of openings (i.e. the number of indents to add after
|
|
197
|
+
# this line)
|
|
198
|
+
#
|
|
199
|
+
# @param [Array] tokens A list of tokens to scan.
|
|
200
|
+
# @return [Array[Integer]]
|
|
201
|
+
#
|
|
202
|
+
def indentation_delta(tokens)
|
|
203
|
+
# We need to keep track of whether we've seen a "for" on this line because
|
|
204
|
+
# if the line ends with "do" then that "do" should be discounted (i.e. we're
|
|
205
|
+
# only opening one level not two) To do this robustly we want to keep track
|
|
206
|
+
# of the indent level at which we saw the for, so we can differentiate
|
|
207
|
+
# between "for x in [1,2,3] do" and "for x in ([1,2,3].map do" properly
|
|
208
|
+
seen_for_at = []
|
|
209
|
+
|
|
210
|
+
# When deciding whether an "if" token is the start of a multiline statement,
|
|
211
|
+
# or just the middle of a single-line if statement, we just look at the
|
|
212
|
+
# preceding token, which is tracked here.
|
|
213
|
+
last_token = nil
|
|
214
|
+
last_kind = nil
|
|
215
|
+
|
|
216
|
+
# delta keeps track of the total difference from the start of each line after
|
|
217
|
+
# the given token, 0 is just the level at which the current line started for
|
|
218
|
+
# reference.
|
|
219
|
+
remove_before = 0
|
|
220
|
+
add_after = 0
|
|
221
|
+
|
|
222
|
+
# If the list of tokens contains a matching closing token the line should
|
|
223
|
+
# not be indented (and thus we should return true).
|
|
224
|
+
tokens.each do |token, kind|
|
|
225
|
+
is_singleline_if =
|
|
226
|
+
SINGLELINE_TOKENS.include?(token) && end_of_statement?(last_token, last_kind)
|
|
227
|
+
is_optional_do = (token == "do" && seen_for_at.include?(add_after - 1))
|
|
228
|
+
|
|
229
|
+
unless kind == :space
|
|
230
|
+
last_token = token
|
|
231
|
+
last_kind = kind
|
|
232
|
+
end
|
|
233
|
+
next if IGNORE_TOKENS.include?(kind)
|
|
234
|
+
|
|
235
|
+
track_module_nesting(token, kind)
|
|
236
|
+
|
|
237
|
+
seen_for_at << add_after if OPTIONAL_DO_TOKENS.include?(token)
|
|
238
|
+
|
|
239
|
+
next if is_singleline_if
|
|
240
|
+
|
|
241
|
+
if kind == :delimiter
|
|
242
|
+
track_delimiter(token)
|
|
243
|
+
elsif OPEN_TOKENS.key?(token) && !is_optional_do && !is_singleline_if
|
|
244
|
+
@stack << token
|
|
245
|
+
add_after += 1
|
|
246
|
+
elsif token == OPEN_TOKENS[@stack.last]
|
|
247
|
+
popped = @stack.pop
|
|
248
|
+
track_module_nesting_end(popped)
|
|
249
|
+
if add_after == 0
|
|
250
|
+
remove_before += 1
|
|
251
|
+
else
|
|
252
|
+
add_after -= 1
|
|
253
|
+
end
|
|
254
|
+
elsif MIDWAY_TOKENS.include?(token)
|
|
255
|
+
if add_after == 0
|
|
256
|
+
remove_before += 1
|
|
257
|
+
add_after += 1
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
[remove_before, add_after]
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# If the code just before an "if" or "while" token on a line looks like the
|
|
266
|
+
# end of a statement, then we want to treat that "if" as a singleline, not
|
|
267
|
+
# multiline statement.
|
|
268
|
+
def end_of_statement?(last_token, last_kind)
|
|
269
|
+
(last_token =~ %r{^[)\]\}/]$} || STATEMENT_END_TOKENS.include?(last_kind))
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# Are we currently in the middle of a string literal.
|
|
273
|
+
#
|
|
274
|
+
# This is used to determine whether to re-indent a given line, we mustn't
|
|
275
|
+
# re-indent within string literals because to do so would actually change
|
|
276
|
+
# the value of the String!
|
|
277
|
+
#
|
|
278
|
+
# @return Boolean
|
|
279
|
+
def in_string?
|
|
280
|
+
!open_delimiters.empty?
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# Given a string of Ruby code, use CodeRay to export the tokens.
|
|
284
|
+
#
|
|
285
|
+
# @param [String] string The Ruby to lex
|
|
286
|
+
# @return [Array] An Array of pairs of [token_value, token_type]
|
|
287
|
+
def tokenize(string)
|
|
288
|
+
SyntaxHighlighter.tokenize(string).each_slice(2).to_a
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# Update the internal state about what kind of strings are open.
|
|
292
|
+
#
|
|
293
|
+
# Most of the complication here comes from the fact that HEREDOCs can be
|
|
294
|
+
# nested. For normal strings (which can't be nested) we assume that CodeRay
|
|
295
|
+
# correctly pairs open-and-close delimiters so we don't bother checking what
|
|
296
|
+
# they are.
|
|
297
|
+
#
|
|
298
|
+
# @param [String] token The token (of type :delimiter)
|
|
299
|
+
def track_delimiter(token)
|
|
300
|
+
case token
|
|
301
|
+
when /^<<-(["'`]?)(.*)\\1/
|
|
302
|
+
@heredoc_queue << token
|
|
303
|
+
@close_heredocs[token] = /^\s*$2/
|
|
304
|
+
when @close_heredocs[@heredoc_queue.first]
|
|
305
|
+
@heredoc_queue.shift
|
|
306
|
+
else
|
|
307
|
+
@string_start = @string_start ? nil : token
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# All the open delimiters, in the order that they first appeared.
|
|
312
|
+
#
|
|
313
|
+
# @return [String]
|
|
314
|
+
def open_delimiters
|
|
315
|
+
@heredoc_queue + [@string_start].compact
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# Return a string which restores the CodeRay string status to the correct value by
|
|
319
|
+
# opening HEREDOCs and strings.
|
|
320
|
+
#
|
|
321
|
+
# @return String
|
|
322
|
+
def open_delimiters_line
|
|
323
|
+
"puts #{open_delimiters.join(', ')}"
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
# Update the internal state relating to module nesting.
|
|
327
|
+
#
|
|
328
|
+
# It's responsible for adding to the @module_nesting array, which looks
|
|
329
|
+
# something like:
|
|
330
|
+
#
|
|
331
|
+
# [ ["class", "Foo"], ["module", "Bar::Baz"], ["class <<", "self"] ]
|
|
332
|
+
#
|
|
333
|
+
# A nil value in the @module_nesting array happens in two places: either
|
|
334
|
+
# when @awaiting_class is true and we're still waiting for the string to
|
|
335
|
+
# fill that space, or when a parse was rejected.
|
|
336
|
+
#
|
|
337
|
+
# At the moment this function is quite restricted about what formats it will
|
|
338
|
+
# parse, for example we disallow expressions after the class keyword. This
|
|
339
|
+
# could maybe be improved in the future.
|
|
340
|
+
#
|
|
341
|
+
# @param [String] token a token from Coderay
|
|
342
|
+
# @param [Symbol] kind the kind of that token
|
|
343
|
+
def track_module_nesting(token, kind)
|
|
344
|
+
if kind == :keyword && %w[class module].include?(token)
|
|
345
|
+
@module_nesting << [token, nil]
|
|
346
|
+
@awaiting_class = true
|
|
347
|
+
elsif @awaiting_class
|
|
348
|
+
if kind == :operator && token == "<<" && @module_nesting.last[0] == "class"
|
|
349
|
+
@module_nesting.last[0] = "class <<"
|
|
350
|
+
@awaiting_class = true
|
|
351
|
+
elsif kind == :class && token =~ /\A(self|[A-Z:][A-Za-z0-9_:]*)\z/
|
|
352
|
+
@module_nesting.last[1] = token if kind == :class
|
|
353
|
+
@awaiting_class = false
|
|
354
|
+
else
|
|
355
|
+
# leave @module_nesting[-1]
|
|
356
|
+
@awaiting_class = false
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
# Update the internal state relating to module nesting on 'end'.
|
|
362
|
+
#
|
|
363
|
+
# If the current 'end' pairs up with a class or a module then we should
|
|
364
|
+
# pop an array off of @module_nesting
|
|
365
|
+
#
|
|
366
|
+
# @param [String] token a token from Coderay
|
|
367
|
+
# @param [Symbol] kind the kind of that token
|
|
368
|
+
def track_module_nesting_end(token, kind = :keyword)
|
|
369
|
+
@module_nesting.pop if kind == :keyword && %w[class module].include?(token)
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
# Return a list of strings which can be used to re-construct the Module.nesting at
|
|
373
|
+
# the current point in the file.
|
|
374
|
+
#
|
|
375
|
+
# Returns nil if the syntax of the file was not recognizable.
|
|
376
|
+
#
|
|
377
|
+
# @return [Array<String>]
|
|
378
|
+
def module_nesting
|
|
379
|
+
@module_nesting.map do |(kind, token)|
|
|
380
|
+
raise UnparseableNestingError, @module_nesting.inspect if token.nil?
|
|
381
|
+
|
|
382
|
+
"#{kind} #{token}"
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
# Return a string which, when printed, will rewrite the previous line with
|
|
387
|
+
# the correct indentation. Mostly useful for fixing 'end'.
|
|
388
|
+
#
|
|
389
|
+
# @param [String] prompt The user's prompt
|
|
390
|
+
# @param [String] code The code the user just typed in
|
|
391
|
+
# @param [Integer] overhang The number of characters to erase afterwards (the
|
|
392
|
+
# the difference in length between the old line and the new one)
|
|
393
|
+
#
|
|
394
|
+
# @return [String] correctly indented line
|
|
395
|
+
def correct_indentation(prompt, code, overhang = 0)
|
|
396
|
+
prompt = prompt.delete("\001\002")
|
|
397
|
+
line_to_measure = Pry::Helpers::Text.strip_color(prompt) << code
|
|
398
|
+
whitespace = ' ' * overhang
|
|
399
|
+
|
|
400
|
+
cols = @pry_instance.output.width
|
|
401
|
+
lines = cols == 0 ? 1 : (line_to_measure.length / cols + 1).to_i
|
|
402
|
+
|
|
403
|
+
if Helpers::Platform.windows_ansi?
|
|
404
|
+
move_up = "\e[#{lines}F"
|
|
405
|
+
move_down = "\e[#{lines}E"
|
|
406
|
+
else
|
|
407
|
+
move_up = "\e[#{lines}A\e[0G"
|
|
408
|
+
move_down = "\e[#{lines}B\e[0G"
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
"#{move_up}#{prompt}#{colorize_code(code)}#{whitespace}#{move_down}"
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
end
|