Refactoring: replace strategy API a little

This commit is contained in:
Valentin Fondaratov
2016-07-19 23:32:10 +03:00
parent 36a95cac10
commit 95afd568c8
5 changed files with 30 additions and 22 deletions
@@ -44,14 +44,18 @@ public class YAMLPlainTextImpl extends YAMLScalarImpl implements YAMLScalar {
@NotNull
@Override
protected String getRangesJoiner(@NotNull CharSequence leftString, @NotNull CharSequence rightString) {
if (leftString.equals("\n") || rightString.equals("\n")) {
protected String getRangesJoiner(@NotNull CharSequence text, @NotNull List<TextRange> contentRanges, int indexBefore) {
if (isNewline(text, contentRanges.get(indexBefore)) || isNewline(text, contentRanges.get(indexBefore + 1))) {
return "";
}
else {
return " ";
}
}
private static boolean isNewline(@NotNull CharSequence text, @NotNull TextRange range) {
return range.getLength() == 1 && text.charAt(range.getStartOffset()) == '\n';
}
@Override
protected List<Pair<TextRange, String>> getEncodeReplacements(@NotNull CharSequence input) throws IllegalArgumentException {
@@ -65,11 +65,12 @@ public class YAMLQuotedTextImpl extends YAMLScalarImpl implements YAMLQuotedText
@NotNull
@Override
protected String getRangesJoiner(@NotNull CharSequence leftString, @NotNull CharSequence rightString) {
if (leftString.length() == 0 || !isSingleQuote() && leftString.charAt(leftString.length() - 1) == '\\') {
protected String getRangesJoiner(@NotNull CharSequence text, @NotNull List<TextRange> contentRanges, int indexBefore) {
final TextRange leftRange = contentRanges.get(indexBefore);
if (leftRange.isEmpty() || !isSingleQuote() && text.charAt(leftRange.getEndOffset() - 1) == '\\') {
return "\n";
}
else if (rightString.length() == 0) {
else if (contentRanges.get(indexBefore + 1).isEmpty()) {
return "";
}
else {
@@ -27,7 +27,7 @@ public abstract class YAMLScalarImpl extends YAMLValueImpl implements YAMLScalar
public abstract List<TextRange> getContentRanges();
@NotNull
protected abstract String getRangesJoiner(@NotNull CharSequence leftString, @NotNull CharSequence rightString);
protected abstract String getRangesJoiner(@NotNull CharSequence text, @NotNull List<TextRange> contentRanges, int indexBefore);
protected List<Pair<TextRange, String>> getDecodeReplacements(@NotNull CharSequence input) {
return Collections.emptyList();
@@ -44,18 +44,15 @@ public abstract class YAMLScalarImpl extends YAMLValueImpl implements YAMLScalar
final List<TextRange> contentRanges = getContentRanges();
final StringBuilder builder = new StringBuilder();
CharSequence nextString = null;
for (int i = 0; i < contentRanges.size(); i++) {
final TextRange range = contentRanges.get(i);
final CharSequence curString = i == 0 ? range.subSequence(text) : nextString;
assert curString != null;
final CharSequence curString = range.subSequence(text);
builder.append(curString);
if (i + 1 != contentRanges.size()) {
nextString = contentRanges.get(i + 1).subSequence(text);
builder.append(getRangesJoiner(curString, nextString));
builder.append(getRangesJoiner(text, contentRanges, i));
}
}
return processReplacements(builder, getDecodeReplacements(builder));
@@ -125,17 +122,14 @@ public abstract class YAMLScalarImpl extends YAMLValueImpl implements YAMLScalar
final List<TextRange> contentRanges = myHost.getContentRanges();
int currentOffsetInDecoded = 0;
String nextString = null;
for (int i = 0; i < contentRanges.size(); i++) {
final TextRange range = contentRanges.get(i);
String curString = i == 0 ? range.subSequence(text).toString() : nextString;
assert curString != null;
String curString = range.subSequence(text).toString();
if (i + 1 != contentRanges.size()) {
nextString = contentRanges.get(i + 1).subSequence(text).toString();
final String joiner = myHost.getRangesJoiner(curString, nextString);
final String joiner = myHost.getRangesJoiner(text, contentRanges, i);
curString += joiner;
}
@@ -36,7 +36,7 @@ public class YAMLScalarListImpl extends YAMLBlockScalarImpl implements YAMLScala
@NotNull
@Override
protected String getRangesJoiner(@NotNull CharSequence leftString, @NotNull CharSequence rightString) {
protected String getRangesJoiner(@NotNull CharSequence text, @NotNull List<TextRange> contentRanges, int indexBefore) {
return "\n";
}
@@ -30,19 +30,28 @@ public class YAMLScalarTextImpl extends YAMLBlockScalarImpl implements YAMLScala
@NotNull
@Override
protected String getRangesJoiner(@NotNull CharSequence leftString, @NotNull CharSequence rightString) {
if (StringUtil.isEmptyOrSpaces(leftString)) {
protected String getRangesJoiner(@NotNull CharSequence text, @NotNull List<TextRange> contentRanges, int indexBefore) {
final TextRange leftRange = contentRanges.get(indexBefore);
final TextRange rightRange = contentRanges.get(indexBefore + 1);
if (leftRange.isEmpty()) {
return "\n";
}
if (StringUtil.startsWithChar(leftString, ' ') || StringUtil.startsWithChar(leftString, '\t')
|| StringUtil.startsWithChar(rightString, ' ') || StringUtil.startsWithChar(rightString, '\t')) {
if (startsWithWhitespace(text, leftRange) || startsWithWhitespace(text, rightRange)) {
return "\n";
}
if (StringUtil.isEmptyOrSpaces(rightString)) {
if (rightRange.isEmpty()) {
return "";
}
return " ";
}
private static boolean startsWithWhitespace(@NotNull CharSequence text, @NotNull TextRange range) {
if (range.isEmpty()) {
return false;
}
final char c = text.charAt(range.getStartOffset());
return c == ' ' || c == '\t';
}
@Override
protected List<Pair<TextRange, String>> getEncodeReplacements(@NotNull CharSequence input) throws IllegalArgumentException {