class Unparser::Comments

Holds the comments that remain to be emitted

def self.source_range(node, part)

Other tags:
    Api: - private

Returns:
  • (nil) -
  • (Parser::Source::Range) -

Parameters:
  • part (Symbol) --
  • node (Parser::AST::Node) --
def self.source_range(node, part)
  location = node.location
  location.public_send(part) if location.respond_to?(part)
end

def consume(node, source_part = :expression)

Other tags:
    Api: - private

Returns:
  • (undefined) -

Parameters:
  • source_part (Symbol) --
  • node (Parser::AST::Node) --
def consume(node, source_part = :expression)
  range = source_range(node, source_part)
  @last_range_consumed = range if range
end

def initialize(comments)

Other tags:
    Api: - private

Returns:
  • (undefined) -

Parameters:
  • comments (Array) --
def initialize(comments)
  @comments = comments.dup
  @last_range_consumed = nil
end

def source_range(*arguments)

Other tags:
    Api: - private

Returns:
  • (undefined) -
def source_range(*arguments)
  self.class.source_range(*arguments)
end

def take_all

Other tags:
    Api: - private

Returns:
  • (Array) -
def take_all
  take_while { true }
end

def take_before(node, source_part)

Other tags:
    Api: - private

Returns:
  • (Array) -

Parameters:
  • source_part (Symbol) --
  • node (Parser::AST::Node) --
def take_before(node, source_part)
  range = source_range(node, source_part)
  if range
    take_while { |comment| comment.location.expression.end_pos <= range.begin_pos }
  else
    EMPTY_ARRAY
  end
end

def take_eol_comments

Other tags:
    Api: - private

Returns:
  • (Array) -
def take_eol_comments
  return EMPTY_ARRAY unless @last_range_consumed
  comments = take_up_to_line(@last_range_consumed.end.line)
  unshift_documents(comments)
end

def take_up_to_line(line)

def take_up_to_line(line)
  take_while { |comment| comment.location.expression.line <= line }
end

def take_while

def take_while
  number_to_take = @comments.index { |comment| !yield(comment) } || @comments.size
  @comments.shift(number_to_take)
end

def unshift_documents(comments)

def unshift_documents(comments)
  doc_comments, other_comments = comments.partition(&:document?)
  doc_comments.reverse_each { |comment| @comments.unshift(comment) }
  other_comments
end