Minor code clarification

This commit is contained in:
Valentin Kipyatkov
2015-05-20 14:29:51 +03:00
parent 5c445ca003
commit 78302be571
3 changed files with 32 additions and 34 deletions
@@ -40,7 +40,9 @@ import org.jetbrains.kotlin.psi.psiUtil.flatMapDescendantsOfTypeVisitor
import org.jetbrains.kotlin.utils.singletonOrEmptyList import org.jetbrains.kotlin.utils.singletonOrEmptyList
import java.util.HashMap import java.util.HashMap
public abstract class JetWholeProjectModalAction<D: Any>(val title: String) : IntentionAction { public abstract class JetWholeProjectModalAction<TData : Any>(val title: String) : IntentionAction {
private val LOG = Logger.getInstance(javaClass<JetWholeProjectModalAction<*>>());
override final fun startInWriteAction() = false override final fun startInWriteAction() = false
override final fun invoke(project: Project, editor: Editor?, file: PsiFile?) = invoke(project) override final fun invoke(project: Project, editor: Editor?, file: PsiFile?) = invoke(project)
@@ -51,7 +53,7 @@ public abstract class JetWholeProjectModalAction<D: Any>(val title: String) : In
ProgressManager.getInstance().run( ProgressManager.getInstance().run(
object : Task.Modal(project, title, true) { object : Task.Modal(project, title, true) {
override fun run(indicator: ProgressIndicator) { override fun run(indicator: ProgressIndicator) {
val filesToData = HashMap<JetFile, D>() val filesToData = HashMap<JetFile, TData>()
runReadAction (fun() { runReadAction (fun() {
val files = PluginJetFilesProvider.allFilesInProject(project) val files = PluginJetFilesProvider.allFilesInProject(project)
@@ -75,7 +77,7 @@ public abstract class JetWholeProjectModalAction<D: Any>(val title: String) : In
} }
}) })
private fun applyAll(project: Project, filesToData: Map<JetFile, D>) { private fun applyAll(project: Project, filesToData: Map<JetFile, TData>) {
UIUtil.invokeLaterIfNeeded { UIUtil.invokeLaterIfNeeded {
project.executeCommand(getText()) { project.executeCommand(getText()) {
runWriteAction { runWriteAction {
@@ -93,70 +95,66 @@ public abstract class JetWholeProjectModalAction<D: Any>(val title: String) : In
} }
// this method will be started under read action // 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 // this method will be started under write action
protected abstract fun applyChangesForFile(project: Project, file: JetFile, data: D) protected abstract fun applyChangesForFile(project: Project, file: JetFile, data: TData)
private companion object {
val LOG = Logger.getInstance(javaClass<JetWholeProjectModalAction<*>>());
}
} }
public abstract class JetWholeProjectModalByCollectionAction<T : Any>(modalTitle: String) public abstract class JetWholeProjectModalByCollectionAction<TTask : Any>(modalTitle: String)
: JetWholeProjectModalAction<Collection<T>>(modalTitle) { : JetWholeProjectModalAction<Collection<TTask>>(modalTitle) {
override fun collectDataForFile(project: Project, file: JetFile): Collection<T>? { override fun collectDataForFile(project: Project, file: JetFile): Collection<TTask>? {
val accumulator = arrayListOf<T>() val accumulator = arrayListOf<TTask>()
collectTasksForFile(project, file, accumulator) 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<T>) abstract fun collectTasksForFile(project: Project, file: JetFile, accumulator: MutableCollection<TTask>)
} }
class JetWholeProjectForEachElementOfTypeFix<T> private ( class JetWholeProjectForEachElementOfTypeFix<TTask> private (
private val collectingVisitorFactory: (MutableCollection<T>) -> JetVisitorVoid, private val collectingVisitorFactory: (MutableCollection<TTask>) -> JetVisitorVoid,
private val tasksProcessor: (Collection<T>) -> Unit, private val tasksProcessor: (Collection<TTask>) -> Unit,
private val name: String, private val name: String,
private val familyName: String = name private val familyName: String = name
) : JetWholeProjectModalByCollectionAction<T>("Applying '$name'") { ) : JetWholeProjectModalByCollectionAction<TTask>("Applying '$name'") {
override fun getFamilyName() = familyName override fun getFamilyName() = familyName
override fun getText() = name override fun getText() = name
override fun collectTasksForFile(project: Project, file: JetFile, accumulator: MutableCollection<T>) { override fun collectTasksForFile(project: Project, file: JetFile, accumulator: MutableCollection<TTask>) {
file.accept(collectingVisitorFactory(accumulator)) file.accept(collectingVisitorFactory(accumulator))
} }
override fun applyChangesForFile(project: Project, file: JetFile, data: Collection<T>) = tasksProcessor(data) override fun applyChangesForFile(project: Project, file: JetFile, data: Collection<TTask>) = tasksProcessor(data)
companion object { companion object {
inline fun <reified E : JetElement> createByPredicate( inline fun <reified TElement : JetElement> createByPredicate(
noinline predicate: (E) -> Boolean, noinline predicate: (TElement) -> Boolean,
noinline taskProcessor: (E) -> Unit, noinline taskProcessor: (TElement) -> Unit,
name: String, name: String,
familyName: String = name familyName: String = name
) = createByTaskFactory<E, E>( ) = createByTaskFactory<TElement, TElement>(
taskFactory = { if (predicate(it)) it else null }, taskFactory = { if (predicate(it)) it else null },
taskProcessor = taskProcessor, taskProcessor = taskProcessor,
name = name, name = name,
familyName = familyName familyName = familyName
) )
inline fun <reified E : JetElement, D : Any> createByTaskFactory( inline fun <reified TElement : JetElement, TTask : Any> createByTaskFactory(
noinline taskFactory: (E) -> D?, noinline taskFactory: (TElement) -> TTask?,
noinline taskProcessor: (D) -> Unit, noinline taskProcessor: (TTask) -> Unit,
name: String, name: String,
familyName: String = name familyName: String = name
) = createForMultiTask<E, D>( ) = createForMultiTaskOnElement<TElement, TTask>(
tasksFactory = { taskFactory(it).singletonOrEmptyList() }, tasksFactory = { taskFactory(it).singletonOrEmptyList() },
tasksProcessor = { it.forEach(taskProcessor) }, tasksProcessor = { it.forEach(taskProcessor) },
name = name, name = name,
familyName = familyName familyName = familyName
) )
inline fun <reified E : JetElement, D> createForMultiTask( inline fun <reified TElement : JetElement, TTask> createForMultiTaskOnElement(
noinline tasksFactory: (E) -> Collection<D>, noinline tasksFactory: (TElement) -> Collection<TTask>,
noinline tasksProcessor: (Collection<D>) -> Unit, noinline tasksProcessor: (Collection<TTask>) -> Unit,
name: String, name: String,
familyName: String = name familyName: String = name
) = JetWholeProjectForEachElementOfTypeFix( ) = JetWholeProjectForEachElementOfTypeFix(
@@ -47,7 +47,7 @@ public class ReplaceJavaClassAsAnnotationArgumentFix(
diagnostic.createIntentionForFirstParentOfType(::ReplaceJavaClassAsAnnotationArgumentFix) diagnostic.createIntentionForFirstParentOfType(::ReplaceJavaClassAsAnnotationArgumentFix)
public fun createWholeProjectFixFactory(): JetSingleIntentionActionFactory = createIntentionFactory { public fun createWholeProjectFixFactory(): JetSingleIntentionActionFactory = createIntentionFactory {
JetWholeProjectForEachElementOfTypeFix.createForMultiTask<JetAnnotationEntry, ReplacementTask>( JetWholeProjectForEachElementOfTypeFix.createForMultiTaskOnElement<JetAnnotationEntry, ReplacementTask>(
tasksFactory = { createReplacementTasks(it) }, tasksFactory = { createReplacementTasks(it) },
tasksProcessor = ::processTasks, tasksProcessor = ::processTasks,
name = "Replace javaClass<T>() with T::class in whole project" name = "Replace javaClass<T>() with T::class in whole project"
@@ -45,7 +45,7 @@ public class ReplaceJavaClassAsAnnotationParameterFix(
diagnostic.createIntentionForFirstParentOfType(::ReplaceJavaClassAsAnnotationParameterFix) diagnostic.createIntentionForFirstParentOfType(::ReplaceJavaClassAsAnnotationParameterFix)
public fun createWholeProjectFixFactory(): JetSingleIntentionActionFactory = createIntentionFactory { public fun createWholeProjectFixFactory(): JetSingleIntentionActionFactory = createIntentionFactory {
JetWholeProjectForEachElementOfTypeFix.createForMultiTask( JetWholeProjectForEachElementOfTypeFix.createForMultiTaskOnElement(
tasksFactory = ::createReplacementTasksForAnnotationClass, tasksFactory = ::createReplacementTasksForAnnotationClass,
tasksProcessor = ::processTasks, tasksProcessor = ::processTasks,
name = "Replace Class<T> with KClass<T> for each annotation in project" name = "Replace Class<T> with KClass<T> for each annotation in project"