From cf17f5ac7c2b7a419bbbfc7ae2105628b9a0ac3d Mon Sep 17 00:00:00 2001 From: Peter Donovan Date: Tue, 20 Jun 2023 12:03:01 -0700 Subject: [PATCH] Make handling of comments more conservative. Also, handle ``` ``` preformatted blocks. --- .../java/org/lflang/ast/FormattingUtil.java | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/org/lflang/ast/FormattingUtil.java b/core/src/main/java/org/lflang/ast/FormattingUtil.java index 7eb7069ef2..0c594bd0b0 100644 --- a/core/src/main/java/org/lflang/ast/FormattingUtil.java +++ b/core/src/main/java/org/lflang/ast/FormattingUtil.java @@ -127,27 +127,31 @@ static String lineWrapComments(List 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) { + if (!MULTILINE_COMMENT.matcher(comment).matches()) return comment; width = Math.max(width, MINIMUM_COMMENT_WIDTH_IN_COLUMNS); - List> paragraphs = - Arrays.stream( - comment - .strip() - .replaceAll("^/?((\\*|//|#)\\s*)+", "") - .replaceAll("\\s*\\*/$", "") - .replaceAll("(?<=(\\r\\n|\\r|\\n))\\h*(\\*|//|#)\\h*", "") - .split("(\n\\s*){2,}")) + var stripped = comment + .strip() + .replaceAll("^/?((\\*|//|#)\\s*)+", "") + .replaceAll("\\s*\\*/$", "") + .replaceAll("(?<=(\\r\\n|\\r|\\n))\\h*(\\*|//|#)\\h?(\\h*)", "$3"); + 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(); - 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, " * ")); + .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); } /** @@ -158,15 +162,15 @@ private static String lineWrapComment(String comment, int width, String singleLi * @param linePrefix A string to prepend to each line of comment. */ private static String lineWrapComment( - List> paragraphs, int width, String linePrefix) { + List> paragraphs, int width, String linePrefix) { int widthAfterPrefix = Math.max(width - linePrefix.length(), MINIMUM_COMMENT_WIDTH_IN_COLUMNS); return paragraphs.stream() - .map( - paragraph -> - wrapLines(paragraph, widthAfterPrefix) - .map(s -> (linePrefix + s.stripLeading()).stripTrailing()) - .collect(Collectors.joining("\n"))) - .collect(Collectors.joining(String.format("\n%s\n", linePrefix.stripTrailing()))); + .map( + paragraph -> + wrapLines(paragraph, widthAfterPrefix) + .map(s -> (linePrefix + s.stripLeading()).stripTrailing()) + .collect(Collectors.joining("\n"))) + .collect(Collectors.joining(String.format("\n%s\n", linePrefix.stripTrailing()))); } /** Wrap a given paragraph. */