| Class | Pry::Indent |
| In: |
lib/pry/indent.rb
|
| Parent: | Object |
Pry::Indent is a class that can be used to indent a number of lines containing Ruby code similar as to how IRB does it (but better). The class works by tokenizing a string using CodeRay and then looping over those tokens. Based on the tokens in a line of code that line (or the next one) will be indented or un-indented by correctly.
| SPACES | = | ' ' | The amount of spaces to insert for each indent level. | |
| OPEN_TOKENS | = | { 'def' => 'end', 'class' => 'end', 'module' => 'end', 'do' => 'end', 'if' => 'end', 'unless' => 'end', 'while' => 'end', 'until' => 'end', 'for' => 'end', 'case' => 'end', 'begin' => 'end', '[' => ']', '{' => '}', '(' => ')' | Hash containing all the tokens that should increase the indentation level. The keys of this hash are open tokens, the values the matching tokens that should prevent a line from being indented if they appear on the same line. | |
| SINGLELINE_TOKENS | = | %w(if while until unless rescue) | Which tokens can either be open tokens, or appear as modifiers on a single-line. | |
| OPTIONAL_DO_TOKENS | = | %w(for while until) | Which tokens can be followed by an optional "do" keyword. | |
| IGNORE_TOKENS | = | [:space, :content, :string, :method, :ident, :constant, :pre_constant, :predefined_constant] |
Collection of token types that should be ignored. Without this list
keywords such as "class" inside strings would cause the code to
be indented incorrectly.
:pre_constant and :preserved_constant are the CodeRay 0.9.8 and 1.0.0 classifications of "true", "false", and "nil". |
|
| STATEMENT_END_TOKENS | = | IGNORE_TOKENS + [:regexp, :integer, :float, :keyword, :delimiter, :reserved] |
Tokens that indicate the end of a statement (i.e. that, if they appear
directly before an "if" indicates that that if applies to the
same line, not the next line)
:reserved and :keywords are the CodeRay 0.9.8 and 1.0.0 respectively classifications of "super", "next", "return", etc. |
|
| MIDWAY_TOKENS | = | %w(when else elsif ensure rescue) | Collection of tokens that should appear dedented even though they don‘t affect the surrounding code. |
| indent_level | [R] | @return [String] String containing the spaces to be inserted before the next line. |
| stack | [R] | @return [Array<String>] The stack of open tokens. |
Get the module nesting at the given point in the given string.
NOTE If the line specified contains a method definition, then the nesting at the start of the method definition is used. Otherwise the nesting from the end of the line is used.
@param [String] str The ruby code to analyze @param [Fixnum] line_number The line number (starting from 1) @return [Array<String>]
Return a string which, when printed, will rewrite the previous line with the correct indentation. Mostly useful for fixing ‘end’.
@param [String] prompt The user‘s prompt @param [String] code The code the user just typed in. @param [Fixnum] overhang (0) The number of chars to erase afterwards (i.e.,
the difference in length between the old line and the new one).
@return [String]
Get the indentation for the start of the next line.
This is what‘s used between the prompt and the cursor in pry.
@return String The correct number of spaces
If the code just before an "if" or "while" token on a line looks like the end of a statement, then we want to treat that "if" as a singleline, not multiline statement.
Indents a string and returns it. This string can either be a single line or multiple ones.
@example
str = <<TXT class User attr_accessor :name end TXT # This would result in the following being displayed: # # class User # attr_accessor :name # end # puts Pry::Indent.new.indent(str)
@param [String] input The input string to indent. @return [String] The indented version of input.
Get the change in indentation indicated by the line.
By convention, you remove indent from the line containing end tokens, but add indent to the line after that which contains the start tokens.
This method returns a pair, where the first number is the number of closings on this line (i.e. the number of indents to remove before the line) and the second is the number of openings (i.e. the number of indents to add after this line)
@param [Array] tokens A list of tokens to scan. @return [Array[Integer]]
Return a list of strings which can be used to re-construct the Module.nesting at the current point in the file.
Returns nil if the syntax of the file was not recognizable.
@return [Array<String>]
Return a string which restores the CodeRay string status to the correct value by opening HEREDOCs and strings.
@return String
Given a string of Ruby code, use CodeRay to export the tokens.
@param [String] string The Ruby to lex @return [Array] An Array of pairs of [token_value, token_type]
Update the internal state about what kind of strings are open.
Most of the complication here comes from the fact that HEREDOCs can be nested. For normal strings (which can‘t be nested) we assume that CodeRay correctly pairs open-and-close delimiters so we don‘t bother checking what they are.
@param [String] token The token (of type :delimiter)
Update the internal state relating to module nesting.
It‘s responsible for adding to the @module_nesting array, which looks something like:
A nil value in the @module_nesting array happens in two places: either when @awaiting_class is true and we‘re still waiting for the string to fill that space, or when a parse was rejected.
At the moment this function is quite restricted about what formats it will parse, for example we disallow expressions after the class keyword. This could maybe be improved in the future.
@param [String] token a token from Coderay @param [Symbol] kind the kind of that token
Update the internal state relating to module nesting on ‘end’.
If the current ‘end’ pairs up with a class or a module then we should pop an array off of @module_nesting
@param [String] token a token from Coderay @param [Symbol] kind the kind of that token