Class: Yoda::AST::CommentBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/ast/comment_block.rb,
lib/yoda/ast/comment_block/token.rb,
lib/yoda/ast/comment_block/tag_part.rb,
lib/yoda/ast/comment_block/base_part.rb,
lib/yoda/ast/comment_block/text_part.rb,
lib/yoda/ast/comment_block/range_calculation.rb,
lib/yoda/ast/comment_block/tag_text_name_part.rb,
lib/yoda/ast/comment_block/tag_text_type_part.rb

Defined Under Namespace

Modules: RangeCalculation Classes: BasePart, TagPart, TagTextNamePart, TagTextTypePart, TextPart, Token

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comments, node: nil) ⇒ CommentBlock

Returns a new instance of CommentBlock.

Parameters:

  • comments (Array<::Parser::Source::Comment>)
  • node (Vnode) (defaults to: nil)


34
35
36
37
# File 'lib/yoda/ast/comment_block.rb', line 34

def initialize(comments, node: nil)
  @comments = Array(comments)
  @node = node
end

Instance Attribute Details

#commentsArray<::Parser::Source::Comment> (readonly)

Returns:

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


27
28
29
# File 'lib/yoda/ast/comment_block.rb', line 27

def comments
  @comments
end

#nodeVnode (readonly)

Returns:



30
31
32
# File 'lib/yoda/ast/comment_block.rb', line 30

def node
  @node
end

Class Method Details

.build_part(comment_block:, token:) ⇒ BasePart



15
16
17
18
19
20
21
22
23
24
# File 'lib/yoda/ast/comment_block.rb', line 15

def self.build_part(comment_block:, token:)
  case token
  when Parsing::CommentTokenizer::Sequence
    TagPart.new(comment_block: comment_block, token: token)
  when Parsing::CommentTokenizer::Text
    TextPart.new(comment_block: comment_block, token: token)
  else
    fail "unexpected"
  end
end

Instance Method Details

#begin_locationParsing::Location?

Returns:



91
92
93
94
# File 'lib/yoda/ast/comment_block.rb', line 91

def begin_location
  return nil unless Parsing::Location.valid_location?(comments.first.location)
  Parsing::Location.new(row: comments.first.location.line, column: comments.first.location.column)
end

#build_part(token) ⇒ BasePart

Parameters:

Returns:



86
87
88
# File 'lib/yoda/ast/comment_block.rb', line 86

def build_part(token)
  self.class.build_part(comment_block: self, token: token)
end

#end_locationParsing::Location?

Returns:



97
98
99
100
# File 'lib/yoda/ast/comment_block.rb', line 97

def end_location
  return nil unless Parsing::Location.valid_location?(comments.last.location)
  Parsing::Location.new(row: comments.last.location.last_line, column: comments.last.location.last_column)
end

#location_from_index(index) ⇒ Parsing::Location?

Parameters:

  • index (Integer)

Returns:



66
67
68
69
70
# File 'lib/yoda/ast/comment_block.rb', line 66

def location_from_index(index)
  partial_text = text.slice(::Range.new(0, index))
  *lines_before, curent_line = partial_text.split("\n", -1)
  begin_location&.move(row: lines_before.length, column: curent_line&.length || 0)
end

#nearest_part(location) ⇒ BasePart?

Parameters:

Returns:



53
54
55
# File 'lib/yoda/ast/comment_block.rb', line 53

def nearest_part(location)
  parts.find { |part| part.include?(location) }
end

#nearest_tag_part(location) ⇒ TagPart?

Parameters:

Returns:



59
60
61
62
# File 'lib/yoda/ast/comment_block.rb', line 59

def nearest_tag_part(location)
  part = nearest_part(location)
  part&.is_a?(TagPart) ? part : nil
end

#offset_from_location(location) ⇒ Integer?

Parameters:

Returns:

  • (Integer, nil)


74
75
76
77
78
79
80
81
82
# File 'lib/yoda/ast/comment_block.rb', line 74

def offset_from_location(location)
  if begin_location
    row_diff = location.row - begin_location.row
    column_diff = location.column - begin_location.column

    lines = text.split("\n", -1)
    lines.slice(0, row_diff).join("\n").length + column_diff
  end
end

#partsArray<BasePart>

Returns:



45
46
47
48
49
# File 'lib/yoda/ast/comment_block.rb', line 45

def parts
  @parts ||= parsed_tokens.map do |token|
    build_part(token)
  end
end

#rangeParsing::Range?

Returns:



103
104
105
106
107
108
109
# File 'lib/yoda/ast/comment_block.rb', line 103

def range
  if begin_location && end_location
    Parsing::Range.new(begin_location, end_location)
  else
    nil
  end
end

#textString

Returns:

  • (String)


40
41
42
# File 'lib/yoda/ast/comment_block.rb', line 40

def text
  comments.map(&:text).join
end