diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/UrlSelector.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/UrlSelector.kt index 58068785..70ed67e7 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/UrlSelector.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/UrlSelector.kt @@ -4,54 +4,46 @@ import com.google.inject.Inject import com.google.inject.Singleton import com.itangcent.common.model.Request import com.itangcent.common.model.URL -import com.itangcent.common.utils.longest -import com.itangcent.common.utils.shortest import com.itangcent.idea.plugin.api.export.core.ClassExportRuleKeys import com.itangcent.idea.plugin.api.export.core.ResolveMultiPath import com.itangcent.idea.psi.resource import com.itangcent.intellij.config.rule.RuleComputer +/** + * The `UrlSelector` class is responsible for selecting a single URL from a given request. + * When a request contains a single URL, it is returned as-is. However, when the request contains + * multiple URLs, a resolution strategy is determined based on specified rules, and this strategy is + * used to resolve the multiple URLs to a single URL. + * + * The resolution strategy is determined based on [ClassExportRuleKeys.PATH_MULTI], and the default + * strategy is [ResolveMultiPath.FIRST]. + */ @Singleton class UrlSelector { @Inject private val ruleComputer: RuleComputer? = null + /** + * Selects a URL from a given request + * + * @param request The request containing the path (URL or URLs) to be resolved. + * @return The selected URL after applying the resolution strategy. + */ fun selectUrls(request: Request): URL { val pathInRequest = request.path ?: return URL.nil() + // If there's only one URL, return it if (pathInRequest.single()) { return pathInRequest } + // Determine the resolution strategy based on a rule, defaulting to FIRST if the rule is not set val pathMultiResolve = ruleComputer!!.computer(ClassExportRuleKeys.PATH_MULTI, request.resource()!!)?.let { ResolveMultiPath.valueOf(it.uppercase()) } ?: ResolveMultiPath.FIRST - when (pathMultiResolve) { - ResolveMultiPath.ALL -> { - return pathInRequest - } - - ResolveMultiPath.FIRST -> { - return URL.of(pathInRequest.urls().firstOrNull()) - } - - ResolveMultiPath.LAST -> { - return URL.of(pathInRequest.urls().lastOrNull()) - } - - ResolveMultiPath.LONGEST -> { - return URL.of(pathInRequest.urls().longest()) - } - - ResolveMultiPath.SHORTEST -> { - return URL.of(pathInRequest.urls().shortest()) - } - - else -> { - return pathInRequest - } - } + // Resolve and return the URL based on the determined strategy + return pathMultiResolve.resolve(pathInRequest) } } \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/core/ResolveMultiPath.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/core/ResolveMultiPath.kt index c84608a7..5426c86a 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/core/ResolveMultiPath.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/api/export/core/ResolveMultiPath.kt @@ -1,9 +1,33 @@ package com.itangcent.idea.plugin.api.export.core +import com.itangcent.common.model.URL +import com.itangcent.common.utils.longest +import com.itangcent.common.utils.shortest + +/** + * This enumeration defines strategies to resolve multiple URLs to a single URL, + * based on different criteria like the first URL, last URL, longest URL, or shortest URL. + * It also provides a strategy to return all URLs as-is without any resolution. + */ enum class ResolveMultiPath { - FIRST, - LAST, - LONGEST, - SHORTEST, - ALL + FIRST { + override fun resolve(url: URL): URL = URL.of(url.urls().firstOrNull()) + }, + LAST { + override fun resolve(url: URL): URL = URL.of(url.urls().lastOrNull()) + }, + LONGEST { + override fun resolve(url: URL): URL = URL.of(url.urls().longest()) + }, + SHORTEST { + override fun resolve(url: URL): URL = URL.of(url.urls().shortest()) + }, + ALL { + override fun resolve(url: URL): URL = url + }; + + /** + * resolve the given URL based on the implemented strategy + */ + abstract fun resolve(url: URL): URL } \ No newline at end of file