Skip to content

Commit

Permalink
Merge pull request #1851 from lf-lang/more-more-formatter-fixes
Browse files Browse the repository at this point in the history
More formatter fixes
  • Loading branch information
cmnrd authored Jun 21, 2023
2 parents bfe8c76 + 0979e64 commit 321aacf
Show file tree
Hide file tree
Showing 790 changed files with 5,861 additions and 5,792 deletions.
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

0 comments on commit 321aacf

Please sign in to comment.