Class: Commonmarker::Merge::Backend::Node

Inherits:
TreeHaver::Base::Node
  • Object
show all
Defined in:
lib/commonmarker/merge/backend.rb

Overview

Commonmarker node wrapper

Wraps Commonmarker::Node to provide TreeHaver::Node-compatible interface.

Instance Method Summary collapse

Instance Method Details

#childrenArray<Node>

Get child nodes

Returns:

  • (Array<Node>)

    Child nodes



127
128
129
130
131
132
133
# File 'lib/commonmarker/merge/backend.rb', line 127

def children
  return [] unless inner_node.respond_to?(:each)

  result = []
  inner_node.each { |child| result << Node.new(child, source: source, lines: lines) }
  result
end

#end_byteObject

Get end byte offset



142
143
144
145
# File 'lib/commonmarker/merge/backend.rb', line 142

def end_byte
  ep = end_point
  calculate_byte_offset(ep[:row], ep[:column])
end

#end_pointPoint

Get end point (0-based row/column)

Returns:

  • (Point)

    End position



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/commonmarker/merge/backend.rb', line 172

def end_point
  if inner_node.respond_to?(:source_position)
    begin
      pos = inner_node.source_position
      return Point.new(pos[:end_line] - 1, (pos[:end_column] || 1) - 1) if pos && pos[:end_line]
    rescue StandardError
      nil
    end
  end

  begin
    pos = inner_node.sourcepos
    return Point.new(pos[2] - 1, pos[3] - 1) if pos
  rescue StandardError
    nil
  end

  Point.new(0, 0)
end

#start_byteObject

Get start byte offset



136
137
138
139
# File 'lib/commonmarker/merge/backend.rb', line 136

def start_byte
  sp = start_point
  calculate_byte_offset(sp[:row], sp[:column])
end

#start_pointPoint

Get start point (0-based row/column)

Returns:

  • (Point)

    Start position



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/commonmarker/merge/backend.rb', line 149

def start_point
  if inner_node.respond_to?(:source_position)
    begin
      pos = inner_node.source_position
      return Point.new(pos[:start_line] - 1, (pos[:start_column] || 1) - 1) if pos && pos[:start_line]
    rescue StandardError
      nil
    end
  end

  # Fallback: check sourcepos (old API)
  begin
    pos = inner_node.sourcepos
    return Point.new(pos[0] - 1, pos[1] - 1) if pos
  rescue StandardError
    nil
  end

  Point.new(0, 0)
end

#textString

Get the text content of this node

Returns:

  • (String)

    Node text



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/commonmarker/merge/backend.rb', line 112

def text
  if inner_node.respond_to?(:string_content)
    begin
      content = inner_node.string_content.to_s
      return content unless content.empty?
    rescue TypeError
      # Container node - fall through
    end
  end
  children.map(&:text).join
end

#typeString Also known as: kind

Get the node type as a string

Returns:

  • (String)

    Node type



102
103
104
# File 'lib/commonmarker/merge/backend.rb', line 102

def type
  inner_node.type.to_s
end