Skip to content

Commit

Permalink
Simplify pr method.
Browse files Browse the repository at this point in the history
The work ostensibly done by this method is, I think, taken care of elsewhere. A bug resulted from its complexity.
  • Loading branch information
petervdonovan committed May 18, 2022
1 parent f1c98da commit d62445f
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions org.lflang/src/org/lflang/generator/CodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,31 +98,11 @@ public void pr(String format, Object... args) {
}

/**
* Append the specified text plus a final newline to the specified
* code buffer. This also replaces tabs with four spaces.
* @param text The the object whose toString() method provides the text.
* Append the given text to the code buffer at the current indentation level.
*/
public void pr(Object text) {
String string = text.toString();
string = string.replaceAll("\t", " ");
String[] split = string.split("\n");
int offset = Stream.of(split).skip(1)
.mapToInt(line -> line.indexOf(line.trim()))
.min()
.orElse(0);
// Now make a pass for each line, replacing the offset leading
// spaces with the current indentation.
boolean firstLine = true;
for (String line : split) {
code.append(indentation);
// Do not trim the first line
if (firstLine) {
code.append(line);
firstLine = false;
} else {
code.append(line.substring(offset));
}
code.append("\n");
public void pr(CharSequence text) {
for (String line : (Iterable<? extends String>) () -> text.toString().lines().iterator()) {

This comment has been minimized.

Copy link
@lhstrh

lhstrh May 18, 2022

Member

This really does look like a nice simplification :-D What was the bug?

code.append(indentation).append(line).append(System.lineSeparator());
}
}

Expand Down

0 comments on commit d62445f

Please sign in to comment.