192: Fix FormatSettingsUtil for 192, can't access getProject any more
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.formatter;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.impl.DocumentImpl;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.testFramework.LightIdeaTestCase;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.idea.KotlinLanguage;
|
||||
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase;
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.SettingsConfigurator;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
|
||||
// Based on from com.intellij.psi.formatter.java.AbstractJavaFormatterTest
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public abstract class AbstractFormatterTest extends LightIdeaTestCase {
|
||||
|
||||
protected enum Action {REFORMAT, INDENT}
|
||||
|
||||
private interface TestFormatAction {
|
||||
void run(PsiFile psiFile, int startOffset, int endOffset);
|
||||
}
|
||||
|
||||
private static final Map<Action, TestFormatAction> ACTIONS = new EnumMap<Action, TestFormatAction>(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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static final String BASE_PATH =
|
||||
new File(PluginTestCaseBase.getTestDataPathBase(), "/formatter/").getAbsolutePath();
|
||||
|
||||
public TextRange myTextRange;
|
||||
public TextRange myLineRange;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.HIGHEST);
|
||||
}
|
||||
|
||||
public void doTextTest(@NonNls String text, File fileAfter, String extension) throws IncorrectOperationException {
|
||||
doTextTest(Action.REFORMAT, text, fileAfter, extension);
|
||||
}
|
||||
|
||||
public void doTextTest(final Action action, final String text, File fileAfter, String extension) throws IncorrectOperationException {
|
||||
final PsiFile file = createFile("A" + extension, text);
|
||||
|
||||
if (myLineRange != null) {
|
||||
DocumentImpl document = new DocumentImpl(text);
|
||||
myTextRange =
|
||||
new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()), document.getLineEndOffset(myLineRange.getEndOffset()));
|
||||
}
|
||||
|
||||
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
|
||||
final Document document = manager.getDocument(file);
|
||||
|
||||
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
document.replaceString(0, document.getTextLength(), text);
|
||||
manager.commitDocument(document);
|
||||
try {
|
||||
TextRange rangeToUse = myTextRange;
|
||||
if (rangeToUse == null) {
|
||||
rangeToUse = file.getTextRange();
|
||||
}
|
||||
ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
assertTrue(e.getLocalizedMessage(), false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}, "", "");
|
||||
|
||||
|
||||
if (document == null) {
|
||||
fail("Don't expect the document to be null");
|
||||
return;
|
||||
}
|
||||
KotlinTestUtils.assertEqualsToFile(fileAfter, document.getText());
|
||||
manager.commitDocument(document);
|
||||
KotlinTestUtils.assertEqualsToFile(fileAfter, file.getText());
|
||||
}
|
||||
|
||||
public void doTest(@NotNull String expectedFileNameWithExtension) throws Exception {
|
||||
doTest(expectedFileNameWithExtension, false);
|
||||
}
|
||||
|
||||
public void doTestInverted(@NotNull String expectedFileNameWithExtension) throws Exception {
|
||||
doTest(expectedFileNameWithExtension, true);
|
||||
}
|
||||
|
||||
public void doTest(@NotNull String expectedFileNameWithExtension, boolean inverted) throws Exception {
|
||||
String testFileName = expectedFileNameWithExtension.substring(0, expectedFileNameWithExtension.indexOf("."));
|
||||
String testFileExtension = expectedFileNameWithExtension.substring(expectedFileNameWithExtension.lastIndexOf("."));
|
||||
String originalFileText = FileUtil.loadFile(new File(testFileName + testFileExtension), true);
|
||||
|
||||
CodeStyleSettings codeStyleSettings = FormatSettingsUtil.getSettings(getProject());
|
||||
try {
|
||||
Integer rightMargin = InTextDirectivesUtils.getPrefixedInt(originalFileText, "// RIGHT_MARGIN: ");
|
||||
if (rightMargin != null) {
|
||||
codeStyleSettings.setRightMargin(KotlinLanguage.INSTANCE, rightMargin);
|
||||
}
|
||||
|
||||
SettingsConfigurator configurator = FormatSettingsUtil.createConfigurator(originalFileText, codeStyleSettings);
|
||||
if (!inverted) {
|
||||
configurator.configureSettings();
|
||||
}
|
||||
else {
|
||||
configurator.configureInvertedSettings();
|
||||
}
|
||||
|
||||
doTextTest(originalFileText, new File(expectedFileNameWithExtension), testFileExtension);
|
||||
} finally {
|
||||
codeStyleSettings.clearCodeStyleSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.formatter;
|
||||
|
||||
import com.intellij.application.options.CodeStyle;
|
||||
import com.intellij.openapi.editor.CaretModel;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.testFramework.EditorTestUtil;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.SettingsConfigurator;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public abstract class AbstractTypingIndentationTestBase extends LightCodeInsightTestCase {
|
||||
public void doNewlineTest(String afterFilePath) throws Exception {
|
||||
doNewlineTest(afterFilePath, false);
|
||||
}
|
||||
|
||||
public void doNewlineTestWithInvert(String afterInvFilePath) throws Exception {
|
||||
doNewlineTest(afterInvFilePath, true);
|
||||
}
|
||||
|
||||
public void doNewlineTest(String afterFilePath, boolean inverted) throws Exception {
|
||||
String testFileName = afterFilePath.substring(0, afterFilePath.indexOf("."));
|
||||
String testFileExtension = afterFilePath.substring(afterFilePath.lastIndexOf("."));
|
||||
|
||||
String originFilePath = testFileName + testFileExtension;
|
||||
String originalFileText = FileUtil.loadFile(new File(originFilePath), true);
|
||||
|
||||
try {
|
||||
SettingsConfigurator configurator = FormatSettingsUtil.createConfigurator(originalFileText, getProject());
|
||||
if (!inverted) {
|
||||
configurator.configureSettings();
|
||||
}
|
||||
else {
|
||||
configurator.configureInvertedSettings();
|
||||
}
|
||||
|
||||
doNewlineTest(originFilePath, afterFilePath);
|
||||
}
|
||||
finally {
|
||||
getSettings().clearCodeStyleSettings();
|
||||
}
|
||||
}
|
||||
|
||||
private void doNewlineTest(String beforeFilePath, String afterFilePath) {
|
||||
configureByFile(beforeFilePath);
|
||||
type('\n');
|
||||
|
||||
CaretModel caretModel = getEditor().getCaretModel();
|
||||
int offset = caretModel.getOffset();
|
||||
String actualTextWithCaret = new StringBuilder(getEditor().getDocument().getText()).insert(offset, EditorTestUtil.CARET_TAG).toString();
|
||||
|
||||
KotlinTestUtils.assertEqualsToFile(new File(afterFilePath), actualTextWithCaret);
|
||||
}
|
||||
|
||||
public static CodeStyleSettings getSettings() {
|
||||
return CodeStyle.getSettings(getProject());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.formatter;
|
||||
|
||||
import com.intellij.application.options.CodeStyle;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import org.jetbrains.kotlin.idea.KotlinLanguage;
|
||||
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings;
|
||||
import org.jetbrains.kotlin.test.SettingsConfigurator;
|
||||
|
||||
public class FormatSettingsUtil {
|
||||
private FormatSettingsUtil() {
|
||||
}
|
||||
|
||||
public static CodeStyleSettings getSettings(Project project) {
|
||||
return CodeStyle.getSettings(project);
|
||||
}
|
||||
|
||||
public static SettingsConfigurator createConfigurator(String fileText, CodeStyleSettings settings) {
|
||||
return new SettingsConfigurator(fileText,
|
||||
settings.getCustomSettings(KotlinCodeStyleSettings.class),
|
||||
settings.getCommonSettings(KotlinLanguage.INSTANCE));
|
||||
}
|
||||
|
||||
public static SettingsConfigurator createConfigurator(String fileText, Project project) {
|
||||
return createConfigurator(fileText, getSettings(project));
|
||||
}
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.codeInsight.moveUpDown
|
||||
|
||||
import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementLeftAction
|
||||
import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementRightAction
|
||||
import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementDownAction
|
||||
import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementUpAction
|
||||
import com.intellij.codeInsight.editorActions.moveUpDown.StatementUpDownMover
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.application.runWriteAction
|
||||
import com.intellij.openapi.editor.actionSystem.EditorAction
|
||||
import com.intellij.openapi.extensions.Extensions
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase
|
||||
import com.intellij.testFramework.LightPlatformCodeInsightTestCase
|
||||
import junit.framework.ComparisonFailure
|
||||
import junit.framework.TestCase
|
||||
import org.jetbrains.kotlin.formatter.FormatSettingsUtil
|
||||
import org.jetbrains.kotlin.idea.codeInsight.upDownMover.KotlinDeclarationMover
|
||||
import org.jetbrains.kotlin.idea.codeInsight.upDownMover.KotlinExpressionMover
|
||||
import org.jetbrains.kotlin.idea.core.script.isScriptDependenciesUpdaterDisabled
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import java.io.File
|
||||
|
||||
abstract class AbstractMoveStatementTest : AbstractCodeMoverTest() {
|
||||
protected fun doTestClassBodyDeclaration(path: String) {
|
||||
doTest(path, KotlinDeclarationMover::class.java)
|
||||
}
|
||||
|
||||
protected fun doTestExpression(path: String) {
|
||||
doTest(path, KotlinExpressionMover::class.java)
|
||||
}
|
||||
|
||||
private fun doTest(path: String, defaultMoverClass: Class<out StatementUpDownMover>) {
|
||||
doTest(path) { isApplicableExpected, direction ->
|
||||
val movers = Extensions.getExtensions(StatementUpDownMover.STATEMENT_UP_DOWN_MOVER_EP)
|
||||
val info = StatementUpDownMover.MoveInfo()
|
||||
val actualMover = movers.firstOrNull {
|
||||
it.checkAvailable(LightPlatformCodeInsightTestCase.getEditor(), LightPlatformCodeInsightTestCase.getFile(), info, direction == "down")
|
||||
} ?: error("No mover found")
|
||||
|
||||
assertEquals("Unmatched movers", defaultMoverClass.name, actualMover::class.java.name)
|
||||
assertEquals("Invalid applicability", isApplicableExpected, info.toMove2 != null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class AbstractMoveLeftRightTest : AbstractCodeMoverTest() {
|
||||
protected fun doTest(path: String) {
|
||||
doTest(path) { _, _ -> }
|
||||
}
|
||||
}
|
||||
|
||||
abstract class AbstractCodeMoverTest : LightCodeInsightTestCase() {
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
ApplicationManager.getApplication().isScriptDependenciesUpdaterDisabled = true
|
||||
}
|
||||
|
||||
override fun tearDown() {
|
||||
ApplicationManager.getApplication().isScriptDependenciesUpdaterDisabled = false
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
protected fun doTest(path: String, isApplicableChecker: (isApplicableExpected: Boolean, direction: String) -> Unit) {
|
||||
configureByFile(path)
|
||||
|
||||
val fileText = FileUtil.loadFile(File(path), true)
|
||||
val direction = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// MOVE: ")
|
||||
?: error("No MOVE directive found")
|
||||
|
||||
val action = when (direction) {
|
||||
"up" -> MoveStatementUpAction()
|
||||
"down" -> MoveStatementDownAction()
|
||||
"left" -> MoveElementLeftAction()
|
||||
"right" -> MoveElementRightAction()
|
||||
else -> error("Unknown direction: $direction")
|
||||
}
|
||||
|
||||
val isApplicableString = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// IS_APPLICABLE: ")
|
||||
val isApplicableExpected = isApplicableString == null || isApplicableString == "true"
|
||||
|
||||
isApplicableChecker(isApplicableExpected, direction)
|
||||
|
||||
invokeAndCheck(fileText, path, action, isApplicableExpected)
|
||||
}
|
||||
|
||||
private fun invokeAndCheck(fileText: String, path: String, action: EditorAction, isApplicableExpected: Boolean) {
|
||||
val editor = LightPlatformCodeInsightTestCase.getEditor()
|
||||
val project = editor.project!!
|
||||
|
||||
val codeStyleSettings = FormatSettingsUtil.getSettings(project)
|
||||
val configurator = FormatSettingsUtil.createConfigurator(fileText, codeStyleSettings)
|
||||
configurator.configureSettings()
|
||||
|
||||
try {
|
||||
val dataContext = LightPlatformCodeInsightTestCase.getCurrentEditorDataContext()
|
||||
|
||||
val before = editor.document.text
|
||||
runWriteAction { action.actionPerformed(editor, dataContext) }
|
||||
|
||||
val after = editor.document.text
|
||||
val actionDoesNothing = after == before
|
||||
|
||||
TestCase.assertEquals(isApplicableExpected, !actionDoesNothing)
|
||||
|
||||
if (isApplicableExpected) {
|
||||
val afterFilePath = path + ".after"
|
||||
try {
|
||||
checkResultByFile(afterFilePath)
|
||||
}
|
||||
catch (e: ComparisonFailure) {
|
||||
KotlinTestUtils.assertEqualsToFile(File(afterFilePath), editor)
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
codeStyleSettings.clearCodeStyleSettings()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getTestDataPath() = ""
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.editor;
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.testFramework.EditorTestUtil;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import org.jetbrains.kotlin.formatter.FormatSettingsUtil;
|
||||
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase;
|
||||
import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.SettingsConfigurator;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.File;
|
||||
@RunWith(JUnit3WithIdeaConfigurationRunner.class)
|
||||
public class KotlinCommenterTest extends LightCodeInsightTestCase {
|
||||
private static final String BASE_PATH =
|
||||
new File(PluginTestCaseBase.getTestDataPathBase(), "/editor/commenter/").getAbsolutePath();
|
||||
|
||||
public void testGenerateDocComment() throws Exception {
|
||||
doNewLineTypingTest();
|
||||
}
|
||||
|
||||
public void testNewLineInComment() throws Exception {
|
||||
doNewLineTypingTest();
|
||||
}
|
||||
|
||||
public void testNewLineInTag() throws Exception {
|
||||
doNewLineTypingTest();
|
||||
}
|
||||
|
||||
public void testNotFirstColumnWithSpace() throws Exception {
|
||||
doLineCommentTest();
|
||||
}
|
||||
|
||||
public void testNotFirstColumnWithoutSpace() throws Exception {
|
||||
doLineCommentTest();
|
||||
}
|
||||
|
||||
private void doNewLineTypingTest() throws Exception {
|
||||
configure();
|
||||
EditorTestUtil.performTypingAction(getEditor(), '\n');
|
||||
check();
|
||||
}
|
||||
|
||||
private void doLineCommentTest() throws Exception {
|
||||
configure();
|
||||
|
||||
CodeStyleSettings codeStyleSettings = FormatSettingsUtil.getSettings(getProject());
|
||||
try {
|
||||
String text = myFile.getText();
|
||||
|
||||
SettingsConfigurator configurator = FormatSettingsUtil.createConfigurator(text, codeStyleSettings);
|
||||
configurator.configureSettings();
|
||||
|
||||
executeAction("CommentByLineComment");
|
||||
} finally {
|
||||
codeStyleSettings.clearCodeStyleSettings();
|
||||
}
|
||||
|
||||
check();
|
||||
}
|
||||
|
||||
private void configure() throws Exception {
|
||||
configureFromFileText("a.kt", loadFile(getTestName(true) + ".kt"));
|
||||
}
|
||||
|
||||
private void check() {
|
||||
File afterFile = getTestFile(getTestName(true) + "_after.kt");
|
||||
KotlinTestUtils.assertEqualsToFile(afterFile, getEditor(), false);
|
||||
}
|
||||
|
||||
private static File getTestFile(String name) {
|
||||
return new File(BASE_PATH, name);
|
||||
}
|
||||
|
||||
private static String loadFile(String name) throws Exception {
|
||||
File file = getTestFile(name);
|
||||
return FileUtil.loadFile(file, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user