From d1455d0d2296417b3e3f9c4949be46b2c8a69fd4 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Mon, 29 Jun 2020 12:42:46 +0700 Subject: [PATCH] KotlinOptimizeImportsRefactoringHelper: should remove unused directives by pointer #KT-39899 Fixed #EA-209827 Fixed --- .../KotlinOptimizeImportsRefactoringHelper.kt | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinOptimizeImportsRefactoringHelper.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinOptimizeImportsRefactoringHelper.kt index f3f9131cdc0..37443da273c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinOptimizeImportsRefactoringHelper.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinOptimizeImportsRefactoringHelper.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-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. + * Copyright 2010-2020 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 @@ -74,16 +63,13 @@ class KotlinOptimizeImportsRefactoringHelper : RefactoringHelper> { for ((counter, pointer) in pointers.withIndex()) { indicator.fraction = counter.toDouble() / myTotal - runReadAction { - val element = pointer.element - if (element?.isValid == true) element!! else null - }?.let { directive -> - val presentableUrl = runReadAction { directive.containingFile }.virtualFile.presentableUrl + runReadAction { pointer.element?.takeIf { it.isValid }?.containingKtFile?.virtualFile }?.let { virtualFile -> + val presentableUrl = virtualFile.presentableUrl indicator.text2 = presentableUrl ApplicationManager.getApplication().invokeAndWait { project.executeWriteCommand(KotlinBundle.message("delete.0", presentableUrl)) { try { - directive.delete() + pointer.element?.delete() } catch (e: IncorrectOperationException) { LOG.error(e) } @@ -103,10 +89,8 @@ class KotlinOptimizeImportsRefactoringHelper : RefactoringHelper> { private val REMOVING_REDUNDANT_IMPORTS_TITLE = KotlinBundle.message("optimize.imports.task.removing.redundant.imports") } - override fun prepareOperation(usages: Array): Set { - return usages.mapNotNullTo(LinkedHashSet()) { - if (!it.isNonCodeUsage) it.file as? KtFile else null - } + override fun prepareOperation(usages: Array): Set = usages.mapNotNullTo(LinkedHashSet()) { + if (!it.isNonCodeUsage) it.file as? KtFile else null } override fun performOperation(project: Project, operationData: Set) { @@ -128,6 +112,7 @@ class KotlinOptimizeImportsRefactoringHelper : RefactoringHelper> { progressManager.run(progressTask) } } + progressManager.run(collectTask) } }