From 97c78baa2fe3317daa91f52f61a548033710673f Mon Sep 17 00:00:00 2001 From: Valentin Fondaratov Date: Tue, 2 Aug 2016 12:56:41 +0300 Subject: [PATCH] RUBY-18345: Fix YAML scalars content retrieval when template is injected Now the "get content" routine is made agnostic to its token-type contents --- .../yaml/psi/impl/YAMLBlockScalarImpl.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/plugins/yaml/src/org/jetbrains/yaml/psi/impl/YAMLBlockScalarImpl.java b/plugins/yaml/src/org/jetbrains/yaml/psi/impl/YAMLBlockScalarImpl.java index 4ef76e6a755f..5b13b551e3e8 100644 --- a/plugins/yaml/src/org/jetbrains/yaml/psi/impl/YAMLBlockScalarImpl.java +++ b/plugins/yaml/src/org/jetbrains/yaml/psi/impl/YAMLBlockScalarImpl.java @@ -1,10 +1,12 @@ 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; + } }