Class: SyntaxTree::RBS::Annotations

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/rbs/utils.rb

Overview

An annotation can be attached to many kinds of nodes, and should be printed using %a{}. This class wraps a set of annotations and provides the ability to print them if they are found.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(annotations) ⇒ Annotations

Returns a new instance of Annotations.



11
12
13
# File 'lib/syntax_tree/rbs/utils.rb', line 11

def initialize(annotations)
  @annotations = annotations
end

Instance Attribute Details

#annotationsObject (readonly)

Returns the value of attribute annotations.



9
10
11
# File 'lib/syntax_tree/rbs/utils.rb', line 9

def annotations
  @annotations
end

Class Method Details

.maybe_format(q, annotations) ⇒ Object



39
40
41
# File 'lib/syntax_tree/rbs/utils.rb', line 39

def self.maybe_format(q, annotations)
  new(annotations).format(q) if annotations.any?
end

.maybe_pretty_print(q, annotations) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/syntax_tree/rbs/utils.rb', line 43

def self.maybe_pretty_print(q, annotations)
  if annotations.any?
    q.breakable
    q.text("annotations=")
    q.pp(new(annotations))
  end
end

Instance Method Details

#format(q) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/syntax_tree/rbs/utils.rb', line 15

def format(q)
  q.seplist(annotations, -> { q.breakable(force: true) }) do |annotation|
    if annotation.string.match?(/[{}]/)
      # Bail out and just print the source string if there are any braces
      # because we don't want to mess with escaping them.
      q.text(q.source[annotation.location.range])
    else
      q.text("%a{")
      q.text(annotation.string)
      q.text("}")
    end
  end
  q.breakable(force: true)
end

#pretty_print(q) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/syntax_tree/rbs/utils.rb', line 30

def pretty_print(q)
  q.seplist(annotations) do |annotation|
    q.group(2, "(annotation", ")") do
      q.breakable
      q.pp(annotation.string)
    end
  end
end