Class: TreeHaver::Backends::Prism::Comment

Inherits:
TreeHaver::Base::Comment show all
Defined in:
lib/tree_haver/backends/prism.rb

Overview

Prism comment wrapper.

Prism exposes native Ruby comments via Prism::Comment subclasses and
Prism::Location objects. This wrapper normalizes those objects onto a
small parser-facing contract for downstream consumers.

Constant Summary

Constants inherited from TreeHaver::Base::Comment

TreeHaver::Base::Comment::ATTACHMENT_HINTS

Instance Attribute Summary

Attributes inherited from TreeHaver::Base::Comment

#attachment_hint, #inner_comment, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TreeHaver::Base::Comment

#attachment_hint?, #end_line, #initialize, #inline?, #inspect, #leading?, #source_position, #start_line, #trailing?

Constructor Details

This class inherits a constructor from TreeHaver::Base::Comment

Class Method Details

.comment_type_for(klass) ⇒ Object



359
360
361
362
363
# File 'lib/tree_haver/backends/prism.rb', line 359

def comment_type_for(klass)
  klass.name.split("::").last
    .gsub(/([a-z\d])([A-Z])/, '\\1_\\2')
    .downcase
end

Instance Method Details

#end_byteObject



336
337
338
# File 'lib/tree_haver/backends/prism.rb', line 336

def end_byte
  location.end_offset
end

#end_pointObject



347
348
349
350
351
352
# File 'lib/tree_haver/backends/prism.rb', line 347

def end_point
  {
    row: location.end_line - 1,
    column: location.end_column,
  }
end

#locationObject



320
321
322
# File 'lib/tree_haver/backends/prism.rb', line 320

def location
  inner_comment.location
end

#start_byteObject



332
333
334
# File 'lib/tree_haver/backends/prism.rb', line 332

def start_byte
  location.start_offset
end

#start_pointObject



340
341
342
343
344
345
# File 'lib/tree_haver/backends/prism.rb', line 340

def start_point
  {
    row: location.start_line - 1,
    column: location.start_column,
  }
end

#styleObject



354
355
356
# File 'lib/tree_haver/backends/prism.rb', line 354

def style
  :line
end

#textObject



328
329
330
# File 'lib/tree_haver/backends/prism.rb', line 328

def text
  inner_comment.slice
end

#typeObject



324
325
326
# File 'lib/tree_haver/backends/prism.rb', line 324

def type
  self.class.comment_type_for(inner_comment.class)
end