Class: Astel::Rewriter
- Inherits:
-
Object
show all
- Includes:
- Diffable, Structured
- Defined in:
- lib/astel/rewriter.rb,
lib/astel/rewriter/edit_index.rb,
lib/astel/rewriter/structured.rb,
lib/astel/rewriter/structured/comments.rb,
lib/astel/rewriter/structured/formatting.rb,
lib/astel/rewriter/structured/collections.rb,
lib/astel/rewriter/structured/source_indenter.rb
Defined Under Namespace
Modules: Structured
Classes: ConflictError, Edit, EditIndex, ValidationError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Structured
#comment_out, #dedent, #find_keyword_argument, #indent, #indentation_of, #insert_into_body, #insert_keyword_argument, #insert_line_after, #insert_line_before, #insert_list_element, #leading_comments, #reindent, #remove_keyword_argument, #remove_line, #remove_list_element, #remove_list_elements, #remove_with_comments, #replace_keyword_key, #replace_keyword_value, #trailing_comment
Methods included from Diffable
#to_diff
Constructor Details
#initialize(source_file, duplicate_insertions: :accept) ⇒ Rewriter
Returns a new instance of Rewriter.
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/astel/rewriter.rb', line 36
def initialize(source_file, duplicate_insertions: :accept)
unless %i[accept raise].include?(duplicate_insertions)
raise ArgumentError, 'duplicate_insertions must be :accept or :raise'
end
@source_file = source_file
@duplicate_insertions = duplicate_insertions
@edits = []
@edits_snapshot = nil
@edit_index = EditIndex.new
@sequence = 0
@result_size_delta = 0
@transaction_staging = false
@transaction_failed = false
end
|
Instance Attribute Details
#source_file ⇒ Object
Returns the value of attribute source_file.
34
35
36
|
# File 'lib/astel/rewriter.rb', line 34
def source_file
@source_file
end
|
Instance Method Details
#edits ⇒ Object
52
53
54
|
# File 'lib/astel/rewriter.rb', line 52
def edits
@edits_snapshot ||= @edits.dup.freeze
end
|
#insert_after(location, content) ⇒ Object
66
67
68
69
|
# File 'lib/astel/rewriter.rb', line 66
def insert_after(location, content)
_, end_offset = offsets(location)
add_edit(end_offset, end_offset, content)
end
|
#insert_before(location, content) ⇒ Object
61
62
63
64
|
# File 'lib/astel/rewriter.rb', line 61
def insert_before(location, content)
start_offset, = offsets(location)
add_edit(start_offset, start_offset, content)
end
|
#merge!(other) ⇒ Object
83
84
85
86
87
88
|
# File 'lib/astel/rewriter.rb', line 83
def merge!(other)
validated_edits_from(other).each do |edit|
add_edit(edit.start_offset, edit.end_offset, edit.replacement)
end
self
end
|
#remove(location) ⇒ Object
71
72
73
|
# File 'lib/astel/rewriter.rb', line 71
def remove(location)
replace(location, '')
end
|
#replace(location, replacement) ⇒ Object
56
57
58
59
|
# File 'lib/astel/rewriter.rb', line 56
def replace(location, replacement)
start_offset, end_offset = offsets(location)
add_edit(start_offset, end_offset, replacement)
end
|
#rewrite(validate: false) ⇒ Object
104
105
106
107
108
|
# File 'lib/astel/rewriter.rb', line 104
def rewrite(validate: false)
result = apply_edits
validate_result(result, validate)
result
end
|
#transaction(raise_on_conflict: false) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/astel/rewriter.rb', line 90
def transaction(raise_on_conflict: false)
staging = transaction_staging
yield staging
return transaction_failed if transaction_failed?(staging)
merge!(staging)
true
rescue ConflictError
@transaction_failed = true if @transaction_staging
raise if raise_on_conflict
false
end
|
#wrap(location, before, after) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/astel/rewriter.rb', line 75
def wrap(location, before, after)
transaction(raise_on_conflict: true) do |rewriter|
rewriter.insert_before(location, before)
rewriter.insert_after(location, after)
end
self
end
|