Sha256: 50c9b9171cc0f269a6e7c9e765e5836d62d15890b99328d67926c9347f7d280e

Contents?: true

Size: 1.7 KB

Versions: 12

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Prism
  class ParseResult
    # The :line tracepoint event gets fired whenever the Ruby VM encounters an
    # expression on a new line. The types of expressions that can trigger this
    # event are:
    #
    # * if statements
    # * unless statements
    # * nodes that are children of statements lists
    #
    # In order to keep track of the newlines, we have a list of offsets that
    # come back from the parser. We assign these offsets to the first nodes that
    # we find in the tree that are on those lines.
    #
    # Note that the logic in this file should be kept in sync with the Java
    # MarkNewlinesVisitor, since that visitor is responsible for marking the
    # newlines for JRuby/TruffleRuby.
    class Newlines < Visitor
      def initialize(newline_marked)
        @newline_marked = newline_marked
      end

      def visit_block_node(node)
        old_newline_marked = @newline_marked
        @newline_marked = Array.new(old_newline_marked.size, false)

        begin
          super(node)
        ensure
          @newline_marked = old_newline_marked
        end
      end

      alias_method :visit_lambda_node, :visit_block_node

      def visit_if_node(node)
        node.set_newline_flag(@newline_marked)
        super(node)
      end

      alias_method :visit_unless_node, :visit_if_node

      def visit_statements_node(node)
        node.body.each do |child|
          child.set_newline_flag(@newline_marked)
        end
        super(node)
      end
    end

    private_constant :Newlines

    # Walk the tree and mark nodes that are on a new line.
    def mark_newlines!
      value.accept(Newlines.new(Array.new(1 + source.offsets.size, false)))
    end
  end
end

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/lib/prism/parse_result/newlines.rb
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/lib/prism/parse_result/newlines.rb
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/lib/prism/parse_result/newlines.rb
study_line-0.1.3 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/lib/prism/parse_result/newlines.rb
study_line-0.1.2 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/lib/prism/parse_result/newlines.rb
study_line-0.1.1 vendor/bundle/ruby/3.2.0/gems/prism-0.15.1/lib/prism/parse_result/newlines.rb
prism-0.16.0 lib/prism/parse_result/newlines.rb
prism-0.15.1 lib/prism/parse_result/newlines.rb
prism-0.15.0 lib/prism/parse_result/newlines.rb
prism-0.14.0 lib/prism/parse_result/newlines.rb
yarp-0.13.0 lib/prism/parse_result/newlines.rb
prism-0.13.0 lib/prism/parse_result/newlines.rb