Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More formatter fixes #1851

Merged
merged 11 commits into from
Jun 21, 2023
  •  
  •  
  •  
9 changes: 4 additions & 5 deletions cli/lff/src/test/java/org/lflang/cli/LffCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ reactor Filter(period: int = 0, b: double[] = {0, 0}) {
}

main reactor {
az_f = new Filter(period = 100, b = {0.229019233988375, 0.421510777305010})
az_f = new Filter(period=100, b = {0.229019233988375, 0.421510777305010})
}
"""),
List.of(
Expand Down Expand Up @@ -165,10 +165,9 @@ reactor Filter(period: int = 0, b: double[] = {0, 0}) {

reactor MACService {
mul_cm = new ContextManager<
loooooooooooooooooooooooooooooong,
looooooooooooooong,
loooooooooooooong
>()
loooooooooooooooooooooooooooooong,
looooooooooooooong,
loooooooooooooong>()
}
"""));

Expand Down
58 changes: 39 additions & 19 deletions core/src/main/java/org/lflang/ast/FormattingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,47 @@ static String lineWrapComments(List<String> comments, int width, String singleLi
}
/** Wrap lines. Do not merge lines that start with weird characters. */
private static String lineWrapComment(String comment, int width, String singleLineCommentPrefix) {
var multiline = MULTILINE_COMMENT.matcher(comment).matches();
comment = comment.strip().replaceAll("(^|(?<=\n))\\h*(/\\*+|//|#)", "");
if (!multiline)
return comment.isBlank()
? ""
: comment
.replaceAll("(^|(?<=\n))\s*(?=\\w)", " ")
.replaceAll("^|(?<=\n)", singleLineCommentPrefix);
width = Math.max(width, MINIMUM_COMMENT_WIDTH_IN_COLUMNS);
List<List<String>> paragraphs =
Arrays.stream(
comment
.strip()
.replaceAll("^/?((\\*|//|#)\\s*)+", "")
.replaceAll("\\s*\\*/$", "")
.replaceAll("(?<=(\\r\\n|\\r|\\n))\\h*(\\*|//|#)\\h*", "")
.split("(\n\\s*){2,}"))
.map(s -> Arrays.stream(s.split("(\\r\\n|\\r|\\n)\\h*(?=[@#$%^&*\\-+=:;<>/])")))
.map(stream -> stream.map(s -> s.replaceAll("\\s+", " ")))
.map(Stream::toList)
.toList();
if (MULTILINE_COMMENT.matcher(comment).matches()) {
if (paragraphs.size() == 1 && paragraphs.get(0).size() == 1) {
String singleLineRepresentation = String.format("/** %s */", paragraphs.get(0).get(0));
if (singleLineRepresentation.length() <= width) return singleLineRepresentation;
}
return String.format("/**\n%s\n */", lineWrapComment(paragraphs, width, " * "));
var stripped =
comment
.replaceAll("\\s*\\*/$", "")
.replaceAll("(?<=(\\r\\n|\\r|\\n))\\h*(\\*|//|#)\\h?(\\h*)", "$3")
.strip();
var preformatted = false;
StringBuilder result = new StringBuilder(stripped.length() * 2);
for (var part : stripped.split("```")) {
result.append(
preformatted
? part.lines()
.skip(1)
.map(it -> " * " + it)
.collect(Collectors.joining("\n", "\n * ```\n", "\n * ```\n"))
: lineWrapComment(
Arrays.stream(part.split("(\n\\s*){2,}"))
.map(
s ->
Arrays.stream(s.split("(\\r\\n|\\r|\\n)\\h*(?=[@#$%^&*\\-+=:;<>/])")))
.map(stream -> stream.map(s -> s.replaceAll("\\s+", " ")))
.map(Stream::toList)
.toList(),
width,
" * "));
preformatted = !preformatted;
}
if (result.indexOf("\n") == -1) {
String singleLineRepresentation =
String.format("/** %s */", result.substring(result.indexOf(" * ") + 3));
if (singleLineRepresentation.length() <= width) return singleLineRepresentation;
}
return lineWrapComment(paragraphs, width, singleLineCommentPrefix + " ");
return String.format("/**\n%s\n */", result);
}

/**
Expand Down
46 changes: 42 additions & 4 deletions core/src/main/java/org/lflang/ast/MalleableString.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
Expand Down Expand Up @@ -91,6 +92,14 @@ public static MalleableString anyOf(Object... possibilities) {
return new Leaf(objectArrayToString(possibilities));
}

/**
* Apply the given constraint to leaf strings of this.
*
* <p>This is done on a best-effort basis in the sense that if no options satisfy the constraint,
* the constraint is not applied.
*/
public abstract MalleableString constrain(Predicate<String> constraint);

private static String[] objectArrayToString(Object[] objects) {
String[] ret = new String[objects.length];
for (int i = 0; i < objects.length; i++) {
Expand Down Expand Up @@ -407,6 +416,14 @@ private boolean optimizeChildren(
providedRender, badness, width, indentation, singleLineCommentPrefix));
}

@Override
public MalleableString constrain(Predicate<String> constraint) {
for (var component : components) {
component.constrain(constraint);
}
return this;
}

@Override
public boolean isEmpty() {
return components.stream().allMatch(MalleableString::isEmpty);
Expand Down Expand Up @@ -468,6 +485,12 @@ public RenderResult render(
.replaceAll("(?<=\n|^)(?=\\h*\\S)", " ".repeat(indentation)),
result.levelsOfCommentDisplacement());
}

@Override
public MalleableString constrain(Predicate<String> constraint) {
nested.constrain(constraint);
return this;
}
}

/** Represent a {@code MalleableString} that admits multiple possible representations. */
Expand Down Expand Up @@ -549,23 +572,31 @@ public RenderResult render(
sourceEObject != null ? sourceEObject : enclosingEObject)
.with(comments.stream());
}

@Override
public MalleableString constrain(Predicate<String> constraint) {
for (var possibility : possibilities) {
possibility.constrain(constraint);
}
return this;
}
}

/** A {@code Leaf} can be represented by multiple possible {@code String}s. */
private static final class Leaf extends MalleableStringWithAlternatives<String> {
private final ImmutableList<String> possibilities;
private List<String> possibilities;

private Leaf(String[] possibilities) {
this.possibilities = ImmutableList.copyOf(possibilities);
this.possibilities = List.of(possibilities);
}

private Leaf(String possibility) {
this.possibilities = ImmutableList.of(possibility);
this.possibilities = List.of(possibility);
}

@Override
protected List<String> getPossibilities() {
return this.possibilities;
return possibilities;
}

@Override
Expand All @@ -586,5 +617,12 @@ public RenderResult render(
: getChosenPossibility(),
0);
}

@Override
public MalleableString constrain(Predicate<String> constraint) {
var newPossibilities = possibilities.stream().filter(constraint).toList();
if (!newPossibilities.isEmpty()) possibilities = newPossibilities;
return this;
}
}
}
Loading