All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jetbrains.plugin.structure.intellij.problems.LevelRemappingPluginCreationResultResolver.kt Maven / Gradle / Ivy

Go to download

Library for parsing JetBrains IDE plugins. Can be used to verify that plugin complies with the JetBrains Marketplace requirements.

There is a newer version: 3.290
Show newest version
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.PluginProblem.Level.UNACCEPTABLE_WARNING
import com.jetbrains.plugin.structure.base.problems.PluginProblem.Level.WARNING
import com.jetbrains.plugin.structure.base.problems.ReclassifiedPluginProblem
import com.jetbrains.plugin.structure.base.problems.unwrapped
import com.jetbrains.plugin.structure.intellij.plugin.IdePlugin
import kotlin.reflect.KClass

class LevelRemappingPluginCreationResultResolver(private val delegatedResolver: PluginCreationResultResolver,
                                                 private val remappedLevel: Map, RemappedLevel> = emptyMap(),
                                                 private val unwrapRemappedProblems: Boolean = false
  ) : PluginCreationResultResolver {

  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) {
      val allRemappedProblems = remapWarnings(warnings) + remapUnacceptableWarnings(unacceptableWarnings)
      if (allRemappedProblems.hasNoErrors()) {
        copy(warnings = allRemappedProblems.warnings(), unacceptableWarnings = allRemappedProblems.unacceptableWarnings())
      } else {
        PluginCreationFail(allRemappedProblems)
      }
    }
  }

  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 remapPluginProblemLevel(pluginProblem, unwrapRemappedProblems)
  }

  private fun remapPluginProblemLevel(pluginProblem: PluginProblem, unwrapRemappedProblems: Boolean): PluginProblem? {
    val problem = if (unwrapRemappedProblems) {
      pluginProblem.unwrapped
    } else {
      pluginProblem
    }

    return when (val remappedLevel = remappedLevel[problem::class]) {
      is StandardLevel -> ReclassifiedPluginProblem(remappedLevel.originalLevel, pluginProblem)
      is IgnoredLevel -> null
      null -> pluginProblem
    }
  }

  override fun classify(plugin: IdePlugin, problems: List): List {
    return delegatedResolver.classify(plugin, problems).mapNotNull {
      classify(it.unwrapped)
    }
  }

  private fun classify(pluginProblem: PluginProblem): PluginProblem? {
    return when (val remappedLevel = remappedLevel[pluginProblem::class]) {
      is StandardLevel -> ReclassifiedPluginProblem(remappedLevel.originalLevel, pluginProblem)
      is IgnoredLevel -> null
      null -> pluginProblem
    }
  }

  private fun List.hasNoErrors(): Boolean = none {
    it.level == PluginProblem.Level.ERROR
  }

  private fun List.warnings() = filter { it.level == WARNING }
  private fun List.unacceptableWarnings() = filter { it.level == UNACCEPTABLE_WARNING }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy