From 4829584d9fcd7de7e183734c503df597362da40b Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 27 Jan 2020 20:08:03 +0100 Subject: [PATCH] cleanup GitOrigin-RevId: a5474db29889f37d01e583fd1ad27e899f0ce331 --- .../lang/LanguageSubstitutorLoadUnloadTest.kt | 31 +++++++++---------- .../com/intellij/openapi/util/extensions.kt | 11 ++++--- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/lang/LanguageSubstitutorLoadUnloadTest.kt b/java/java-tests/testSrc/com/intellij/lang/LanguageSubstitutorLoadUnloadTest.kt index 92373ff97896..29be5ffb4448 100644 --- a/java/java-tests/testSrc/com/intellij/lang/LanguageSubstitutorLoadUnloadTest.kt +++ b/java/java-tests/testSrc/com/intellij/lang/LanguageSubstitutorLoadUnloadTest.kt @@ -5,12 +5,12 @@ import com.intellij.ide.plugins.loadExtensionWithText import com.intellij.lang.java.JavaLanguage import com.intellij.openapi.fileTypes.PlainTextLanguage import com.intellij.openapi.project.Project -import com.intellij.openapi.util.Disposer +import com.intellij.openapi.util.use import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.LanguageSubstitutor import com.intellij.psi.PsiManager -import com.intellij.testFramework.UsefulTestCase import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase +import org.assertj.core.api.Assertions.assertThat class LanguageSubstitutorLoadUnloadTest : LightJavaCodeInsightFixtureTestCase() { fun testBefore() { @@ -19,24 +19,21 @@ class LanguageSubstitutorLoadUnloadTest : LightJavaCodeInsightFixtureTestCase() fun testLoadUnload() { val beforeLoading = myFixture.configureByText("dummy.txt", "package hello;") - UsefulTestCase.assertInstanceOf(beforeLoading.language, PlainTextLanguage::class.java) + assertThat(beforeLoading.language).isInstanceOf(PlainTextLanguage::class.java) val virtualFile = beforeLoading.virtualFile val text = "" - val disposable = loadExtensionWithText(text, javaClass.classLoader) - val psiManager = PsiManager.getInstance(myFixture.project) - val afterLoading = psiManager.findFile(virtualFile) - UsefulTestCase.assertInstanceOf(afterLoading!!.language, JavaLanguage::class.java) - - Disposer.dispose(disposable) - val afterUnloading = psiManager.findFile(virtualFile) - UsefulTestCase.assertInstanceOf(afterUnloading!!.language, PlainTextLanguage::class.java) - } - - private class TextToJavaSubstitutor : LanguageSubstitutor() { - override fun getLanguage(file: VirtualFile, - project: Project): Language? { - return if (file.name.startsWith("dummy")) JavaLanguage.INSTANCE else null + loadExtensionWithText(text, javaClass.classLoader).use { + val afterLoading = PsiManager.getInstance(myFixture.project).findFile(virtualFile) + assertThat(afterLoading!!.language).isInstanceOf(JavaLanguage::class.java) } + val afterUnloading = psiManager.findFile(virtualFile) + assertThat(afterUnloading!!.language).isInstanceOf(PlainTextLanguage::class.java) + } +} + +private class TextToJavaSubstitutor : LanguageSubstitutor() { + override fun getLanguage(file: VirtualFile, project: Project): Language? { + return if (file.name.startsWith("dummy")) JavaLanguage.INSTANCE else null } } \ No newline at end of file diff --git a/platform/util-ex/src/com/intellij/openapi/util/extensions.kt b/platform/util-ex/src/com/intellij/openapi/util/extensions.kt index e1995922cb5a..7e7afe811337 100644 --- a/platform/util-ex/src/com/intellij/openapi/util/extensions.kt +++ b/platform/util-ex/src/com/intellij/openapi/util/extensions.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.util import com.intellij.openapi.Disposable @@ -18,10 +18,13 @@ fun Pair.toNotNull(): kotlin.Pair = requireNotN * @see kotlin.io.use * @see Disposer.dispose */ -inline fun T.use(block: (T) -> R): R = +inline fun T.use(block: (T) -> R): R { try { - block(this) + return block(this) } finally { - if (this != null) Disposer.dispose(this) + if (this != null) { + Disposer.dispose(this) + } } +}