class Solargraph::Rails::RubyParser

Attributes

current_line_length[R]
current_line_number[R]

Public Class Methods

new(file_contents: '') click to toggle source
# File lib/solargraph/rails/ruby_parser.rb, line 6
def initialize(file_contents: '')
  @lines = file_contents.lines
  @comment_handlers = []
  @non_comment_handlers = []
  @class_handlers = []
  @module_handlers = []
end

Public Instance Methods

on_class(&blk) click to toggle source
# File lib/solargraph/rails/ruby_parser.rb, line 18
def on_class(&blk)
  @class_handlers << blk
end
on_comment(&blk) click to toggle source
# File lib/solargraph/rails/ruby_parser.rb, line 14
def on_comment(&blk)
  @comment_handlers << blk
end
on_module(&blk) click to toggle source
# File lib/solargraph/rails/ruby_parser.rb, line 22
def on_module(&blk)
  @module_handlers << blk
end
on_ruby_line(&blk) click to toggle source
# File lib/solargraph/rails/ruby_parser.rb, line 26
def on_ruby_line(&blk)
  @non_comment_handlers << blk
end
parse() click to toggle source
# File lib/solargraph/rails/ruby_parser.rb, line 30
def parse
  @lines
    .map(&:rstrip)
    .each_with_index do |line, i|
    @current_line_number = i
    @current_line_length = line.length

    if is_comment?(line)
      comment_content = line.gsub(/#\s*/, '')
      @comment_handlers.each { |handler| handler.call(comment_content) }
    else
      @non_comment_handlers.each { |handler| handler.call(line) }
    end

    if is_class?(line)
      line.scan(/(?:(?<!<\s)(?:(\b\w+\b)\:\:))/).flatten.each do |inline_module_name|
        @module_handlers.each { | handler| handler.call(inline_module_name) }
      end
      line.match(/class\s+(?:\w*?(?:\:\:))*([A-Z]\w*)/)
      klass_name = $1
      line.match(/(?:<\s+)((?:[A-Z]\w*(?:\:\:)?)*)/)
      superklass_name = $1
      @class_handlers.each { |handler| handler.call(klass_name, superklass_name) }
    end

    if is_module?(line)
      module_name = line.match(/^\s*module\s*?([A-Z]\w+)/)[1]
      @module_handlers.each { |handler| handler.call(module_name) }
    end
  end
end

Private Instance Methods

is_class?(line) click to toggle source
# File lib/solargraph/rails/ruby_parser.rb, line 68
def is_class?(line)
  line =~ /^[^#]?.*?class\s+?[A-Z]/
end
is_comment?(line) click to toggle source
# File lib/solargraph/rails/ruby_parser.rb, line 64
def is_comment?(line)
  line =~ (/^\s*#/)
end
is_module?(line) click to toggle source
# File lib/solargraph/rails/ruby_parser.rb, line 72
def is_module?(line)
  line =~ (/^\s*module\s*?([A-Z]\w+)/)
end