[formatter tests] migrated indent detection tests to new test formatting model builder, removed old model

This commit is contained in:
Yaroslav Lepenkin
2016-11-29 12:55:16 +03:00
parent 5c8b0f5967
commit c1a0af9bf2
11 changed files with 36 additions and 493 deletions
@@ -1,14 +0,0 @@
<Block start="0" end="10">
<Block start="0" end="3">
<Indent type="NORMAL"/>
</Block>
<Block start="5" end="6">
<Indent type="NORMAL"/>
<Block start="5" end="6">
<Indent type="SPACE" spaces="1"/>
</Block>
</Block>
<Block start="8" end="10">
<Indent type="SPACE" spaces="1"/>
</Block>
</Block>
@@ -1,32 +0,0 @@
<Block start="0" end="50">
<Indent type="NONE"/>
<Block start="0" end="13">
<Indent type="NONE"/>
</Block>
<Block start="18" end="35">
<Indent type="NONE"/>
<Block start="18" end="25">
<Indent type="NORMAL"/>
<Block start="18" end="25">
<Indent type="NONE"/>
<Block start="18" end="19"/>
<Block start="20" end="25">
<Indent type="NONE"/>
</Block>
</Block>
</Block>
<Block start="30" end="35">
<Indent type="NORMAL"/>
<Block start="30" end="35">
<Indent type="NONE"/>
<Block start="30" end="31"/>
<Block start="32" end="35">
<Indent type="NONE"/>
</Block>
</Block>
</Block>
</Block>
<Block start="36" end="50">
<Indent type="NONE"/>
</Block>
</Block>
@@ -1,7 +0,0 @@
<Block start="0" end="17">
<Block start="0" end="16">
<Block start="0" end="5"/>
<Block start="6" end="16"/>
</Block>
<Block start = "16" end="17"></Block>
</Block>
@@ -16,10 +16,10 @@
package com.intellij.psi.codeStyle.autodetect;
import com.intellij.JavaTestUtil;
import com.intellij.formatting.FormattingModelXmlReader;
import com.intellij.formatting.TestBlock;
import com.intellij.formatting.TestFormattingModel;
import com.intellij.formatting.engine.FormatterEngineTestsKt;
import com.intellij.formatting.engine.TestData;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.testFramework.LightPlatformCodeInsightTestCase;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NotNull;
@@ -37,19 +37,19 @@ public class FormatterBasedLineInfoBuilderTest extends LightPlatformCodeInsightT
}
public void testKotlinComment() throws IOException, JDOMException {
String text = "/**\n" +
" *\n" +
" */";
String text = "[i_cont]([i_norm]/**\n" +
" [i_norm]([i_space_1]*)\n" +
" [i_space_1]*/)";
List<LineIndentInfo> infos = getLineInfos(text);
assertLinesWithNormalIndent(infos, 0);
}
public void testXmlContinuationWithoutFirst() throws IOException, JDOMException {
String text = "<idea-plugin>\n" +
" <name/>\n" +
" <id/>\n" +
"</idea-plugin>";
String text = "[i_none]<idea-plugin>\n" +
" [i_none]([i_norm]([i_none]<[i_none]name/>)\n" +
" [i_norm]([i_none]([]<[i_none]id/>)))\n" +
"[i_none]</idea-plugin>";
List<LineIndentInfo> infos = getLineInfos(text);
assertLinesWithNormalIndent(infos, 4);
@@ -60,12 +60,10 @@ public class FormatterBasedLineInfoBuilderTest extends LightPlatformCodeInsightT
assertEquals(expected, linesWithNormalIndent);
}
private List<LineIndentInfo> getLineInfos(String text) throws IOException, JDOMException {
String file = getTestName(false) + ".xml";
TestFormattingModel model = new TestFormattingModel(text);
Document document = model.getDocument();
TestBlock block = new FormattingModelXmlReader(model).readTestBlock(getTestDataPath(), file);
FormatterBasedLineIndentInfoBuilder builder = new FormatterBasedLineIndentInfoBuilder(document, block, null);
private static List<LineIndentInfo> getLineInfos(String text) throws IOException, JDOMException {
TestData data = FormatterEngineTestsKt.extractFormattingTestData(text);
Document document = EditorFactory.getInstance().createDocument(data.getTextToFormat());
FormatterBasedLineIndentInfoBuilder builder = new FormatterBasedLineIndentInfoBuilder(document, data.getRootBlock(), null);
return builder.build();
}
}
@@ -17,10 +17,10 @@ package com.intellij.psi.codeStyle.autodetect;
import com.intellij.JavaTestUtil;
import com.intellij.formatting.Block;
import com.intellij.formatting.FormattingModelXmlReader;
import com.intellij.formatting.TestBlock;
import com.intellij.formatting.TestFormattingModel;
import com.intellij.formatting.engine.FormatterEngineTestsKt;
import com.intellij.formatting.engine.TestData;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.psi.formatter.common.NewLineBlocksIterator;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NotNull;
@@ -58,11 +58,14 @@ public class NewLineBlocksIteratorTest extends AbstractNewLineBlocksIteratorTest
}
public void testFirstBlockOnNewLineNotStartsIt() throws IOException, JDOMException {
String text = "var x = r'''\n" +
"''';";
Iterator<Block> it = newIteratorFromTestFormattingModel(text);
checkStartOffsets(new int[] {0}, it);
String text = "[]varx []=r'''\n" +
"'''";
TestData data = FormatterEngineTestsKt.extractFormattingTestData(text);
Document document = EditorFactory.getInstance().createDocument(data.getTextToFormat());
NewLineBlocksIterator iterator = new NewLineBlocksIterator(data.getRootBlock(), document);
checkStartOffsets(new int[] {0}, iterator);
}
@@ -73,11 +76,5 @@ public class NewLineBlocksIteratorTest extends AbstractNewLineBlocksIteratorTest
iterator.next();
}
}
protected Iterator<Block> newIteratorFromTestFormattingModel(String text) throws IOException, JDOMException {
TestFormattingModel model = new TestFormattingModel(text);
Document document = model.getDocument();
TestBlock block = new FormattingModelXmlReader(model).readTestBlock(getTestDataPath(), getFileName() + ".xml");
return new NewLineBlocksIterator(block, document);
}
}
@@ -1,3 +0,0 @@
<Block start="0" end="1" incomplete="true">
<Indent type="NORMAL" count="1" spaces="0"/>
</Block>
@@ -1,143 +0,0 @@
package com.intellij.formatting;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.TextRange;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.containers.HashMap;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NonNls;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@NonNls public class FormattingModelXmlReader {
private final Map<String, Alignment> myIdToAlignemt = new HashMap<>();
private final Map<String, Wrap> myIdToWrap = new HashMap<>();
private final FormattingDocumentModel myModel;
public FormattingModelXmlReader(final FormattingDocumentModel model) {
myModel = model;
}
public TestBlock readTestBlock(String dataName) throws IOException, JDOMException {
final File dataFile = new File( PlatformTestUtil.getCommunityPath().replace(File.separatorChar, '/') +
"/platform/platform-tests/testData/newGeneralFormatter", dataName + ".xml");
return readBlock(JDOMUtil.load(dataFile));
}
public TestBlock readTestBlock(String path, String file) throws IOException, JDOMException {
final File dataFile = new File(path, file);
return readBlock(JDOMUtil.load(dataFile));
}
private TestBlock readBlock(final Element rootElement) {
final int startOffset = Integer.parseInt(rootElement.getAttributeValue("start"));
final int endOffset = Integer.parseInt(rootElement.getAttributeValue("end"));
final TextRange textRange = new TextRange(startOffset, endOffset);
final TestBlock result = new TestBlock(textRange);
result.setIsIncomplete(Boolean.valueOf(rootElement.getAttributeValue("incomplete")).booleanValue());
final CharSequence text = myModel.getText(textRange);
if (text != null) {
result.setText(text.toString());
}
final Element indentElement = rootElement.getChild("Indent");
if (indentElement != null) {
result.setIndent(readIndent(indentElement));
}
final Element alignmentElement = rootElement.getChild("Alignment");
if (alignmentElement != null) {
result.setAlignment(readAlignment(alignmentElement));
}
final Element wrapElement = rootElement.getChild("Wrap");
if (wrapElement != null) {
result.setWrap(readWrap(wrapElement));
}
final List children = rootElement.getChildren();
for (final Object aChildren : children) {
Element element = (Element)aChildren;
if (element.getName().equals("Space")) {
result.addSpace(readSpace(element));
}
else if (element.getName().equals("Block")) {
result.addBlock(readBlock(element));
}
}
return result;
}
private Wrap readWrap(final Element wrapElement) {
final String wrapId = wrapElement.getAttributeValue("id");
if (myIdToWrap.containsKey(wrapId)) return myIdToWrap.get(wrapId);
final String type = wrapElement.getAttributeValue("type");
String parentId = wrapElement.getAttributeValue("parent");
boolean wrapFirst = "true".equals(wrapElement.getAttributeValue("ignoreParents"));
if (myIdToWrap.containsKey(parentId)) {
final Wrap wrap = Wrap.createChildWrap(myIdToWrap.get(parentId),readWrapType(type), wrapFirst);
if ("true".equals(wrapElement.getAttributeValue("ignoreParents"))) {
wrap.ignoreParentWraps();
}
myIdToWrap.put(wrapId, wrap);
return wrap;
} else {
final Wrap wrap = Wrap.createWrap(readWrapType(type), wrapFirst);
if ("true".equals(wrapElement.getAttributeValue("ignoreParents"))) {
wrap.ignoreParentWraps();
}
myIdToWrap.put(wrapId, wrap);
return wrap;
}
}
private WrapType readWrapType(final String type) {
if ("ALWAYS".equals(type)) return WrapType.ALWAYS;
if ("NORMAL".equals(type)) return WrapType.NORMAL;
if ("CHOP".equals(type)) return WrapType.CHOP_DOWN_IF_LONG;
return WrapType.NONE;
}
private Spacing readSpace(final Element element) {
return Spacing.createSpacing(
getInt(element.getAttributeValue("minspaces")),
getInt(element.getAttributeValue("maxspaces")),
getInt(element.getAttributeValue("minlinefeeds")),
"true".equals(element.getAttributeValue("keepLineBreaks")), 0);
}
private Alignment readAlignment(final Element alignmentElement) {
final String alignId = alignmentElement.getAttributeValue("id");
if (myIdToAlignemt.containsKey(alignId)) return myIdToAlignemt.get(alignId);
final Alignment alignment = Alignment.createAlignment();
myIdToAlignemt.put(alignId, alignment);
return alignment;
}
private Indent readIndent(final Element indentElement) {
final String type = indentElement.getAttributeValue("type");
if ("LABEL".equals(type)) return Indent.getLabelIndent();
if ("NONE".equals(type)) return Indent.getNoneIndent();
if ("CONTINUATION".equals(type)) return Indent.getContinuationIndent();
if ("SPACE".equals(type)) {
String spaces = indentElement.getAttributeValue("spaces");
return Indent.getSpaceIndent(Integer.valueOf(spaces));
}
return Indent.getNormalIndent();
}
private int getInt(final String count) {
try {
return Integer.parseInt(count);
}
catch (Exception e) {
return 0;
}
}
}
@@ -1,126 +0,0 @@
package com.intellij.formatting;
import com.intellij.openapi.util.TextRange;
import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class TestBlock implements Block{
private TextRange myTextRange;
private final List<Object> myElements = new ArrayList<>();
private Wrap myWrap;
private Indent myIndent;
private Alignment myAlignment;
private String myText = "";
private boolean myIsIncomplete = false;
public TestBlock(final TextRange textRange) {
myTextRange = textRange;
}
@Override
@NotNull
public TextRange getTextRange() {
return myTextRange;
}
@Override
@NotNull
public List<Block> getSubBlocks() {
return getBlockList();
}
public String toString() {
return myText;
}
private List<Block> getBlockList() {
final ArrayList<Block> blocks = new ArrayList<>();
for (Object o : myElements) {
if (o instanceof Block) blocks.add((Block)o);
}
return blocks;
}
@Override
public Wrap getWrap() {
return myWrap;
}
@Override
public Indent getIndent() {
return myIndent;
}
@Override
public Alignment getAlignment() {
return myAlignment;
}
public TestBlock setWrap(final Wrap wrap) {
myWrap = wrap;
return this;
}
public TestBlock setAlignment(final Alignment alignment) {
myAlignment = alignment;
return this;
}
public TestBlock setIndent(final Indent indent) {
myIndent = indent;
return this;
}
@Override
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
if (child1 == null) {
return null;
}
final int index = myElements.indexOf(child2);
if (myElements.get(index - 1) instanceof Spacing) {
return (Spacing)myElements.get(index - 1);
}
return null;
}
public void addBlock(final Block block) {
myElements.add(block);
}
public void setTextRange(final TextRange textRange) {
myTextRange = textRange;
}
public void addSpace(final Spacing spacing) {
myElements.add(spacing);
}
public void setText(final String s) {
myText = s;
}
@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
return new ChildAttributes(getIndent(), null);
}
@Override
public boolean isIncomplete() {
return myIsIncomplete;
}
@Override
public boolean isLeaf() {
return myElements.isEmpty();
}
public void setIsIncomplete(final boolean value) {
myIsIncomplete = value;
}
}
@@ -1,133 +0,0 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.formatting;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.formatter.CompositeWhiteSpaceFormattingStrategy;
import com.intellij.psi.formatter.StaticSymbolWhiteSpaceDefinitionStrategy;
import com.intellij.psi.formatter.WhiteSpaceFormattingStrategy;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class TestFormattingModel implements FormattingModel, FormattingDocumentModel{
private final CompositeWhiteSpaceFormattingStrategy myWhiteSpaceStrategy = new CompositeWhiteSpaceFormattingStrategy(
Arrays.<WhiteSpaceFormattingStrategy>asList(
new StaticSymbolWhiteSpaceDefinitionStrategy(' ', '\t', '\n')
)
);
private final Document myDocument;
private Block myRootBlock;
public TestFormattingModel(String text) {
myDocument = new DocumentImpl(text);
}
public TestFormattingModel(final Document document) {
myDocument = document;
}
public void setRootBlock(final Block rootBlock) {
myRootBlock = rootBlock;
}
@Override
public int getLineNumber(int offset) {
return myDocument.getLineNumber(offset);
}
@Override
public int getLineStartOffset(int line) {
return myDocument.getLineStartOffset(line);
}
@Override
public TextRange replaceWhiteSpace(final TextRange textRange,
final String whiteSpace
) {
if (ApplicationManager.getApplication() != null) {
WriteCommandAction.runWriteCommandAction(null, () -> myDocument.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), whiteSpace));
} else {
myDocument.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), whiteSpace);
}
return new TextRange(textRange.getStartOffset(), textRange.getStartOffset() + whiteSpace.length());
}
@Override
public CharSequence getText(final TextRange textRange) {
return myDocument.getCharsSequence().subSequence(textRange.getStartOffset(), textRange.getEndOffset());
}
@Override
@NotNull
public FormattingDocumentModel getDocumentModel() {
return this;
}
@Override
@NotNull
public Block getRootBlock() {
return myRootBlock;
}
@Override
public void commitChanges() {
}
@Override
public int getTextLength() {
return myDocument.getTextLength();
}
@NotNull
@Override
public Document getDocument() {
return myDocument;
}
@Override
public TextRange shiftIndentInsideRange(ASTNode node, TextRange range, int indent) {
return range;
}
@Override
public boolean containsWhiteSpaceSymbolsOnly(int startOffset, int endOffset) {
return myWhiteSpaceStrategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset;
}
@NotNull
@Override
public CharSequence adjustWhiteSpaceIfNecessary(@NotNull CharSequence whiteSpaceText, int startOffset, int endOffset,
ASTNode nodeAfter, boolean changedViaPsi) {
return whiteSpaceText;
}
//@Override
//public boolean isWhiteSpaceSymbol(char symbol) {
// return containsWhiteSpaceSymbolsOnly(CharBuffer.wrap(new char[] {symbol}), 0, 1);
//}
//private boolean containsWhiteSpaceSymbolsOnly(CharSequence text, int startOffset, int endOffset) {
// return myWhiteSpaceStrategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset;
//}
}
@@ -137,13 +137,19 @@ class AttributesProvider {
}
private fun getIndent(attributes: List<String>): Indent? {
val type = attributes.find { it.startsWith("i_") }?.substring(2)
val type = attributes.find { it.startsWith("i_") }?.substring(2) ?: return null
return when (type) {
"cont" -> Indent.getContinuationIndent()
"none" -> Indent.getNoneIndent()
"norm" -> Indent.getNormalIndent()
"label" -> Indent.getLabelIndent()
else -> null
else -> {
if (type.startsWith("space_")) {
val spacesCount = type.substringAfter("space_").toInt()
return Indent.getSpaceIndent(spacesCount)
}
return null
}
}
}
@@ -25,7 +25,7 @@ enum class Label {
}
private class TestBlockTreeBuilder {
private val stack = mutableListOf(TestBlock.Composite(""))
private val stack = mutableListOf(TestBlock.Composite("i_none"))
private var recentSeenAttributes: String? = null
fun attributes(attributes: String) {
@@ -62,7 +62,7 @@ private class TestBlockTreeBuilder {
fun rootBlock(): TestBlock.Composite {
if (recentSeenAttributes != null) leafBlock("")
return stack.first()
}