diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/AbstractJavaFormatterTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/AbstractJavaFormatterTest.java index d4c3e9980fbd..08570072682c 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/AbstractJavaFormatterTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/AbstractJavaFormatterTest.java @@ -32,9 +32,9 @@ import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.*; +import com.intellij.psi.formatter.java.FormatterTestUtils.Action; import com.intellij.testFramework.LightIdeaTestCase; import com.intellij.util.IncorrectOperationException; -import com.intellij.util.containers.ContainerUtil; import com.intellij.util.text.LineReader; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -42,9 +42,10 @@ import org.jetbrains.annotations.Nullable; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; -import java.util.EnumMap; import java.util.List; -import java.util.Map; + +import static com.intellij.psi.formatter.java.FormatterTestUtils.ACTIONS; +import static com.intellij.psi.formatter.java.FormatterTestUtils.Action.*; /** * Base class for java formatter tests that holds utility methods. @@ -82,46 +83,10 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { return result.toString(); } - protected enum Action {REFORMAT, INDENT, REFORMAT_WITH_CONTEXT, REFORMAT_WITH_INSERTED_LINE_CONTEXT} - public static JavaCodeStyleSettings getJavaSettings() { return getSettings().getRootSettings().getCustomSettings(JavaCodeStyleSettings.class); } - - private interface TestFormatAction { - void run(PsiFile psiFile, int startOffset, int endOffset); - } - - private static final Map ACTIONS = new EnumMap<>(Action.class); - static { - ACTIONS.put(Action.REFORMAT, new TestFormatAction() { - @Override - public void run(PsiFile psiFile, int startOffset, int endOffset) { - CodeStyleManager.getInstance(getProject()).reformatText(psiFile, startOffset, endOffset); - } - }); - ACTIONS.put(Action.INDENT, new TestFormatAction() { - @Override - public void run(PsiFile psiFile, int startOffset, int endOffset) { - CodeStyleManager.getInstance(getProject()).adjustLineIndent(psiFile, startOffset); - } - }); - ACTIONS.put(Action.REFORMAT_WITH_CONTEXT, new TestFormatAction() { - @Override - public void run(PsiFile psiFile, int startOffset, int endOffset) { - List ranges = ContainerUtil.newArrayList(new TextRange(startOffset, endOffset)); - CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, ranges); - } - }); - ACTIONS.put(Action.REFORMAT_WITH_INSERTED_LINE_CONTEXT, new TestFormatAction() { - @Override - public void run(PsiFile psiFile, int startOffset, int endOffset) { - List ranges = ContainerUtil.newArrayList(new TextRange(startOffset, endOffset)); - CodeStyleManager.getInstance(getProject()).reformatTextWithContext(psiFile, new ChangedRangesInfo(ranges, ranges)); - } - }); - } - + private static final String BASE_PATH = JavaTestUtil.getJavaTestDataPath() + "/psi/formatter/java"; public TextRange myTextRange; @@ -147,7 +112,7 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { } public void doTest(@NotNull String fileNameBefore, @NotNull String fileNameAfter) { - doTextTest(Action.REFORMAT, loadFile(fileNameBefore), loadFile(fileNameAfter)); + doTextTest(REFORMAT, loadFile(fileNameBefore), loadFile(fileNameAfter)); } public void doTestWithDetectableIndentOptions(@NotNull String text, @NotNull String textAfter) { @@ -163,7 +128,7 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { } public void doTextTest(@NotNull String text, @NotNull String textAfter) throws IncorrectOperationException { - doTextTest(Action.REFORMAT, text, textAfter); + doTextTest(REFORMAT, text, textAfter); } public void doTextTest(@NotNull Action action, @NotNull String text, @NotNull String textAfter) throws IncorrectOperationException { @@ -185,9 +150,9 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { final PsiFile file = createFile("A.java", ""); final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject()); final Document document = manager.getDocument(file); - String afterFirst = replaceAndProcessDocument(Action.REFORMAT, before[0], file, document); + String afterFirst = replaceAndProcessDocument(REFORMAT, before[0], file, document); for (String nextBefore: before) { - assertEquals(afterFirst, replaceAndProcessDocument(Action.REFORMAT, nextBefore, file, document)); + assertEquals(afterFirst, replaceAndProcessDocument(REFORMAT, nextBefore, file, document)); } } @@ -220,14 +185,14 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { catch (IncorrectOperationException e) { assertTrue(e.getLocalizedMessage(), false); } - }), action == Action.REFORMAT ? ReformatCodeProcessor.COMMAND_NAME : "", ""); + }), action == REFORMAT ? ReformatCodeProcessor.COMMAND_NAME : "", ""); return document.getText(); } public void doMethodTest(@NotNull String before, @NotNull String after) { doTextTest( - Action.REFORMAT, + REFORMAT, "class Foo{\n" + " void foo() {\n" + before + '\n' + " }\n" + "}", "class Foo {\n" + " void foo() {\n" + shiftIndentInside(after, 8, false) + '\n' + " }\n" + "}" ); @@ -235,7 +200,7 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase { public void doClassTest(@NotNull String before, @NotNull String after) { doTextTest( - Action.REFORMAT, + REFORMAT, "class Foo{\n" + before + '\n' + "}", "class Foo {\n" + shiftIndentInside(after, 4, false) + '\n' + "}" ); diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatWithContextTest.kt b/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatWithContextTest.kt index 9f476c2d40ad..827c8de6cf0d 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatWithContextTest.kt +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatWithContextTest.kt @@ -16,13 +16,12 @@ package com.intellij.psi.formatter.java import com.intellij.openapi.util.TextRange -import com.intellij.openapi.util.registry.Registry class FormatWithContextTest : AbstractJavaFormatterTest() { fun check(before: String, after: String) { - doTextTest(Action.REFORMAT_WITH_INSERTED_LINE_CONTEXT, before, after) + doTextTest(FormatterTestUtils.Action.REFORMAT_WITH_INSERTED_LINE_CONTEXT, before, after) } fun `test if block`() { diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatterTestUtils.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatterTestUtils.java new file mode 100644 index 000000000000..880847d984be --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/FormatterTestUtils.java @@ -0,0 +1,143 @@ +/* + * Copyright 2000-2017 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.formatter.java; + +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.fileTypes.FileType; +import com.intellij.openapi.fileTypes.FileTypeManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiFileFactory; +import com.intellij.psi.codeStyle.ChangedRangesInfo; +import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.util.containers.ContainerUtil; +import org.junit.Assert; +import org.jetbrains.annotations.NotNull; + +import java.util.EnumMap; +import java.util.List; +import java.util.Map; + +public class FormatterTestUtils { + + + public interface TestFormatAction { + void run(PsiFile psiFile, int startOffset, int endOffset); + } + + public enum Action { + REFORMAT, + INDENT, + REFORMAT_WITH_CONTEXT, + REFORMAT_WITH_INSERTED_LINE_CONTEXT + } + + public static final Map ACTIONS = new EnumMap<>(Action.class); + + public static class FormatData { + public int startOffset; + public int endOffset; + public String text; + + public FormatData(String text, int startOffset, int endOffset) { + this.text = text; + this.startOffset = startOffset; + this.endOffset = endOffset; + } + } + + public static void doTest(@NotNull Project project, + @NotNull String ext, + @NotNull String before, + @NotNull String after, + @NotNull Action action) { + String fileName = "FTU." + ext; + FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(fileName); + + FormatData data = extractFormatData(before); + PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(fileName, fileType, data.text); + + PsiDocumentManager manager = PsiDocumentManager.getInstance(project); + Document document = manager.getDocument(file); + if (document == null) { + throw new IllegalStateException("Document is null"); + } + + TestFormatAction formatAction = ACTIONS.get(action); + if (formatAction == null) { + throw new IllegalStateException("Format action is null"); + } + + WriteCommandAction.runWriteCommandAction(project, () -> formatAction.run(file, data.startOffset, data.endOffset)); + Assert.assertEquals(after, document.getText()); + } + + private static FormatData extractFormatData(@NotNull String before) { + final String SELECTION_START = ""; + final String SELECTION_END = ""; + + int startOffset = before.indexOf(SELECTION_START); + if (startOffset > 0) { + int endOffset = before.indexOf(SELECTION_END) - SELECTION_START.length(); + String text = before + .replace(SELECTION_START, "") + .replace(SELECTION_END, ""); + return new FormatData(text, startOffset, endOffset); + } + + return new FormatData(before, 0, before.length()); + } + + static { + ACTIONS.put(Action.REFORMAT, new TestFormatAction() { + @Override + public void run(PsiFile psiFile, int startOffset, int endOffset) { + Project project = psiFile.getProject(); + CodeStyleManager.getInstance(project).reformatText(psiFile, startOffset, endOffset); + } + }); + + ACTIONS.put(Action.INDENT, new TestFormatAction() { + @Override + public void run(PsiFile psiFile, int startOffset, int endOffset) { + Project project = psiFile.getProject(); + CodeStyleManager.getInstance(project).adjustLineIndent(psiFile, startOffset); + } + }); + + ACTIONS.put(Action.REFORMAT_WITH_CONTEXT, new TestFormatAction() { + @Override + public void run(PsiFile psiFile, int startOffset, int endOffset) { + List ranges = ContainerUtil.newArrayList(new TextRange(startOffset, endOffset)); + Project project = psiFile.getProject(); + CodeStyleManager.getInstance(project).reformatTextWithContext(psiFile, ranges); + } + }); + + ACTIONS.put(Action.REFORMAT_WITH_INSERTED_LINE_CONTEXT, new TestFormatAction() { + @Override + public void run(PsiFile psiFile, int startOffset, int endOffset) { + List ranges = ContainerUtil.newArrayList(new TextRange(startOffset, endOffset)); + Project project = psiFile.getProject(); + CodeStyleManager.getInstance(project).reformatTextWithContext(psiFile, new ChangedRangesInfo(ranges, ranges)); + } + }); + } + +} diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterAlignmentTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterAlignmentTest.java index 78800153a4e1..86665e7cc07b 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterAlignmentTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaFormatterAlignmentTest.java @@ -20,6 +20,8 @@ import com.intellij.openapi.util.TextRange; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.util.IncorrectOperationException; +import static com.intellij.psi.formatter.java.FormatterTestUtils.Action.REFORMAT_WITH_CONTEXT; + /** * Is intended to hold specific java formatting tests for alignment settings ( * Project Settings - Code Style - Alignment and Braces). @@ -634,7 +636,7 @@ public class JavaFormatterAlignmentTest extends AbstractJavaFormatterTest { myLineRange = new TextRange(2, 2); getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true; doTextTest( - Action.REFORMAT_WITH_CONTEXT, + REFORMAT_WITH_CONTEXT, "public class Test {\n" + " \n" + " public void fooooo(String foo,\n" + @@ -661,7 +663,7 @@ public class JavaFormatterAlignmentTest extends AbstractJavaFormatterTest { myLineRange = new TextRange(2, 2); getSettings().ALIGN_GROUP_FIELD_DECLARATIONS = true; doTextTest( - Action.REFORMAT_WITH_CONTEXT, + REFORMAT_WITH_CONTEXT, "public class Test {\n" + " public int i = 1;\n" + " public String iiiiiiiiii = 2;\n" + @@ -677,7 +679,7 @@ public class JavaFormatterAlignmentTest extends AbstractJavaFormatterTest { myLineRange = new TextRange(3, 3); getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true; doTextTest( - Action.REFORMAT_WITH_CONTEXT, + REFORMAT_WITH_CONTEXT, "public class Test {\n" + " public void test() {\n" + " int s = 2;\n" + diff --git a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaIndenterTest.java b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaIndenterTest.java index 625dce1c0c43..a4c49c027792 100644 --- a/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaIndenterTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/formatter/java/JavaIndenterTest.java @@ -76,7 +76,7 @@ public class JavaIndenterTest extends AbstractJavaFormatterTest { @Override public void doTextTest(@NotNull String text, @NotNull String textAfter) throws IncorrectOperationException { - doTextTest(Action.INDENT, adjustTextIfNecessary(text), textAfter); + doTextTest(FormatterTestUtils.Action.INDENT, adjustTextIfNecessary(text), textAfter); } private String adjustTextIfNecessary(String text) {