AbstractFormatterTest: cleanup code

This commit is contained in:
Dmitry Gridin
2020-01-14 18:56:06 +07:00
parent c27bf051d5
commit 9109f29c88
@@ -44,19 +44,14 @@ public abstract class AbstractFormatterTest extends KotlinLightIdeaTestCase {
} }
private static final Map<Action, TestFormatAction> ACTIONS = new EnumMap<Action, TestFormatAction>(Action.class); private static final Map<Action, TestFormatAction> ACTIONS = new EnumMap<Action, TestFormatAction>(Action.class);
static { static {
ACTIONS.put(Action.REFORMAT, new TestFormatAction() { ACTIONS.put(Action.REFORMAT,
@Override (psiFile, startOffset, endOffset) -> CodeStyleManager.getInstance(psiFile.getProject())
public void run(PsiFile psiFile, int startOffset, int endOffset) { .reformatText(psiFile, startOffset, endOffset));
CodeStyleManager.getInstance(psiFile.getProject()).reformatText(psiFile, startOffset, endOffset); ACTIONS.put(Action.INDENT,
} (psiFile, startOffset, endOffset) -> CodeStyleManager.getInstance(psiFile.getProject())
}); .adjustLineIndent(psiFile, startOffset));
ACTIONS.put(Action.INDENT, new TestFormatAction() {
@Override
public void run(PsiFile psiFile, int startOffset, int endOffset) {
CodeStyleManager.getInstance(psiFile.getProject()).adjustLineIndent(psiFile, startOffset);
}
});
} }
private static final String BASE_PATH = private static final String BASE_PATH =
@@ -75,40 +70,32 @@ public abstract class AbstractFormatterTest extends KotlinLightIdeaTestCase {
doTextTest(Action.REFORMAT, text, fileAfter, extension); doTextTest(Action.REFORMAT, text, fileAfter, extension);
} }
public void doTextTest(final Action action, final String text, File fileAfter, String extension) throws IncorrectOperationException { public void doTextTest(Action action, String text, File fileAfter, String extension) throws IncorrectOperationException {
final PsiFile file = createFile("A" + extension, text); PsiFile file = createFile("A" + extension, text);
if (myLineRange != null) { if (myLineRange != null) {
DocumentImpl document = new DocumentImpl(text); DocumentImpl document = new DocumentImpl(text);
myTextRange = myTextRange =
new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()), document.getLineEndOffset(myLineRange.getEndOffset())); new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()),
document.getLineEndOffset(myLineRange.getEndOffset()));
} }
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject()); PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
final Document document = manager.getDocument(file); Document document = manager.getDocument(file);
CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> {
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { document.replaceString(0, document.getTextLength(), text);
@Override manager.commitDocument(document);
public void run() { try {
ApplicationManager.getApplication().runWriteAction(new Runnable() { TextRange rangeToUse = myTextRange;
@Override if (rangeToUse == null) {
public void run() { rangeToUse = file.getTextRange();
document.replaceString(0, document.getTextLength(), text); }
manager.commitDocument(document); ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
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);
}
}
});
} }
}, "", ""); catch (IncorrectOperationException e) {
fail(e.getLocalizedMessage());
}
}), "", "");
if (document == null) { if (document == null) {
@@ -154,7 +141,8 @@ public abstract class AbstractFormatterTest extends KotlinLightIdeaTestCase {
} }
doTextTest(originalFileText, new File(expectedFileNameWithExtension), testFileExtension); doTextTest(originalFileText, new File(expectedFileNameWithExtension), testFileExtension);
} finally { }
finally {
codeStyleSettings.clearCodeStyleSettings(); codeStyleSettings.clearCodeStyleSettings();
} }
} }