com.jetbrains.plugin.structure.intellij.problems.LevelRemappingPluginCreationResultResolver.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of structure-intellij Show documentation
Show all versions of structure-intellij Show documentation
Library for parsing JetBrains IDE plugins. Can be used to verify that plugin complies with the JetBrains Marketplace requirements.
package com.jetbrains.plugin.structure.intellij.problems
import com.jetbrains.plugin.structure.base.plugin.PluginCreationFail
import com.jetbrains.plugin.structure.base.plugin.PluginCreationResult
import com.jetbrains.plugin.structure.base.plugin.PluginCreationSuccess
import com.jetbrains.plugin.structure.base.problems.PluginProblem
import com.jetbrains.plugin.structure.base.problems.ReclassifiedPluginProblem
import com.jetbrains.plugin.structure.intellij.plugin.IdePlugin
import kotlin.reflect.KClass
class LevelRemappingPluginCreationResultResolver(private val delegatedResolver: PluginCreationResultResolver,
additionalLevelRemapping: Map, RemappedLevel> = emptyMap()
) : PluginCreationResultResolver {
private val remappedLevel: Map, RemappedLevel> = additionalLevelRemapping
override fun resolve(plugin: IdePlugin, problems: List): PluginCreationResult {
return when (val pluginCreationResult = delegatedResolver.resolve(plugin, problems)) {
is PluginCreationSuccess -> remapSuccess(pluginCreationResult)
is PluginCreationFail -> remapFailure(plugin, pluginCreationResult)
}
}
private fun remapSuccess(pluginCreationResult: PluginCreationSuccess): PluginCreationResult {
return with(pluginCreationResult) {
copy(warnings = remapWarnings(warnings), unacceptableWarnings = remapUnacceptableWarnings(unacceptableWarnings))
}
}
private fun remapFailure(plugin: IdePlugin, pluginCreationResult: PluginCreationFail): PluginCreationResult {
return with(pluginCreationResult) {
val remappedErrorsAndWarnings = remapErrorsAndWarnings(errorsAndWarnings)
if (remappedErrorsAndWarnings.hasNoErrors()) {
return PluginCreationSuccess(plugin, remappedErrorsAndWarnings)
} else {
copy(errorsAndWarnings = remapErrorsAndWarnings(this.errorsAndWarnings))
}
}
}
private fun remapWarnings(warnings: List): List {
return warnings.mapNotNull(::remapPluginProblemLevel)
}
private fun remapUnacceptableWarnings(unacceptableWarnings: List): List {
return unacceptableWarnings.mapNotNull(::remapPluginProblemLevel)
}
private fun remapErrorsAndWarnings(errorsAndWarnings: List): List {
return errorsAndWarnings.mapNotNull(::remapPluginProblemLevel)
}
private fun remapPluginProblemLevel(pluginProblem: PluginProblem): PluginProblem?{
return when (val remappedLevel = remappedLevel[pluginProblem::class]) {
is StandardLevel -> ReclassifiedPluginProblem(remappedLevel.originalLevel, pluginProblem)
is IgnoredLevel -> null
null -> pluginProblem
}
}
override fun classify(plugin: IdePlugin, problems: List): List {
return problems.mapNotNull {
remapPluginProblemLevel(it)
}
}
private fun List.hasNoErrors(): Boolean = none {
it.level == PluginProblem.Level.ERROR
}
}