Skip to content

Commit 8c44a9d

Browse files
committed
Loop forward
This will allow us to combine buffers
1 parent d809f52 commit 8c44a9d

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

fixtures/page.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ module Example
55

66
class Page < Phlex::HTML
77
def view_template
8-
h1
8+
h1 { "Hello" }
99
h1
1010
1.times do
1111
h1
1212
end
1313
h1
1414
h1
1515
end
16+
17+
def foobar
18+
end
1619
end
1720
end

lib/phlex/compiler/method_compiler.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@ def compile(node)
1414
source = @compiler.source.byteslice(method_offset, node.location.length)
1515

1616
if @mutations.any?
17-
@mutations.sort_by! { |(offset, length, replacement)| offset }
17+
@mutations.sort_by! { |(pos)| pos }
1818

19-
@mutations.reverse_each do |(offset, length, replacement)|
20-
source[offset - method_offset, length] = replacement
19+
index = 0
20+
new_source = +""
21+
22+
@mutations.each do |(pos, length, replacement)|
23+
pos -= method_offset
24+
25+
new_source << source.byteslice(index, pos - index)
26+
new_source << replacement
27+
28+
index = pos + length
2129
end
2230

23-
puts source
24-
@compiler.component.redefine_compiled_method(source, @compiler.path, node.location.start_line)
31+
new_source << source.byteslice(index, source.bytesize - index)
32+
33+
puts new_source
34+
@compiler.component.redefine_compiled_method(new_source, @compiler.path, node.location.start_line)
2535
end
2636
end
2737

0 commit comments

Comments
 (0)