Class: Yoda::Parsing::Query::CurrentCommentQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/parsing/query/current_comment_query.rb

Overview

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

Instance Attribute Summary collapse

Coordinate conversion collapse

Instance Method Summary collapse

Constructor Details

#initialize(comments, location) ⇒ CurrentCommentQuery

Returns a new instance of CurrentCommentQuery.

Parameters:

  • comments (Array<::Parser::Source::Comment>)
  • location (Location)

    represents the current position.



10
11
12
13
14
15
# 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

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



6
7
8
# File 'lib/yoda/parsing/query/current_comment_query.rb', line 6

def comments
  @comments
end

#locationObject (readonly)

Returns the value of attribute location.



6
7
8
# File 'lib/yoda/parsing/query/current_comment_query.rb', line 6

def location
  @location
end

Instance Method Details

#absolute_position(position) ⇒ Object

Calculate absolute position from the relative position.

Parameters:



50
51
52
# 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) ⇒ Object

Calculate absolute range from the relative range.

Parameters:



62
63
64
# 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_blockLocation

Returns:



36
37
38
# 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::Parser::Source::Comment?

The single line comment which the current position is on.

Returns:

  • (::Parser::Source::Comment, nil)


19
20
21
# 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_blockArray<::Parser::Source::Comment>

The multiple line comments which the current position is on.

Returns:

  • (Array<::Parser::Source::Comment>)


25
26
27
# 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_textString

Returns:

  • (String)


67
68
69
# 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_blockLocation

The relative coordinates of the current position from the beginning position of the current comment.

Returns:



31
32
33
# File 'lib/yoda/parsing/query/current_comment_query.rb', line 31

def location_in_current_comment_block
  relative_position(location)
end

#relative_position(position) ⇒ Object

Calculate relative position (the coordinates from the beginning point) from the relative position.

Parameters:



44
45
46
# 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) ⇒ Object

Calculate relative range from the relative range.

Parameters:



56
57
58
# 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