From 78302be5714aa1da79147dd24c0b336a3793a25b Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 20 May 2015 14:29:51 +0300 Subject: [PATCH] Minor code clarification --- .../quickfix/JetWholeProjectModalAction.kt | 62 +++++++++---------- ...ReplaceJavaClassAsAnnotationArgumentFix.kt | 2 +- ...eplaceJavaClassAsAnnotationParameterFix.kt | 2 +- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/JetWholeProjectModalAction.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/JetWholeProjectModalAction.kt index 1e1e33d32f9..c051077036e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/JetWholeProjectModalAction.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/JetWholeProjectModalAction.kt @@ -40,7 +40,9 @@ import org.jetbrains.kotlin.psi.psiUtil.flatMapDescendantsOfTypeVisitor import org.jetbrains.kotlin.utils.singletonOrEmptyList import java.util.HashMap -public abstract class JetWholeProjectModalAction(val title: String) : IntentionAction { +public abstract class JetWholeProjectModalAction(val title: String) : IntentionAction { + private val LOG = Logger.getInstance(javaClass>()); + override final fun startInWriteAction() = false override final fun invoke(project: Project, editor: Editor?, file: PsiFile?) = invoke(project) @@ -51,7 +53,7 @@ public abstract class JetWholeProjectModalAction(val title: String) : In ProgressManager.getInstance().run( object : Task.Modal(project, title, true) { override fun run(indicator: ProgressIndicator) { - val filesToData = HashMap() + val filesToData = HashMap() runReadAction (fun() { val files = PluginJetFilesProvider.allFilesInProject(project) @@ -75,7 +77,7 @@ public abstract class JetWholeProjectModalAction(val title: String) : In } }) - private fun applyAll(project: Project, filesToData: Map) { + private fun applyAll(project: Project, filesToData: Map) { UIUtil.invokeLaterIfNeeded { project.executeCommand(getText()) { runWriteAction { @@ -93,70 +95,66 @@ public abstract class JetWholeProjectModalAction(val title: String) : In } // this method will be started under read action - protected abstract fun collectDataForFile(project: Project, file: JetFile): D? + protected abstract fun collectDataForFile(project: Project, file: JetFile): TData? // this method will be started under write action - protected abstract fun applyChangesForFile(project: Project, file: JetFile, data: D) - - private companion object { - val LOG = Logger.getInstance(javaClass>()); - } + protected abstract fun applyChangesForFile(project: Project, file: JetFile, data: TData) } -public abstract class JetWholeProjectModalByCollectionAction(modalTitle: String) -: JetWholeProjectModalAction>(modalTitle) { - override fun collectDataForFile(project: Project, file: JetFile): Collection? { - val accumulator = arrayListOf() +public abstract class JetWholeProjectModalByCollectionAction(modalTitle: String) +: JetWholeProjectModalAction>(modalTitle) { + override fun collectDataForFile(project: Project, file: JetFile): Collection? { + val accumulator = arrayListOf() collectTasksForFile(project, file, accumulator) - return if (!accumulator.isEmpty()) return accumulator else null + return if (!accumulator.isEmpty()) accumulator else null } - abstract fun collectTasksForFile(project: Project, file: JetFile, accumulator: MutableCollection) + abstract fun collectTasksForFile(project: Project, file: JetFile, accumulator: MutableCollection) } -class JetWholeProjectForEachElementOfTypeFix private ( - private val collectingVisitorFactory: (MutableCollection) -> JetVisitorVoid, - private val tasksProcessor: (Collection) -> Unit, +class JetWholeProjectForEachElementOfTypeFix private ( + private val collectingVisitorFactory: (MutableCollection) -> JetVisitorVoid, + private val tasksProcessor: (Collection) -> Unit, private val name: String, private val familyName: String = name -) : JetWholeProjectModalByCollectionAction("Applying '$name'") { +) : JetWholeProjectModalByCollectionAction("Applying '$name'") { override fun getFamilyName() = familyName override fun getText() = name - override fun collectTasksForFile(project: Project, file: JetFile, accumulator: MutableCollection) { + override fun collectTasksForFile(project: Project, file: JetFile, accumulator: MutableCollection) { file.accept(collectingVisitorFactory(accumulator)) } - override fun applyChangesForFile(project: Project, file: JetFile, data: Collection) = tasksProcessor(data) + override fun applyChangesForFile(project: Project, file: JetFile, data: Collection) = tasksProcessor(data) companion object { - inline fun createByPredicate( - noinline predicate: (E) -> Boolean, - noinline taskProcessor: (E) -> Unit, + inline fun createByPredicate( + noinline predicate: (TElement) -> Boolean, + noinline taskProcessor: (TElement) -> Unit, name: String, familyName: String = name - ) = createByTaskFactory( + ) = createByTaskFactory( taskFactory = { if (predicate(it)) it else null }, taskProcessor = taskProcessor, name = name, familyName = familyName ) - inline fun createByTaskFactory( - noinline taskFactory: (E) -> D?, - noinline taskProcessor: (D) -> Unit, + inline fun createByTaskFactory( + noinline taskFactory: (TElement) -> TTask?, + noinline taskProcessor: (TTask) -> Unit, name: String, familyName: String = name - ) = createForMultiTask( + ) = createForMultiTaskOnElement( tasksFactory = { taskFactory(it).singletonOrEmptyList() }, tasksProcessor = { it.forEach(taskProcessor) }, name = name, familyName = familyName ) - inline fun createForMultiTask( - noinline tasksFactory: (E) -> Collection, - noinline tasksProcessor: (Collection) -> Unit, + inline fun createForMultiTaskOnElement( + noinline tasksFactory: (TElement) -> Collection, + noinline tasksProcessor: (Collection) -> Unit, name: String, familyName: String = name ) = JetWholeProjectForEachElementOfTypeFix( diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceJavaClass/ReplaceJavaClassAsAnnotationArgumentFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceJavaClass/ReplaceJavaClassAsAnnotationArgumentFix.kt index 8ef2c62238e..bf8c5d0bae1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceJavaClass/ReplaceJavaClassAsAnnotationArgumentFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceJavaClass/ReplaceJavaClassAsAnnotationArgumentFix.kt @@ -47,7 +47,7 @@ public class ReplaceJavaClassAsAnnotationArgumentFix( diagnostic.createIntentionForFirstParentOfType(::ReplaceJavaClassAsAnnotationArgumentFix) public fun createWholeProjectFixFactory(): JetSingleIntentionActionFactory = createIntentionFactory { - JetWholeProjectForEachElementOfTypeFix.createForMultiTask( + JetWholeProjectForEachElementOfTypeFix.createForMultiTaskOnElement( tasksFactory = { createReplacementTasks(it) }, tasksProcessor = ::processTasks, name = "Replace javaClass() with T::class in whole project" diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceJavaClass/ReplaceJavaClassAsAnnotationParameterFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceJavaClass/ReplaceJavaClassAsAnnotationParameterFix.kt index d7f215341b9..31c12cc9bfb 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceJavaClass/ReplaceJavaClassAsAnnotationParameterFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceJavaClass/ReplaceJavaClassAsAnnotationParameterFix.kt @@ -45,7 +45,7 @@ public class ReplaceJavaClassAsAnnotationParameterFix( diagnostic.createIntentionForFirstParentOfType(::ReplaceJavaClassAsAnnotationParameterFix) public fun createWholeProjectFixFactory(): JetSingleIntentionActionFactory = createIntentionFactory { - JetWholeProjectForEachElementOfTypeFix.createForMultiTask( + JetWholeProjectForEachElementOfTypeFix.createForMultiTaskOnElement( tasksFactory = ::createReplacementTasksForAnnotationClass, tasksProcessor = ::processTasks, name = "Replace Class with KClass for each annotation in project"