app/models/effective/code_writer.rb in effective_developer-0.0.10 vs app/models/effective/code_writer.rb in effective_developer-0.1.1
- old
+ new
@@ -20,23 +20,23 @@
lines.each { |line| file.write(line) }
end
end
# Returns true if the insert happened, nil if no insert
- def insert_after_last(content, &block)
+ def insert_after_last(content, depth: nil, content_depth: nil, &block)
index = last(&block)
return nil unless index
- insert(content, index)
+ insert(content, index, depth: depth, content_depth: content_depth)
end
# Returns true if the insert happened, nil if no insert
- def insert_before_last(content, &block)
+ def insert_before_last(content, depth: nil, content_depth: nil, &block)
index = last(&block)
return nil unless index
- insert(content, index-1)
+ insert(content, index-1, depth: depth, content_depth: content_depth)
end
def within(content, &block)
content ||= 0
@@ -51,11 +51,11 @@
@from.push(from); @to.push(to)
yield
@from.pop; @to.pop
end
- def insert(content, index, depth = nil)
+ def insert(content, index, depth: nil, content_depth: nil)
contents = (content.kind_of?(Array) ? content : content.split(newline)).map { |str| str.strip }
depth ||= depth_at(index)
# If the line we're inserting at is a block, fast-forward the end of the block. And add a newline.
@@ -65,11 +65,11 @@
elsif !whitespace?(index) && (open?(contents) || !same?(contents, index))
index += 1
lines.insert(index, newline)
end
- content_depth = 0
+ content_depth ||= 0
index = index + 1 # Insert after the given line
contents.each do |content|
content_depth -= 1 if close?(content)
@@ -197,10 +197,10 @@
private
def open?(content)
stripped = ss(content)
- [' do'].any? { |end_with| stripped.end_with?(end_with) } ||
+ [' do'].any? { |end_with| stripped.split('#').first.to_s.end_with?(end_with) } ||
['class ', 'module ', 'def ', 'if '].any? { |start_with| stripped.start_with?(start_with) }
end
def close?(content)
stripped = ss(content, array_method: :last)