class DeadEnd::CodeLine
Represents a single line of code of a given source file
This object contains metadata about the line such as amount of indentation. An if it is empty or not.
While a given search for syntax errors is being performed state about the search can be stored in individual lines such as :valid or :invalid.
Visibility of lines can be toggled on and off.
Example:
line = CodeLine.new(line: "def foo\n", index: 0) line.line_number => 1 line.empty? # => false line.visible? # => true line.mark_invisible line.visible? # => false
A CodeBlock
is made of multiple CodeLines
Marking a line as invisible indicates that it should not be used for syntax checks. It's essentially the same as commenting it out
Marking a line as invisible also lets the overall program know that it should not check that area for syntax errors.
Constants
- TRAILING_SLASH
Attributes
indent[R]
index[R]
line[R]
original[R]
original_line[R]
Public Class Methods
new(line: , index:)
click to toggle source
# File lib/dead_end/code_line.rb, line 42 def initialize(line: , index:) @original_line = line.freeze @line = @original_line if line.strip.empty? @empty = true @indent = 0 else @empty = false @indent = SpaceCount.indent(line) end @index = index @status = nil # valid, invalid, unknown @invalid = false lex_detect! end
parse(source)
click to toggle source
# File lib/dead_end/code_line.rb, line 34 def self.parse(source) source.lines.map.with_index do |line, index| CodeLine.new(line: line, index: index) end end
Public Instance Methods
<=>(b)
click to toggle source
# File lib/dead_end/code_line.rb, line 95 def <=>(b) self.index <=> b.index end
empty?()
click to toggle source
# File lib/dead_end/code_line.rb, line 142 def empty? @empty end
indent_index()
click to toggle source
# File lib/dead_end/code_line.rb, line 91 def indent_index @indent_index ||= [indent, index] end
is_comment?()
click to toggle source
# File lib/dead_end/code_line.rb, line 99 def is_comment? @is_comment end
is_end?()
click to toggle source
# File lib/dead_end/code_line.rb, line 111 def is_end? @is_end end
is_kw?()
click to toggle source
# File lib/dead_end/code_line.rb, line 107 def is_kw? @is_kw end
line_number()
click to toggle source
# File lib/dead_end/code_line.rb, line 133 def line_number index + 1 end
Also aliased as: number
mark_invisible()
click to toggle source
# File lib/dead_end/code_line.rb, line 115 def mark_invisible @line = "" self end
mark_visible()
click to toggle source
# File lib/dead_end/code_line.rb, line 120 def mark_visible @line = @original_line self end
not_comment?()
click to toggle source
# File lib/dead_end/code_line.rb, line 103 def not_comment? !is_comment? end
not_empty?()
click to toggle source
# File lib/dead_end/code_line.rb, line 138 def not_empty? !empty? end
to_s()
click to toggle source
# File lib/dead_end/code_line.rb, line 146 def to_s self.line end
trailing_slash?()
click to toggle source
# File lib/dead_end/code_line.rb, line 87 def trailing_slash? @is_trailing_slash end
visible?()
click to toggle source
# File lib/dead_end/code_line.rb, line 125 def visible? !line.empty? end
Private Instance Methods
lex_detect!()
click to toggle source
# File lib/dead_end/code_line.rb, line 59 def lex_detect! lex_array = LexAll.new(source: line) kw_count = 0 end_count = 0 lex_array.each_with_index do |lex, index| next unless lex.type == :on_kw case lex.token when 'if', 'unless', 'while', 'until' # Only count if/unless when it's not a "trailing" if/unless # https://github.com/ruby/ruby/blob/06b44f819eb7b5ede1ff69cecb25682b56a1d60c/lib/irb/ruby-lex.rb#L374-L375 kw_count += 1 if !lex.expr_label? when 'def', 'case', 'for', 'begin', 'class', 'module', 'do' kw_count += 1 when 'end' end_count += 1 end end @is_comment = lex_array.detect {|lex| lex.type != :on_sp}&.type == :on_comment return if @is_comment @is_kw = (kw_count - end_count) > 0 @is_end = (end_count - kw_count) > 0 @is_trailing_slash = lex_array.last.token == TRAILING_SLASH end