Class: RBS::Inline::Annotator::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/inline/annotator/writer.rb

Overview

action:

insert_before(range, "# @rbs foo: Integer")
insert_before(range, "# @rbs bar: Integer")

expect:

# @rbs foo: Integer
# @rbs bar: Integer
def foo(foo, bar)

Defined Under Namespace

Classes: Action, InsertAfter, InsertBefore, Replace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Writer

Returns a new instance of Writer.



64
65
66
67
68
69
# File 'lib/rbs/inline/annotator/writer.rb', line 64

def initialize(source)
  @source = source
  @actions = []
  @slice = []
  @before_pos = 0
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



61
62
63
# File 'lib/rbs/inline/annotator/writer.rb', line 61

def actions
  @actions
end

#before_posObject

Returns the value of attribute before_pos.



62
63
64
# File 'lib/rbs/inline/annotator/writer.rb', line 62

def before_pos
  @before_pos
end

#sliceObject (readonly)

Returns the value of attribute slice.



61
62
63
# File 'lib/rbs/inline/annotator/writer.rb', line 61

def slice
  @slice
end

#sourceObject (readonly)

Returns the value of attribute source.



61
62
63
# File 'lib/rbs/inline/annotator/writer.rb', line 61

def source
  @source
end

Instance Method Details

#insert_after(range:, text:) ⇒ Object



75
76
77
# File 'lib/rbs/inline/annotator/writer.rb', line 75

def insert_after(range:, text:)
  actions << InsertAfter.new(range:, text:)
end

#insert_before(range:, text:) ⇒ Object



71
72
73
# File 'lib/rbs/inline/annotator/writer.rb', line 71

def insert_before(range:, text:)
  actions << InsertBefore.new(range:, text:)
end

#processObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rbs/inline/annotator/writer.rb', line 83

def process
  actions.sort_by { |action|
    action.range.begin
  }.each do |action|
    action.process(self)
  end

  if before_pos < source.length
    slice << source[before_pos..]
  end

  slice.join
end

#replace(range:, text:) ⇒ Object



79
80
81
# File 'lib/rbs/inline/annotator/writer.rb', line 79

def replace(range:, text:)
  actions << Replace.new(range:, text:)
end