Class: TreeHaver::Base::Comment

Inherits:
Object
  • Object
show all
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

TreeHaver::Backends::Prism::Comment

Constant Summary collapse

ATTACHMENT_HINTS =
%i[leading inline trailing].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = normalize_attachment_hint(attachment_hint)
end

Instance Attribute Details

#attachment_hintSymbol? (readonly)

Optional parser-provided attachment hint.

Returns:

  • (Symbol, nil)


23
24
25
# File 'lib/tree_haver/base/comment.rb', line 23

def attachment_hint
  @attachment_hint
end

#inner_commentObject (readonly)

The underlying backend-specific comment object.

Returns:

  • (Object)


15
16
17
# File 'lib/tree_haver/base/comment.rb', line 15

def inner_comment
  @inner_comment
end

#sourceString? (readonly)

The source text used for fallback range extraction.

Returns:

  • (String, nil)


19
20
21
# File 'lib/tree_haver/base/comment.rb', line 19

def source
  @source
end

Instance Method Details

#attachment_hint?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/tree_haver/base/comment.rb', line 74

def attachment_hint?
  !attachment_hint.nil?
end

#end_byteInteger

Get the end byte offset of the comment.

Returns:

  • (Integer)

Raises:

  • (NotImplementedError)


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_lineObject



94
95
96
# File 'lib/tree_haver/base/comment.rb', line 94

def end_line
  end_point[:row] + 1
end

#end_pointHash{Symbol => Integer}

Get the end position (row/column, 0-based).

Returns:

  • (Hash{Symbol => Integer})


64
65
66
# File 'lib/tree_haver/base/comment.rb', line 64

def end_point
  {row: 0, column: 0}
end

#inline?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/tree_haver/base/comment.rb', line 82

def inline?
  attachment_hint == :inline
end

#inspectObject



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

Returns:

  • (Boolean)


78
79
80
# File 'lib/tree_haver/base/comment.rb', line 78

def leading?
  attachment_hint == :leading
end

#source_positionObject



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_byteInteger

Get the start byte offset of the comment.

Returns:

  • (Integer)

Raises:

  • (NotImplementedError)


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_lineObject



90
91
92
# File 'lib/tree_haver/base/comment.rb', line 90

def start_line
  start_point[:row] + 1
end

#start_pointHash{Symbol => Integer}

Get the start position (row/column, 0-based).

Returns:

  • (Hash{Symbol => Integer})


58
59
60
# File 'lib/tree_haver/base/comment.rb', line 58

def start_point
  {row: 0, column: 0}
end

#styleSymbol?

Get the normalized delimiter style.

Returns:

  • (Symbol, nil)


70
71
72
# File 'lib/tree_haver/base/comment.rb', line 70

def style
  nil
end

#textString

Get the comment text including delimiters when appropriate.

Returns:

  • (String)

Raises:

  • (NotImplementedError)


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

Returns:

  • (Boolean)


86
87
88
# File 'lib/tree_haver/base/comment.rb', line 86

def trailing?
  attachment_hint == :trailing
end

#typeString

Get the normalized comment type.
Examples: “inline_comment”, “block_comment”.

Returns:

  • (String)

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/tree_haver/base/comment.rb', line 34

def type
  raise NotImplementedError, "#{self.class}#type must be implemented"
end