diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/JavaEditingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/JavaEditingTest.java index 5c0457c9c5d7..c416fe87536c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/JavaEditingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/JavaEditingTest.java @@ -106,15 +106,11 @@ public class JavaEditingTest extends AbstractBasicJavaEditingTest { } public void testSmartIndentOnEnterWithinNonLastStatement() { - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - boolean oldValue = settings.SMART_INDENT_ON_ENTER; - settings.SMART_INDENT_ON_ENTER = true; - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.SMART_INDENT_ON_ENTER = true; doTest(JavaFileType.INSTANCE, '\n'); - } - finally { - settings.SMART_INDENT_ON_ENTER = oldValue; - } + return null; + }); } public void testEmacsTabWithSelection() { diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/CopyReferenceActionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/CopyReferenceActionTest.java index 414587f30b2c..7d47758b5c57 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/CopyReferenceActionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/CopyReferenceActionTest.java @@ -10,11 +10,10 @@ import com.intellij.openapi.ide.CopyPasteManager; import com.intellij.psi.PsiFile; import com.intellij.testFramework.LightProjectDescriptor; import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; +import com.intellij.util.ThrowableRunnable; import org.jetbrains.annotations.NotNull; public class CopyReferenceActionTest extends LightJavaCodeInsightFixtureTestCase { - private int oldSetting; - @NotNull @Override protected LightProjectDescriptor getProjectDescriptor() { @@ -27,25 +26,12 @@ public class CopyReferenceActionTest extends LightJavaCodeInsightFixtureTestCase } @Override - protected void setUp() throws Exception { - super.setUp(); - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - oldSetting = settings.ADD_IMPORTS_ON_PASTE; - settings.ADD_IMPORTS_ON_PASTE = CodeInsightSettings.YES; - } - - @Override - protected void tearDown() throws Exception { - try { - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - settings.ADD_IMPORTS_ON_PASTE = oldSetting; - } - catch (Throwable e) { - addSuppressedException(e); - } - finally { - super.tearDown(); - } + protected void runTestRunnable(@NotNull ThrowableRunnable testRunnable) throws Throwable { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.ADD_IMPORTS_ON_PASTE = CodeInsightSettings.YES; + super.runTestRunnable(testRunnable); + return null; + }); } public void testConstructor() { doTest(); } diff --git a/java/java-tests/testSrc/com/intellij/java/psi/OptimizeImportsMultiFileTest.java b/java/java-tests/testSrc/com/intellij/java/psi/OptimizeImportsMultiFileTest.java index 4db7c4a1071d..b46e77d4d4db 100644 --- a/java/java-tests/testSrc/com/intellij/java/psi/OptimizeImportsMultiFileTest.java +++ b/java/java-tests/testSrc/com/intellij/java/psi/OptimizeImportsMultiFileTest.java @@ -35,9 +35,8 @@ public class OptimizeImportsMultiFileTest extends JavaPsiTestCase { } public void testOptimizeImportsMustAddUnambiguousImportsIfTheCorrespondingSettingIsOn() throws Exception { - boolean importsOnTheFly = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY; - CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true; - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true; VirtualFile root = createTestProjectStructure(OptimizeImportsTest.BASE_PATH + "/src1", false); PsiTestUtil.addSourceRoot(getModule(), root); PsiDirectory directory = myPsiManager.findDirectory(root); @@ -52,16 +51,13 @@ public class OptimizeImportsMultiFileTest extends JavaPsiTestCase { assertTrue(text1After, text1After.contains("import java.util.ArrayList;")); String text2After = VfsUtilCore.loadText(root.findFileByRelativePath("p/X2.java")); assertTrue(text2After, text2After.contains("import java.util.ArrayList;")); - } - finally { - CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = importsOnTheFly; - } + return null; + }); } public void testOptimizeImportsMustNotAddUnambiguousImportsIfTheCorrespondingSettingIsOff() throws Exception { - boolean importsOnTheFly = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY; - CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = false; - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = false; VirtualFile root = createTestProjectStructure(OptimizeImportsTest.BASE_PATH + "/src1", false); PsiTestUtil.addSourceRoot(getModule(), root); PsiDirectory directory = myPsiManager.findDirectory(root); @@ -76,9 +72,7 @@ public class OptimizeImportsMultiFileTest extends JavaPsiTestCase { assertFalse(text1After, text1After.contains("import java.util.ArrayList;")); String text2After = VfsUtilCore.loadText(root.findFileByRelativePath("p/X2.java")); assertFalse(text2After, text2After.contains("import java.util.ArrayList;")); - } - finally { - CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = importsOnTheFly; - } + return null; + }); } } diff --git a/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavaEnterActionTest.java b/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavaEnterActionTest.java index de1b77499b4d..da89980cf8ea 100644 --- a/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavaEnterActionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavaEnterActionTest.java @@ -349,36 +349,24 @@ public class JavaEnterActionTest extends AbstractBasicJavaEnterActionTest { } public void testNoCloseJavaDocComment() { - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - boolean old = settings.CLOSE_COMMENT_ON_ENTER; - settings.CLOSE_COMMENT_ON_ENTER = false; - - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.CLOSE_COMMENT_ON_ENTER = false; doTextTest("java", "/**", "/**\n "); - } - finally { - settings.CLOSE_COMMENT_ON_ENTER = old; - } + return null; + }); } public void testNoSmartIndentInJavadoc() { - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - boolean indent = settings.SMART_INDENT_ON_ENTER; - settings.SMART_INDENT_ON_ENTER = false; - boolean stub = settings.JAVADOC_STUB_ON_ENTER; - settings.JAVADOC_STUB_ON_ENTER = false; - - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.SMART_INDENT_ON_ENTER = false; + settings.JAVADOC_STUB_ON_ENTER = false; configureByFile("/codeInsight/enterAction/settings/NoJavadocStub.java"); performAction(); checkResultByFile(null, "/codeInsight/enterAction/settings/NoJavadocStub_after.java", true); // side effect... - } - finally { - settings.SMART_INDENT_ON_ENTER = indent; - settings.JAVADOC_STUB_ON_ENTER = stub; - } + return null; + }); } public void testLineCommentAtTrailingSpaces() { @@ -390,18 +378,13 @@ public class JavaEnterActionTest extends AbstractBasicJavaEnterActionTest { } public void testNoJavadocStub() { - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - boolean old = settings.JAVADOC_STUB_ON_ENTER; - settings.JAVADOC_STUB_ON_ENTER = false; - - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.JAVADOC_STUB_ON_ENTER = false; configureByFile("/codeInsight/enterAction/settings/NoJavadocStub.java"); performAction(); checkResultByFile("/codeInsight/enterAction/settings/NoJavadocStub_after.java"); - } - finally { - settings.JAVADOC_STUB_ON_ENTER = old; - } + return null; + }); } diff --git a/java/java-tests/testSrc/com/intellij/openapi/editor/impl/JavaFoldingTest.java b/java/java-tests/testSrc/com/intellij/openapi/editor/impl/JavaFoldingTest.java index 5807278c4e0a..bd9595651282 100644 --- a/java/java-tests/testSrc/com/intellij/openapi/editor/impl/JavaFoldingTest.java +++ b/java/java-tests/testSrc/com/intellij/openapi/editor/impl/JavaFoldingTest.java @@ -1000,11 +1000,10 @@ public class JavaFoldingTest extends JavaFoldingTestCase { } public void test_imports_remain_collapsed_when_new_item_is_added_at_the_end() { - boolean oldValue = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY; - CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true; - DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);// tests disable this by default - ((CodeInsightTestFixtureImpl)myFixture).canChangeDocumentDuringHighlighting(true); - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true; + DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);// tests disable this by default + ((CodeInsightTestFixtureImpl)myFixture).canChangeDocumentDuringHighlighting(true); configure(""" import java.util.ArrayList; import java.util.List; @@ -1034,10 +1033,8 @@ public class JavaFoldingTest extends JavaFoldingTestCase { }"""); myFixture.doHighlighting();// update folding for the new text assertTopLevelFoldRegionsState("[FoldRegion +(7:76), placeholder='...']"); - } - finally { - CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = oldValue; - } + return null; + }); } public void testGroupedFoldingsAreNotUpdatedOnUnrelatedDocumentChange() { diff --git a/platform/analysis-impl/api-dump-unreviewed.txt b/platform/analysis-impl/api-dump-unreviewed.txt index 1dac8be9b5be..c3a43b2d82ac 100644 --- a/platform/analysis-impl/api-dump-unreviewed.txt +++ b/platform/analysis-impl/api-dump-unreviewed.txt @@ -171,6 +171,7 @@ c:com.intellij.codeInsight.CodeInsightSettings - isSelectAutopopupSuggestionsByChars():Z - loadState(org.jdom.Element):V - noStateLoaded():V +- s:runWithTemporarySettings(com.intellij.util.ThrowableConvertor):java.lang.Object - setBackspaceMode(com.intellij.codeInsight.editorActions.SmartBackspaceMode):V - setCompletionCaseSensitive(I):V - setSelectAutopopupSuggestionsByChars(Z):V diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/CodeInsightSettings.java b/platform/analysis-impl/src/com/intellij/codeInsight/CodeInsightSettings.java index 2ce5304766bb..4693d2ddf22a 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/CodeInsightSettings.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/CodeInsightSettings.java @@ -16,6 +16,7 @@ import com.intellij.openapi.util.Disposer; import com.intellij.serialization.SerializationException; import com.intellij.util.ArrayUtilRt; import com.intellij.util.ReflectionUtil; +import com.intellij.util.ThrowableConvertor; import com.intellij.util.xmlb.annotations.OptionTag; import com.intellij.util.xmlb.annotations.Property; import com.intellij.util.xmlb.annotations.Transient; @@ -24,6 +25,7 @@ import org.intellij.lang.annotations.MagicConstant; import org.jdom.Element; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -242,4 +244,28 @@ public class CodeInsightSettings implements PersistentStateComponent, C if (o == null || getClass() != o.getClass()) return false; return ReflectionUtil.comparePublicNonFinalFields(this, o); } + + /** + * Run the {@code consumer} with the current {@link CodeInsightSettings} and then restore them to the state before this method call. + * Useful for running the tests with custom code insight settings, like this: + *
+   *  runWithTemporarySettings(settings -> {
+   *    settings.MY_SETTING_TO_TEST = newValue;
+   *    settings.OTHER_SETTING_TO_TEST = newValue2;
+   *    doTest();
+   *  });
+   * 
+ * This will run {@code doTest()} with some settings changed, and then restore them automatically when the test finished. + */ + @TestOnly + public static T runWithTemporarySettings(@NotNull ThrowableConvertor consumer) throws E { + CodeInsightSettings settings = getInstance(); + Element temp = settings.getState(); + try { + return consumer.convert(settings); + } + finally { + settings.loadState(temp); + } + } } \ No newline at end of file diff --git a/platform/lang-impl/testSources/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandlerTest.java b/platform/lang-impl/testSources/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandlerTest.java index 969dca988f5c..b3f35d4a90ea 100644 --- a/platform/lang-impl/testSources/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandlerTest.java +++ b/platform/lang-impl/testSources/com/intellij/codeInsight/editorActions/SimpleIndentingBackspaceHandlerTest.java @@ -51,15 +51,12 @@ public class SimpleIndentingBackspaceHandlerTest extends LightPlatformCodeInsigh } private void doTest(String before, String after) { - SmartBackspaceMode savedMode = CodeInsightSettings.getInstance().getBackspaceMode(); - try { - CodeInsightSettings.getInstance().setBackspaceMode(SmartBackspaceMode.INDENT); + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.setBackspaceMode(SmartBackspaceMode.INDENT); configureFromFileText(getTestName(false) + ".txt", before); executeAction(IdeActions.ACTION_EDITOR_BACKSPACE); checkResultByText(after); - } - finally { - CodeInsightSettings.getInstance().setBackspaceMode(savedMode); - } + return null; + }); } } diff --git a/platform/platform-tests/testSrc/com/intellij/application/options/codeInsight/editor/quotes/SelectionQuotingTypedHandlerTest.java b/platform/platform-tests/testSrc/com/intellij/application/options/codeInsight/editor/quotes/SelectionQuotingTypedHandlerTest.java index 31af559e5839..13aa4f9522b6 100644 --- a/platform/platform-tests/testSrc/com/intellij/application/options/codeInsight/editor/quotes/SelectionQuotingTypedHandlerTest.java +++ b/platform/platform-tests/testSrc/com/intellij/application/options/codeInsight/editor/quotes/SelectionQuotingTypedHandlerTest.java @@ -10,12 +10,10 @@ import com.intellij.openapi.editor.ex.EditorEx; import com.intellij.openapi.fileTypes.FileTypes; import com.intellij.openapi.project.Project; import com.intellij.testFramework.fixtures.BasePlatformTestCase; +import com.intellij.util.ThrowableRunnable; import org.jetbrains.annotations.NotNull; public class SelectionQuotingTypedHandlerTest extends BasePlatformTestCase { - - private boolean myPrevValue; - /** * Performs an action as write action * @@ -27,23 +25,12 @@ public class SelectionQuotingTypedHandlerTest extends BasePlatformTestCase { } @Override - protected void setUp() throws Exception { - super.setUp(); - myPrevValue = CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED; - CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = true; - } - - @Override - protected void tearDown() throws Exception { - try { - CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = myPrevValue; - } - catch (Throwable e) { - addSuppressedException(e); - } - finally { - super.tearDown(); - } + protected void runTestRunnable(@NotNull ThrowableRunnable testRunnable) throws Throwable { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.SURROUND_SELECTION_ON_QUOTE_TYPED = true; + super.runTestRunnable(testRunnable); + return null; + }); } public void testWOSelection() { diff --git a/platform/platform-tests/testSrc/com/intellij/codeInsight/PlainTextEditingTest.java b/platform/platform-tests/testSrc/com/intellij/codeInsight/PlainTextEditingTest.java index f8eae1a02d98..415474803d22 100644 --- a/platform/platform-tests/testSrc/com/intellij/codeInsight/PlainTextEditingTest.java +++ b/platform/platform-tests/testSrc/com/intellij/codeInsight/PlainTextEditingTest.java @@ -64,10 +64,8 @@ public class PlainTextEditingTest extends EditingTestBase { } public void testCopyPasteWithoutUnnecessaryIndent() { - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - int oldValue = settings.REFORMAT_ON_PASTE; - settings.REFORMAT_ON_PASTE = CodeInsightSettings.INDENT_BLOCK; - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.REFORMAT_ON_PASTE = CodeInsightSettings.INDENT_BLOCK; doTest(PlainTextFileType.INSTANCE, () -> { // Move caret to the non-zero column. getEditor().getCaretModel().moveToOffset(3); @@ -79,10 +77,8 @@ public class PlainTextEditingTest extends EditingTestBase { copy(); paste(); }); - } - finally { - settings.REFORMAT_ON_PASTE = oldValue; - } + return null; + }); } public void testCamelHumpsSelectionAndDigits() { @@ -228,15 +224,11 @@ public class PlainTextEditingTest extends EditingTestBase { EditorEx editorEx = (EditorEx)getEditor(); editorEx.setStickySelection(true); editorEx.getCaretModel().moveCaretRelatively(2, 0, false, false, false); - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - boolean oldValue = settings.SURROUND_SELECTION_ON_QUOTE_TYPED; - settings.SURROUND_SELECTION_ON_QUOTE_TYPED = true; - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.SURROUND_SELECTION_ON_QUOTE_TYPED = true; type('\''); - } - finally { - settings.SURROUND_SELECTION_ON_QUOTE_TYPED = oldValue; - } + return null; + }); checkResultByText("ab'cd'ef"); } diff --git a/platform/platform-tests/testSrc/com/intellij/codeInsight/editorActions/CustomFileTypeEditorTest.java b/platform/platform-tests/testSrc/com/intellij/codeInsight/editorActions/CustomFileTypeEditorTest.java index 9d8cea57b60f..15fbf4d6bb41 100644 --- a/platform/platform-tests/testSrc/com/intellij/codeInsight/editorActions/CustomFileTypeEditorTest.java +++ b/platform/platform-tests/testSrc/com/intellij/codeInsight/editorActions/CustomFileTypeEditorTest.java @@ -154,12 +154,9 @@ public class CustomFileTypeEditorTest extends BasePlatformTestCase { } public void testReplaceQuoteDontSurroundSelection() { - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - boolean oldValue = settings.SURROUND_SELECTION_ON_QUOTE_TYPED; - settings.SURROUND_SELECTION_ON_QUOTE_TYPED = false; - boolean oldValuePairQuote = settings.AUTOINSERT_PAIR_QUOTE; - settings.AUTOINSERT_PAIR_QUOTE = false; - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.SURROUND_SELECTION_ON_QUOTE_TYPED = false; + settings.AUTOINSERT_PAIR_QUOTE = false; checkTyping("a.cs", "\"a\"", '\'', "'a\""); checkTyping("a.cs", "\"a'", '\'', "'a'"); checkTyping("a.cs", "\"a\"", '\'', "\"a'"); @@ -169,18 +166,13 @@ public class CustomFileTypeEditorTest extends BasePlatformTestCase { checkTyping("a.cs", "'a'", '\"', "\"a'"); checkTyping("a.cs", "\"a'", '\"', "\"a\""); checkTyping("a.cs", "'a'", '\"', "'a\""); - } - finally { - settings.SURROUND_SELECTION_ON_QUOTE_TYPED = oldValue; - settings.AUTOINSERT_PAIR_QUOTE = oldValuePairQuote; - } + return null; + }); } public void testReplaceQuoteDontInsertPair() { - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - boolean oldValue = settings.AUTOINSERT_PAIR_QUOTE; - settings.AUTOINSERT_PAIR_QUOTE = false; - try { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.AUTOINSERT_PAIR_QUOTE = false; checkTyping("a.cs", "\"a\"", '\'', "'\"'a\""); checkTyping("a.cs", "\"a'", '\'', "'\"'a'"); checkTyping("a.cs", "\"a\"", '\'', "\"a'\"'"); @@ -189,10 +181,8 @@ public class CustomFileTypeEditorTest extends BasePlatformTestCase { checkTyping("a.cs", "'a'", '\"', "\"'\"a'"); checkTyping("a.cs", "\"a'", '\"', "\"a\"'\""); checkTyping("a.cs", "'a'", '\"', "'a\"'\""); - } - finally { - settings.AUTOINSERT_PAIR_QUOTE = oldValue; - } + return null; + }); } public void testReplaceQuote() { diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/CopyActionSimpleHandlerTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/CopyActionSimpleHandlerTest.java index cbf02ad1a9cd..10b43d54acb0 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/CopyActionSimpleHandlerTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/actions/CopyActionSimpleHandlerTest.java @@ -16,27 +16,16 @@ package com.intellij.openapi.editor.actions; import com.intellij.codeInsight.CodeInsightSettings; +import com.intellij.util.ThrowableRunnable; +import org.jetbrains.annotations.NotNull; public class CopyActionSimpleHandlerTest extends CopyActionTest { - private int myPrevValue; - @Override - public void setUp() throws Exception { - super.setUp(); - myPrevValue = CodeInsightSettings.getInstance().ADD_IMPORTS_ON_PASTE; - CodeInsightSettings.getInstance().ADD_IMPORTS_ON_PASTE = CodeInsightSettings.NO; - } - - @Override - public void tearDown() throws Exception { - try { - CodeInsightSettings.getInstance().ADD_IMPORTS_ON_PASTE = myPrevValue; - } - catch (Throwable e) { - addSuppressedException(e); - } - finally { - super.tearDown(); - } + protected void runTestRunnable(@NotNull ThrowableRunnable testRunnable) throws Throwable { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.ADD_IMPORTS_ON_PASTE = CodeInsightSettings.NO; + super.runTestRunnable(testRunnable); + return null; + }); } } diff --git a/plugins/kotlin/completion/tests-shared/test/org/jetbrains/kotlin/idea/completion/test/CompletionTestUtil.kt b/plugins/kotlin/completion/tests-shared/test/org/jetbrains/kotlin/idea/completion/test/CompletionTestUtil.kt index aaafe7ffebe8..c41aca791745 100644 --- a/plugins/kotlin/completion/tests-shared/test/org/jetbrains/kotlin/idea/completion/test/CompletionTestUtil.kt +++ b/plugins/kotlin/completion/tests-shared/test/org/jetbrains/kotlin/idea/completion/test/CompletionTestUtil.kt @@ -50,16 +50,10 @@ fun testCompletion( fun testWithAutoCompleteSetting(fileText: String, doTest: () -> Unit) { val autoComplete = ExpectedCompletionUtils.getAutocompleteSetting(fileText) ?: false - val settings = CodeInsightSettings.getInstance() - val oldValue1 = settings.AUTOCOMPLETE_ON_CODE_COMPLETION - val oldValue2 = settings.AUTOCOMPLETE_ON_SMART_TYPE_COMPLETION - try { + CodeInsightSettings.runWithTemporarySettings<_,Error> { settings -> settings.AUTOCOMPLETE_ON_CODE_COMPLETION = autoComplete settings.AUTOCOMPLETE_ON_SMART_TYPE_COMPLETION = autoComplete doTest() - } finally { - settings.AUTOCOMPLETE_ON_CODE_COMPLETION = oldValue1 - settings.AUTOCOMPLETE_ON_SMART_TYPE_COMPLETION = oldValue2 } } diff --git a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java index 279959b8d318..bae9a0c55c9a 100644 --- a/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java +++ b/python/testSrc/com/jetbrains/python/PyCopyPasteTest.java @@ -4,30 +4,19 @@ package com.jetbrains.python; import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.openapi.actionSystem.IdeActions; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; +import com.intellij.util.ThrowableRunnable; import com.jetbrains.python.fixtures.PyTestCase; +import org.jetbrains.annotations.NotNull; public class PyCopyPasteTest extends PyTestCase { - private boolean myOldEnabled; - @Override - public void setUp() throws Exception { - super.setUp(); - myOldEnabled = CodeInsightSettings.getInstance().INDENT_TO_CARET_ON_PASTE; - CodeInsightSettings.getInstance().INDENT_TO_CARET_ON_PASTE = true; - } - - @Override - public void tearDown() throws Exception { - try { - CodeInsightSettings.getInstance().INDENT_TO_CARET_ON_PASTE = myOldEnabled; - } - catch (Throwable e) { - addSuppressedException(e); - } - finally { - super.tearDown(); - } + protected void runTestRunnable(@NotNull ThrowableRunnable testRunnable) throws Throwable { + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.INDENT_TO_CARET_ON_PASTE = true; + super.runTestRunnable(testRunnable); + return null; + }); } public void testIndent1() { diff --git a/xml/tests/src/com/intellij/codeInsight/completion/XmlTypedHandlersTest.java b/xml/tests/src/com/intellij/codeInsight/completion/XmlTypedHandlersTest.java index 0c1edb61773e..1acfe7b83e40 100644 --- a/xml/tests/src/com/intellij/codeInsight/completion/XmlTypedHandlersTest.java +++ b/xml/tests/src/com/intellij/codeInsight/completion/XmlTypedHandlersTest.java @@ -23,11 +23,21 @@ import com.intellij.ide.highlighter.XmlFileType; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.formatter.xml.HtmlCodeStyleSettings; import com.intellij.testFramework.fixtures.BasePlatformTestCase; +import com.intellij.util.ThrowableRunnable; +import org.jetbrains.annotations.NotNull; /** * @author Dmitry Avdeev */ public class XmlTypedHandlersTest extends BasePlatformTestCase { + @Override + protected void runTestRunnable(@NotNull ThrowableRunnable testRunnable) throws Throwable { + CodeInsightSettings.runWithTemporarySettings(settings -> { + super.runTestRunnable(testRunnable); + return null; + }); + } + public void testClosingTag() { doTest("<", '/', ""); } @@ -201,77 +211,45 @@ public class XmlTypedHandlersTest extends BasePlatformTestCase { } public void testSelectionBraces() { - boolean surround = CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED; - try { CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = true; doTest("
", '(', "(
)"); - } finally { - CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = surround; - } } public void testSelectionBracesInner() { - boolean surround = CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED; - try { CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = true; doTest("
", '(', "
(
)
"); - } finally { - CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = surround; - } } public void testSelectionBracesStart() { - boolean surround = CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED; - try { CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = true; doTest("
", '(', "(
)
"); - } finally { - CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = surround; - } } public void testSelectionBracesEnd() { - boolean surround = CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED; - try { CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = true; doTest("
", '(', "
(
)"); - } finally { - CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = surround; - } } public void testSelectionBracesShort() { - boolean surround = CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED; - try { CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = true; doTest("
", '(', "(
)"); - } - finally { - CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = surround; - } } public void testSelectionBracesShortInner() { - boolean surround = CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED; - try { CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = true; doTest("
", '(', "
(
)
"); - } - finally { - CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = surround; - } } public void testTagClosing() {