Represents a single line of code of a given source file

This object contains metadata about the line such as amount of indentation, if it is empty or not, and lexical data, such as if it has an ‘end` or a keyword in it.

Visibility of lines can be toggled off. Marking a line as invisible indicates that it should not be used for syntax checks. It’s functionally the same as commenting it out.

Example:

line = CodeLine.from_source("def foo\n").first
line.number => 1
line.empty? # => false
line.visible? # => true
line.mark_invisible
line.visible? # => false
Constants
No documentation available
Attributes
Read
No documentation available
Read
No documentation available

lex

Read
No documentation available
No documentation available
Read
No documentation available
Read
No documentation available
Read

When the code line is marked invisible we retain the original value of it’s line this is useful for debugging and for showing extra context

DisplayCodeWithLineNumbers will render all lines given to it, not just visible lines, it uses the original method to obtain them.

Class Methods

Returns an array of CodeLine objects from the source string

No documentation available
Instance Methods

Comparison operator, needed for equality and sorting

An ‘empty?` line is one that was originally left empty in the source code, while a “hidden” line is one that we’ve since marked as “invisible”

Opposite or ‘visible?` (note: different than `empty?`)

Not stable API

Lines that have a ‘on_ignored_nl` type token and NOT a `BEG` type seem to be a good proxy for the ability to join multiple lines into one.

This predicate method is used to determine when those two criteria have been met.

The one known case this doesn’t handle is:

Ripper.lex <<~EOM
  a &&
   b ||
   c
EOM

For some reason this introduces ‘on_ignore_newline` but with BEG type

Used for stable sort via indentation level

Ruby’s sort is not “stable” meaning that when multiple elements have the same value, they are not guaranteed to return in the same order they were put in.

So when multiple code lines have the same indentation level, they’re sorted by their index value which is unique and consistent.

This is mostly needed for consistency of the test suite

Returns true if the code line is determined to contain an ‘end` keyword

Returns true if the code line is determined to contain a keyword that matches with an ‘end`

For example: ‘def`, `do`, `begin`, `ensure`, etc.

Used to hide lines

The search alorithm will group lines into blocks then if those blocks are determined to represent valid code they will be hidden

Opposite of ‘empty?` (note: different than `visible?`)

Endless method detection

From github.com/ruby/irb/commit/826ae909c9c93a2ddca6f9cfcd9c94dbf53d44ab Detecting a “oneliner” seems to need a state machine. This can be done by looking mostly at the “state” (last value):

ENDFN -> BEG (token = '=' ) -> END

Renders the given line

Also allows us to represent source code as an array of code lines.

When we have an array of code line elements calling ‘join` on the array will call `to_s` on each element, which essentially converts it back into it’s original source string.

Determines if the given line has a trailing slash

lines = CodeLine.from_source(<<~EOM)
  it "foo" \
EOM
expect(lines.first.trailing_slash?).to eq(true)

Means the line was marked as “invisible” Confusingly, “empty” lines are visible…they just don’t contain any source code other than a newline (“n”).