Class: TreeHaver::Base::Comment
- Inherits:
-
Object
- Object
- TreeHaver::Base::Comment
- Defined in:
- lib/tree_haver/base/comment.rb
Overview
Base class for backend comment wrappers.
This defines the parser-facing contract for normalized comment wrappers in
TreeHaver. Backends that can expose comment objects should subclass this and
implement text/type/location accessors using their native parser data.
Direct Known Subclasses
Constant Summary collapse
- ATTACHMENT_HINTS =
%i[leading inline trailing].freeze
Instance Attribute Summary collapse
-
#attachment_hint ⇒ Symbol?
readonly
Optional parser-provided attachment hint.
-
#inner_comment ⇒ Object
readonly
The underlying backend-specific comment object.
-
#source ⇒ String?
readonly
The source text used for fallback range extraction.
Instance Method Summary collapse
-
#attachment_hint? ⇒ Boolean
-
#end_byte ⇒ Integer
Get the end byte offset of the comment.
-
#end_line ⇒ Object
-
#end_point ⇒ Hash{Symbol => Integer}
Get the end position (row/column, 0-based).
-
#initialize(comment, source: nil, attachment_hint: nil) ⇒ Comment
constructor
A new instance of Comment.
-
#inline? ⇒ Boolean
-
#inspect ⇒ Object
-
#leading? ⇒ Boolean
-
#source_position ⇒ Object
-
#start_byte ⇒ Integer
Get the start byte offset of the comment.
-
#start_line ⇒ Object
-
#start_point ⇒ Hash{Symbol => Integer}
Get the start position (row/column, 0-based).
-
#style ⇒ Symbol?
Get the normalized delimiter style.
-
#text ⇒ String
Get the comment text including delimiters when appropriate.
-
#trailing? ⇒ Boolean
-
#type ⇒ String
Get the normalized comment type.
Constructor Details
#initialize(comment, source: nil, attachment_hint: nil) ⇒ Comment
Returns a new instance of Comment.
25 26 27 28 29 |
# File 'lib/tree_haver/base/comment.rb', line 25 def initialize(comment, source: nil, attachment_hint: nil) @inner_comment = comment @source = source @attachment_hint = () end |
Instance Attribute Details
#attachment_hint ⇒ Symbol? (readonly)
Optional parser-provided attachment hint.
23 24 25 |
# File 'lib/tree_haver/base/comment.rb', line 23 def @attachment_hint end |
#inner_comment ⇒ Object (readonly)
The underlying backend-specific comment object.
15 16 17 |
# File 'lib/tree_haver/base/comment.rb', line 15 def inner_comment @inner_comment end |
#source ⇒ String? (readonly)
The source text used for fallback range extraction.
19 20 21 |
# File 'lib/tree_haver/base/comment.rb', line 19 def source @source end |
Instance Method Details
#attachment_hint? ⇒ Boolean
74 75 76 |
# File 'lib/tree_haver/base/comment.rb', line 74 def !.nil? end |
#end_byte ⇒ Integer
Get the end byte offset of the comment.
52 53 54 |
# File 'lib/tree_haver/base/comment.rb', line 52 def end_byte raise NotImplementedError, "#{self.class}#end_byte must be implemented" end |
#end_line ⇒ Object
94 95 96 |
# File 'lib/tree_haver/base/comment.rb', line 94 def end_line end_point[:row] + 1 end |
#end_point ⇒ Hash{Symbol => Integer}
Get the end position (row/column, 0-based).
64 65 66 |
# File 'lib/tree_haver/base/comment.rb', line 64 def end_point {row: 0, column: 0} end |
#inline? ⇒ Boolean
82 83 84 |
# File 'lib/tree_haver/base/comment.rb', line 82 def inline? == :inline end |
#inspect ⇒ Object
107 108 109 |
# File 'lib/tree_haver/base/comment.rb', line 107 def inspect "#<#{self.class} type=#{type.inspect} range=#{start_byte}...#{end_byte}>" end |
#leading? ⇒ Boolean
78 79 80 |
# File 'lib/tree_haver/base/comment.rb', line 78 def leading? == :leading end |
#source_position ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/tree_haver/base/comment.rb', line 98 def source_position { start_line: start_line, end_line: end_line, start_column: start_point[:column], end_column: end_point[:column], } end |
#start_byte ⇒ Integer
Get the start byte offset of the comment.
46 47 48 |
# File 'lib/tree_haver/base/comment.rb', line 46 def start_byte raise NotImplementedError, "#{self.class}#start_byte must be implemented" end |
#start_line ⇒ Object
90 91 92 |
# File 'lib/tree_haver/base/comment.rb', line 90 def start_line start_point[:row] + 1 end |
#start_point ⇒ Hash{Symbol => Integer}
Get the start position (row/column, 0-based).
58 59 60 |
# File 'lib/tree_haver/base/comment.rb', line 58 def start_point {row: 0, column: 0} end |
#style ⇒ Symbol?
Get the normalized delimiter style.
70 71 72 |
# File 'lib/tree_haver/base/comment.rb', line 70 def style nil end |
#text ⇒ String
Get the comment text including delimiters when appropriate.
40 41 42 |
# File 'lib/tree_haver/base/comment.rb', line 40 def text raise NotImplementedError, "#{self.class}#text must be implemented" end |
#trailing? ⇒ Boolean
86 87 88 |
# File 'lib/tree_haver/base/comment.rb', line 86 def trailing? == :trailing end |
#type ⇒ String
Get the normalized comment type.
Examples: “inline_comment”, “block_comment”.
34 35 36 |
# File 'lib/tree_haver/base/comment.rb', line 34 def type raise NotImplementedError, "#{self.class}#type must be implemented" end |