class Yoda::Parsing::Query::CurrentCommentQuery

Provides helper methods to find the current comment which means the comment on the current position.

Attributes

comments[R]
location[R]

Public Class Methods

new(comments, location) click to toggle source

@param comments [Array<::Parser::Source::Comment>] @param location [Location] represents the current position.

# File lib/yoda/parsing/query/current_comment_query.rb, line 10
def initialize(comments, location)
  fail ArgumentError, comments unless comments.all? { |comment| comment.is_a?(::Parser::Source::Comment) }
  fail ArgumentError, location unless location.is_a?(Location)
  @comments = comments
  @location = location
end

Public Instance Methods

absolute_position(position) click to toggle source

Calculate absolute position from the relative position. @param position [Location]

# File lib/yoda/parsing/query/current_comment_query.rb, line 50
def absolute_position(position)
  position.move(row: current_comment_block.first.location.line - 1, column: current_comment_block.first.location.column)
end
absolute_range(range) click to toggle source

Calculate absolute range from the relative range. @param range [Range]

# File lib/yoda/parsing/query/current_comment_query.rb, line 62
def absolute_range(range)
  range.move(row: current_comment_block.first.location.line - 1, column: current_comment_block.first.location.column)
end
begin_point_of_current_comment_block() click to toggle source

@return [Location]

# File lib/yoda/parsing/query/current_comment_query.rb, line 36
def begin_point_of_current_comment_block
  Location.new(row: current_comment_block.first.location.line, column: current_comment_block.first.location.column)
end
current_comment() click to toggle source

The single line comment which the current position is on. @return [::Parser::Source::Comment, nil]

# File lib/yoda/parsing/query/current_comment_query.rb, line 19
def current_comment
  @current_comment ||= comments.find { |comment| location.included?(comment.location) }
end
current_comment_block() click to toggle source

The multiple line comments which the current position is on. @return [Array<::Parser::Source::Comment>]

# File lib/yoda/parsing/query/current_comment_query.rb, line 25
def current_comment_block
  @current_comment_block ||= current_comment ? comment_blocks.find { |block| block.include?(current_comment) } : []
end
current_comment_block_text() click to toggle source

@return [String]

# File lib/yoda/parsing/query/current_comment_query.rb, line 67
def current_comment_block_text
  current_comment_block.map(&:text).join("\n")
end
location_in_current_comment_block() click to toggle source

The relative coordinates of the current position from the beginning position of the current comment. @return [Location]

# File lib/yoda/parsing/query/current_comment_query.rb, line 31
def location_in_current_comment_block
  relative_position(location)
end
relative_position(position) click to toggle source

Calculate relative position (the coordinates from the beginning point) from the relative position. @param position [Location]

# File lib/yoda/parsing/query/current_comment_query.rb, line 44
def relative_position(position)
  position.move(row: 1 - current_comment_block.first.location.line, column: - current_comment_block.first.location.column)
end
relative_range(range) click to toggle source

Calculate relative range from the relative range. @param range [Range]

# File lib/yoda/parsing/query/current_comment_query.rb, line 56
def relative_range(range)
  range.move(row: 1 - current_comment_block.first.location.line, column: - current_comment_block.first.location.column)
end

Private Instance Methods

comment_blocks() click to toggle source

@return [Array<Array<::Parser::Source::Comment>>]

# File lib/yoda/parsing/query/current_comment_query.rb, line 74
def comment_blocks
  @comment_blocks ||= comments.chunk_while { |i, j| i.location.line + 1 == j.location.line }
end