Commit 4a44ad54 authored by Valentin Fondaratov's avatar Valentin Fondaratov
Browse files

RUBY-18345: Fix YAML scalars content retrieval when template is injected

Now the "get content" routine is made agnostic to its token-type contents
(cherry picked from commit 97c78baa r=IDEA-CR-12697)

(cherry picked from commit 88002d69)
parent eee9030b
Showing with 22 additions and 8 deletions
+22 -8
package org.jetbrains.yaml.psi.impl;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.yaml.YAMLTokenTypes;
import org.jetbrains.yaml.YAMLUtil;
......@@ -43,23 +45,31 @@ public abstract class YAMLBlockScalarImpl extends YAMLScalarImpl {
int thisLineStart = firstEol.getStartOffset() + 1;
for (ASTNode child = firstEol.getTreeNext(); child != null; child = child.getTreeNext()) {
if (child.getElementType() == getContentType()) {
assert thisLineStart != -1;
result.add(TextRange.create(thisLineStart, child.getTextRange().getEndOffset()).shiftRight(-myStart));
thisLineStart = -1;
}
else if (child.getElementType() == YAMLTokenTypes.INDENT) {
final IElementType childType = child.getElementType();
final TextRange childRange = child.getTextRange();
if (childType == YAMLTokenTypes.INDENT && isEol(child.getTreePrev())) {
thisLineStart = child.getStartOffset() + Math.min(indent, child.getTextLength());
}
if (child.getElementType() == YAMLTokenTypes.EOL) {
else if (childType == YAMLTokenTypes.EOL) {
if (thisLineStart != -1) {
result.add(TextRange.create(thisLineStart, child.getStartOffset()).shiftRight(-myStart));
}
thisLineStart = child.getStartOffset() + 1;
}
else {
if (isEol(child.getTreeNext())) {
if (thisLineStart == -1) {
Logger.getInstance(YAMLBlockScalarImpl.class).warn("thisLineStart == -1: '" + getText() + "'", new Throwable());
continue;
}
result.add(TextRange.create(thisLineStart, childRange.getEndOffset()).shiftRight(-myStart));
thisLineStart = -1;
}
}
}
if (thisLineStart != -1 && thisLineStart != getTextRange().getEndOffset()) {
result.add(TextRange.create(thisLineStart, getTextRange().getEndOffset()).shiftRight(-myStart));
result.add(TextRange.create(thisLineStart, getTextRange().getEndOffset()).shiftRight(-myStart));
}
final int lastNonEmpty = ContainerUtil.lastIndexOf(result, range -> range.getLength() != 0);
......@@ -79,4 +89,8 @@ public abstract class YAMLBlockScalarImpl extends YAMLScalarImpl {
}
return 0;
}
private static boolean isEol(@Nullable ASTNode node) {
return node != null && node.getElementType() == YAMLTokenTypes.EOL;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment