class Decode::Language::Ruby::Code

A Ruby-specific block of code.

Attributes

language[R]
text[R]

Public Class Methods

new(text, index, relative_to: nil, language: relative_to&.language) click to toggle source
# File lib/decode/language/ruby/code.rb, line 31
def initialize(text, index, relative_to: nil, language: relative_to&.language)
        @text = text
        @root = ::Parser::CurrentRuby.parse(text)
        @index = index
        @relative_to = relative_to
        @language = language
end

Public Instance Methods

extract(into = []) click to toggle source
# File lib/decode/language/ruby/code.rb, line 43
def extract(into = [])
        if @index
                traverse(@root, into)
        end
        
        return into
end

Private Instance Methods

traverse(node, into) click to toggle source
# File lib/decode/language/ruby/code.rb, line 53
def traverse(node, into)
        case node&.type
        when :send
                if reference = Reference.from_const(node, @language)
                        if definition = @index.lookup(reference, relative_to: @relative_to)
                                expression = node.location.selector
                                range = expression.begin_pos...expression.end_pos
                                into << Syntax::Link.new(range, definition)
                        end
                end
                
                # Extract constants from arguments:
                children = node.children[2..-1].each do |node|
                        traverse(node, into)
                end
        when :const
                if reference = Reference.from_const(node, @language)
                        if definition = @index.lookup(reference, relative_to: @relative_to)
                                expression = node.location.name
                                range = expression.begin_pos...expression.end_pos
                                into << Syntax::Link.new(range, definition)
                        end
                end
        when :begin
                node.children.each do |child|
                        traverse(child, into)
                end
        end
end