Class: SyntaxTree::RBS::Comment

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

Overview

A comment can be attached to many kinds of nodes, and should be printed before them. This class wraps a comment and provides the ability to print it if it is found.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment) ⇒ Comment

Returns a new instance of Comment.



58
59
60
# File 'lib/syntax_tree/rbs/utils.rb', line 58

def initialize(comment)
  @comment = comment
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



56
57
58
# File 'lib/syntax_tree/rbs/utils.rb', line 56

def comment
  @comment
end

Class Method Details

.maybe_format(q, comment) ⇒ Object



78
79
80
# File 'lib/syntax_tree/rbs/utils.rb', line 78

def self.maybe_format(q, comment)
  new(comment).format(q) if comment
end

.maybe_pretty_print(q, comment) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/syntax_tree/rbs/utils.rb', line 82

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

Instance Method Details

#format(q) ⇒ Object

Comments come in as one whole string, so here we split it up into multiple lines and then prefix it with the pound sign.



64
65
66
67
68
69
# File 'lib/syntax_tree/rbs/utils.rb', line 64

def format(q)
  q.seplist(comment.string.split(/\r?\n/), -> { q.breakable(force: true) }) do |line|
    q.text("# #{line}")
  end
  q.breakable(force: true)
end

#pretty_print(q) ⇒ Object



71
72
73
74
75
76
# File 'lib/syntax_tree/rbs/utils.rb', line 71

def pretty_print(q)
  q.group(2, "(comment", ")") do
    q.breakable
    q.pp(comment.string)
  end
end