From 5ce2126533fd321c2001e123989bbf6e3f640db0 Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Tue, 16 Jun 2015 13:32:12 +0300 Subject: [PATCH] fixed EA-69515, do not reverse block's subBlocks collection, added few test for NewLineBlocksIterator --- .../psi/autodetect/doNotReverseBlocks.xml | 7 ++ .../simpleFileNewBlockOffsetDetection.java | 24 +++++++ .../AbstractNewLineBlocksIteratorTest.java | 68 +++++++++++++++++++ .../autodetect/NewLineBlocksIteratorTest.java | 50 ++++++++++++++ .../common/NewLineBlocksIterator.java | 11 ++- 5 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 java/java-tests/testData/psi/autodetect/doNotReverseBlocks.xml create mode 100644 java/java-tests/testData/psi/autodetect/simpleFileNewBlockOffsetDetection.java create mode 100644 java/java-tests/testSrc/com/intellij/psi/autodetect/AbstractNewLineBlocksIteratorTest.java create mode 100644 java/java-tests/testSrc/com/intellij/psi/autodetect/NewLineBlocksIteratorTest.java diff --git a/java/java-tests/testData/psi/autodetect/doNotReverseBlocks.xml b/java/java-tests/testData/psi/autodetect/doNotReverseBlocks.xml new file mode 100644 index 000000000000..7d1f6889ce54 --- /dev/null +++ b/java/java-tests/testData/psi/autodetect/doNotReverseBlocks.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/java/java-tests/testData/psi/autodetect/simpleFileNewBlockOffsetDetection.java b/java/java-tests/testData/psi/autodetect/simpleFileNewBlockOffsetDetection.java new file mode 100644 index 000000000000..766282e18817 --- /dev/null +++ b/java/java-tests/testData/psi/autodetect/simpleFileNewBlockOffsetDetection.java @@ -0,0 +1,24 @@ + + + +import java.lang.Override; +import java.lang.Runnable; + + + +public class T { + + + + public void test() { + run(new Runnable() { + @Override + public void run() { + System.out.println("AAAA"); + } + }); + } + + + +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/psi/autodetect/AbstractNewLineBlocksIteratorTest.java b/java/java-tests/testSrc/com/intellij/psi/autodetect/AbstractNewLineBlocksIteratorTest.java new file mode 100644 index 000000000000..2b6350e05e1a --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/psi/autodetect/AbstractNewLineBlocksIteratorTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2000-2015 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.psi.autodetect; + +import com.intellij.formatting.Block; +import com.intellij.formatting.FormattingModel; +import com.intellij.formatting.FormattingModelBuilder; +import com.intellij.lang.LanguageFormatting; +import com.intellij.openapi.editor.Document; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.formatter.common.NewLineBlocksIterator; +import com.intellij.testFramework.LightPlatformCodeInsightTestCase; +import org.jetbrains.annotations.NotNull; +import org.junit.Assert; + +import java.util.Iterator; + +public abstract class AbstractNewLineBlocksIteratorTest extends LightPlatformCodeInsightTestCase { + + @NotNull + protected String getFileName() { + return getTestName(true); + } + + protected void checkNewLineBlocksStartOffsets(int[] newLineStartOffsets) { + Iterator iterator = createNewLineBlocksIterator(); + + int i = 0; + while (iterator.hasNext()) { + Assert.assertTrue("Detected unspecified new line block start offset ", i < newLineStartOffsets.length); + Block next = iterator.next(); + Assert.assertEquals("Block start offset do not match ", newLineStartOffsets[i++], next.getTextRange().getStartOffset()); + } + + Assert.assertEquals("Not detected new line block start offset ", i, newLineStartOffsets.length); + } + + @NotNull + protected static Iterator createNewLineBlocksIterator() { + FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(myFile); + Assert.assertNotNull(builder); + + CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(getProject()).getCurrentSettings(); + FormattingModel model = builder.createModel(myFile, settings); + + Block root = model.getRootBlock(); + Document document = PsiDocumentManager.getInstance(getProject()).getDocument(myFile); + Assert.assertNotNull(document); + + return new NewLineBlocksIterator(root, document); + } + +} diff --git a/java/java-tests/testSrc/com/intellij/psi/autodetect/NewLineBlocksIteratorTest.java b/java/java-tests/testSrc/com/intellij/psi/autodetect/NewLineBlocksIteratorTest.java new file mode 100644 index 000000000000..25c4416ad73a --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/psi/autodetect/NewLineBlocksIteratorTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2015 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.psi.autodetect; + +import com.intellij.JavaTestUtil; +import org.jetbrains.annotations.NotNull; + +public class NewLineBlocksIteratorTest extends AbstractNewLineBlocksIteratorTest { + + @NotNull + @Override + protected String getTestDataPath() { + return JavaTestUtil.getJavaTestDataPath() + + "/psi/autodetect/"; + } + + public void testSimpleFileNewBlockOffsetDetection() { + configureByFile(getFileName() + ".java"); + + int[] newLineBlocksStartOffsets = new int[] { + 0, 3, 30, 60, 82, 107, 136, 154, 184, 220, 226, 232, 237 + }; + + checkNewLineBlocksStartOffsets(newLineBlocksStartOffsets); + } + + public void testDoNotReverseBlocks() { + configureByFile(getFileName() + ".xml"); + + int[] newLineBlocksStartOffsets = new int[] { + 0, 39, 48, 118, 177, 235, 243 + }; + + checkNewLineBlocksStartOffsets(newLineBlocksStartOffsets); + } + +} diff --git a/platform/lang-impl/src/com/intellij/psi/formatter/common/NewLineBlocksIterator.java b/platform/lang-impl/src/com/intellij/psi/formatter/common/NewLineBlocksIterator.java index 9ec55edcb104..9ca8ddcf03c7 100644 --- a/platform/lang-impl/src/com/intellij/psi/formatter/common/NewLineBlocksIterator.java +++ b/platform/lang-impl/src/com/intellij/psi/formatter/common/NewLineBlocksIterator.java @@ -19,10 +19,7 @@ import com.intellij.formatting.Block; import com.intellij.openapi.editor.Document; import com.intellij.openapi.util.TextRange; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Stack; +import java.util.*; public class NewLineBlocksIterator implements Iterator { @@ -97,9 +94,9 @@ public class NewLineBlocksIterator implements Iterator { } List blocks = current.getSubBlocks(); - Collections.reverse(blocks); - for (Block block : blocks) { - myStack.push(block); + ListIterator iterator = blocks.listIterator(blocks.size()); + while (iterator.hasPrevious()) { + myStack.push(iterator.previous()); } }