Skip to content

Commit 96edaf3

Browse files
committed
Mark newlines for lazy DefNodes
* By running the MarkNewlinesVisitor on them when they are lazily loaded. * See #3983. * Achieves the same nodes as with eager DefNode.
1 parent fb80fe2 commit 96edaf3

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

java/org/ruby_lang/prism/MarkNewlinesVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ final class MarkNewlinesVisitor extends AbstractNodeVisitor<Void> {
66
private final Nodes.Source source;
77
private boolean[] newlineMarked;
88

9-
MarkNewlinesVisitor(Nodes.Source source, boolean[] newlineMarked) {
9+
MarkNewlinesVisitor(Nodes.Source source) {
1010
this.source = source;
11-
this.newlineMarked = newlineMarked;
11+
this.newlineMarked = new boolean[1 + source.getLineCount()];
1212
}
1313

1414
@Override

templates/java/org/ruby_lang/prism/Loader.java.erb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<%- string_type = Prism::Template::JAVA_STRING_TYPE -%>
22
package org.ruby_lang.prism;
33

4-
import org.ruby_lang.prism.Nodes;
5-
64
import java.lang.Short;
75
import java.math.BigInteger;
86
import java.nio.ByteBuffer;
@@ -76,13 +74,14 @@ public class Loader {
7674
private Charset encodingCharset;
7775
<%- end -%>
7876
private ConstantPool constantPool;
77+
private Nodes.Source source = null;
7978

8079
protected Loader(byte[] serialized) {
8180
this.buffer = ByteBuffer.wrap(serialized).order(ByteOrder.nativeOrder());
8281
}
8382

8483
protected ParseResult load() {
85-
Nodes.Source source = new Nodes.Source();
84+
this.source = new Nodes.Source();
8685

8786
expect((byte) 'P', "incorrect prism header");
8887
expect((byte) 'R', "incorrect prism header");
@@ -127,8 +126,7 @@ public class Loader {
127126
throw new Error("Expected to consume all bytes while deserializing but there were " + left + " bytes left");
128127
}
129128

130-
boolean[] newlineMarked = new boolean[1 + source.getLineCount()];
131-
MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source, newlineMarked);
129+
MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source);
132130
node.accept(visitor);
133131
} else {
134132
node = null;
@@ -407,11 +405,17 @@ public class Loader {
407405
}
408406

409407
Nodes.DefNode createDefNodeFromSavedPosition(<%= base_params_sig -%>, int bufferPosition) {
408+
Nodes.DefNode node;
410409
// This method mutates the buffer position and may be called from different threads so we must synchronize
411410
synchronized (this) {
412411
buffer.position(bufferPosition);
413-
return createDefNode(<%= base_params.join(", ") -%>);
412+
node = createDefNode(<%= base_params.join(", ") -%>);
414413
}
414+
415+
MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source);
416+
node.accept(visitor);
417+
418+
return node;
415419
}
416420
<%- array_types.uniq.each do |type| -%>
417421

0 commit comments

Comments
 (0)