Skip to content

Commit

Permalink
Remove linebreak helper methods from StringUtils + use more inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Nov 28, 2024
1 parent fd322f8 commit 4af17b9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.regex.Pattern;

public class StringUtils {
private static final Pattern LINE_BREAK = Pattern.compile("\\R");
public static final Pattern LINE_BREAK = Pattern.compile("\\R");

private StringUtils() {
}
Expand Down Expand Up @@ -720,19 +720,4 @@ public static String formatUriForPropertiesFile(String uri) {
public static boolean hasLineBreak(@Nullable String s) {
return s != null && LINE_BREAK.matcher(s).find();
}

public static String substringOfBeforeFirstLineBreak(String s) {
String[] lines = LINE_BREAK.split(s);
return lines.length > 0 ? lines[0] : "";
}

public static String substringOfAfterFirstLineBreak(String s) {
String[] lines = LINE_BREAK.split(s);
return lines.length > 1 ? String.join("\n", Arrays.copyOfRange(lines, 1, lines.length)) : "";
}

public static String substringOfAfterLastLineBreak(String s) {
String[] parts = LINE_BREAK.split(s);
return parts.length > 1 ? parts[parts.length - 1] : "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import org.openrewrite.yaml.tree.Yaml;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.openrewrite.Cursor.ROOT_VALUE;
import static org.openrewrite.Tree.randomId;
import static org.openrewrite.internal.ListUtils.*;
import static org.openrewrite.internal.StringUtils.LINE_BREAK;
import static org.openrewrite.internal.StringUtils.hasLineBreak;
import static org.openrewrite.yaml.MergeYaml.REMOVE_PREFIX;

Expand Down Expand Up @@ -132,7 +134,7 @@ public Yaml visitMapping(Yaml.Mapping existingMapping, P p) {
if (getCursor().getMessage(REMOVE_PREFIX, false)) {
List<Yaml.Mapping.Entry> entries = ((Yaml.Mapping) getCursor().getValue()).getEntries();
return mapping.withEntries(mapLast(mapping.getEntries(), it ->
it.withPrefix(linebreak() + StringUtils.substringOfAfterFirstLineBreak(entries.get(entries.size() - 1).getPrefix()))));
it.withPrefix(linebreak() + substringOfAfterFirstLineBreak(entries.get(entries.size() - 1).getPrefix()))));
}

return mapping;
Expand Down Expand Up @@ -219,13 +221,13 @@ private Yaml.Mapping mergeMapping(Yaml.Mapping m1, Yaml.Mapping m2, P p, Cursor
// get comment from next element in same mapping block
for (int i = 0; i < entries.size() - 1; i++) {
if (entries.get(i).getValue().equals(getCursor().getValue())) {
comment = StringUtils.substringOfBeforeFirstLineBreak(entries.get(i + 1).getPrefix());
comment = substringOfBeforeFirstLineBreak(entries.get(i + 1).getPrefix());
break;
}
}
// or retrieve it for last item from next element (could potentially be much higher in the tree)
if (comment == null && hasLineBreak(entries.get(entries.size() - 1).getPrefix())) {
comment = StringUtils.substringOfBeforeFirstLineBreak(entries.get(entries.size() - 1).getPrefix());
comment = substringOfBeforeFirstLineBreak(entries.get(entries.size() - 1).getPrefix());
}
}

Expand All @@ -247,7 +249,7 @@ private void removePrefixForDirectChildren(List<Yaml.Mapping.Entry> m1Entries, L
if (m1Entries.get(i).getValue() instanceof Yaml.Mapping && mutatedEntries.get(i).getValue() instanceof Yaml.Mapping &&
((Yaml.Mapping) m1Entries.get(i).getValue()).getEntries().size() < ((Yaml.Mapping) mutatedEntries.get(i).getValue()).getEntries().size()) {
mutatedEntries.set(i + 1, mutatedEntries.get(i + 1).withPrefix(
linebreak() + StringUtils.substringOfAfterFirstLineBreak(mutatedEntries.get(i + 1).getPrefix())));
linebreak() + substringOfAfterFirstLineBreak(mutatedEntries.get(i + 1).getPrefix())));
}
}
}
Expand Down Expand Up @@ -323,9 +325,20 @@ private Yaml.Scalar mergeScalar(Yaml.Scalar y1, Yaml.Scalar y2) {
return !s1.equals(s2) && !acceptTheirs ? y1.withValue(s2) : y1;
}

public String substringOfBeforeFirstLineBreak(String s) {
String[] lines = LINE_BREAK.split(s);
return lines.length > 0 ? lines[0] : "";
}

public String substringOfAfterFirstLineBreak(String s) {
String[] lines = LINE_BREAK.split(s);
return lines.length > 1 ? String.join(linebreak(), Arrays.copyOfRange(lines, 1, lines.length)) : "";
}

private int calculateMultilineIndent(Yaml.Mapping.Entry entry) {
int keyIndent = StringUtils.substringOfAfterLastLineBreak(entry.getPrefix()).length();
int indent = StringUtils.minCommonIndentLevel(StringUtils.substringOfAfterFirstLineBreak(((Yaml.Scalar) entry.getValue()).getValue()));
String[] lines = LINE_BREAK.split(entry.getPrefix());
int keyIndent = (lines.length > 1 ? lines[lines.length - 1] : "").length();
int indent = StringUtils.minCommonIndentLevel(substringOfAfterFirstLineBreak(((Yaml.Scalar) entry.getValue()).getValue()));
return Math.max(indent - keyIndent, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import org.openrewrite.yaml.style.IndentsStyle;
import org.openrewrite.yaml.tree.Yaml;

import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.openrewrite.internal.StringUtils.LINE_BREAK;

public class IndentsVisitor<P> extends YamlIsoVisitor<P> {

private static final Pattern COMMENT_PATTERN = Pattern.compile("^(\\s*)(.*\\R?)", Pattern.MULTILINE);
Expand Down Expand Up @@ -105,9 +108,9 @@ public IndentsVisitor(IndentsStyle style, @Nullable Tree stopAfter) {
}
indentValue += y.getMarkers().findFirst(MultilineScalarChanged.class).get().getIndent();

String oldValue = ((Yaml.Scalar) y).getValue();
String newValue = StringUtils.substringOfBeforeFirstLineBreak(oldValue) +
("\n" + StringUtils.trimIndent(StringUtils.substringOfAfterFirstLineBreak(oldValue)))
String[] lines = LINE_BREAK.split(((Yaml.Scalar) y).getValue());
String newValue = lines[0] +
("\n" + StringUtils.trimIndent(String.join("\n", Arrays.copyOfRange(lines, 1, lines.length))))
.replaceAll("\\R", "\n" + StringUtils.repeat(" ", indentValue));

y = ((Yaml.Scalar) y).withValue(newValue);
Expand Down

0 comments on commit 4af17b9

Please sign in to comment.