diff --git a/java/java-tests/testData/psi/autodetect/KotlinComment.xml b/java/java-tests/testData/psi/autodetect/KotlinComment.xml deleted file mode 100644 index cb4ea9b2607e..000000000000 --- a/java/java-tests/testData/psi/autodetect/KotlinComment.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/java/java-tests/testData/psi/autodetect/XmlContinuationWithoutFirst.xml b/java/java-tests/testData/psi/autodetect/XmlContinuationWithoutFirst.xml deleted file mode 100644 index 59deea200bf8..000000000000 --- a/java/java-tests/testData/psi/autodetect/XmlContinuationWithoutFirst.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/java/java-tests/testData/psi/autodetect/firstBlockOnNewLineNotStartsIt.xml b/java/java-tests/testData/psi/autodetect/firstBlockOnNewLineNotStartsIt.xml deleted file mode 100644 index 0beebd746e23..000000000000 --- a/java/java-tests/testData/psi/autodetect/firstBlockOnNewLineNotStartsIt.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/FormatterBasedLineInfoBuilderTest.java b/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/FormatterBasedLineInfoBuilderTest.java index 8ed5cc75f174..0af0d0304f90 100644 --- a/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/FormatterBasedLineInfoBuilderTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/FormatterBasedLineInfoBuilderTest.java @@ -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 infos = getLineInfos(text); assertLinesWithNormalIndent(infos, 0); } public void testXmlContinuationWithoutFirst() throws IOException, JDOMException { - String text = "\n" + - " \n" + - " \n" + - ""; + String text = "[i_none]\n" + + " [i_none]([i_norm]([i_none]<[i_none]name/>)\n" + + " [i_norm]([i_none]([]<[i_none]id/>)))\n" + + "[i_none]"; List infos = getLineInfos(text); assertLinesWithNormalIndent(infos, 4); @@ -60,12 +60,10 @@ public class FormatterBasedLineInfoBuilderTest extends LightPlatformCodeInsightT assertEquals(expected, linesWithNormalIndent); } - private List 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 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(); } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/NewLineBlocksIteratorTest.java b/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/NewLineBlocksIteratorTest.java index 8f3edd58d57a..3e4056e9b596 100644 --- a/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/NewLineBlocksIteratorTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/codeStyle/autodetect/NewLineBlocksIteratorTest.java @@ -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 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 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); - } + } diff --git a/platform/platform-tests/testData/newGeneralFormatter/lineIndent.xml b/platform/platform-tests/testData/newGeneralFormatter/lineIndent.xml deleted file mode 100644 index 83a57843bea6..000000000000 --- a/platform/platform-tests/testData/newGeneralFormatter/lineIndent.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/platform/platform-tests/testSrc/com/intellij/formatting/FormattingModelXmlReader.java b/platform/platform-tests/testSrc/com/intellij/formatting/FormattingModelXmlReader.java deleted file mode 100644 index b32b4d5910d8..000000000000 --- a/platform/platform-tests/testSrc/com/intellij/formatting/FormattingModelXmlReader.java +++ /dev/null @@ -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 myIdToAlignemt = new HashMap<>(); - private final Map 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; - } - } -} diff --git a/platform/platform-tests/testSrc/com/intellij/formatting/TestBlock.java b/platform/platform-tests/testSrc/com/intellij/formatting/TestBlock.java deleted file mode 100644 index d9fe410c3f2c..000000000000 --- a/platform/platform-tests/testSrc/com/intellij/formatting/TestBlock.java +++ /dev/null @@ -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 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 getSubBlocks() { - return getBlockList(); - } - - public String toString() { - return myText; - } - - private List getBlockList() { - final ArrayList 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; - } -} diff --git a/platform/platform-tests/testSrc/com/intellij/formatting/TestFormattingModel.java b/platform/platform-tests/testSrc/com/intellij/formatting/TestFormattingModel.java deleted file mode 100644 index 69130f7c9fb5..000000000000 --- a/platform/platform-tests/testSrc/com/intellij/formatting/TestFormattingModel.java +++ /dev/null @@ -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.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; - //} -} diff --git a/platform/platform-tests/testSrc/com/intellij/formatting/TestModelBuilder.kt b/platform/platform-tests/testSrc/com/intellij/formatting/TestModelBuilder.kt index 437b98f084ae..0a6eca9c4914 100644 --- a/platform/platform-tests/testSrc/com/intellij/formatting/TestModelBuilder.kt +++ b/platform/platform-tests/testSrc/com/intellij/formatting/TestModelBuilder.kt @@ -137,13 +137,19 @@ class AttributesProvider { } private fun getIndent(attributes: List): 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 + } } } diff --git a/platform/platform-tests/testSrc/com/intellij/formatting/engine/testModel/TestModelParser.kt b/platform/platform-tests/testSrc/com/intellij/formatting/engine/testModel/TestModelParser.kt index 3d2d7fec0d17..87fead404854 100644 --- a/platform/platform-tests/testSrc/com/intellij/formatting/engine/testModel/TestModelParser.kt +++ b/platform/platform-tests/testSrc/com/intellij/formatting/engine/testModel/TestModelParser.kt @@ -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() }