Skip to content

Commit

Permalink
Refactor attribute parsing and merging methods in plugin (#55) (#56)
Browse files Browse the repository at this point in the history
This commit improves the handling of attributes within the JetBrains package search plugin. Changes include renaming the `buildAttributesFromRawStrings` function to `parseAttributesFromRawStrings`, and refactoring its implementation for better readability. Additionally, a new `mergeAttributes` function has been added to handle cases where attributes need to be combined, improving code reusability and clarity.

Co-authored-by: Lamberto Basti <[email protected]>
  • Loading branch information
fscarponi and lamba92 authored Feb 6, 2024
1 parent a312e73 commit b788f59
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ interface PackageSearchModuleVariant : PackageSearchDependencyManager {
fun isCompatible(dependency: ApiPackage, version: ApiPackageVersion): Boolean

}

Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,30 @@ fun Set<MppCompilationInfoModel.Compilation>.buildAttributes(): List<PackageSear
}
}.toSet()

return buildAttributesFromRawStrings(rawStrings)
return rawStrings.parseAttributesFromRawStrings()
}

fun buildAttributesFromRawStrings(rawStrings: Set<String>) = buildList {
var queue = rawStrings.toList()

internal fun PackageSearchModuleVariant.Attribute.flatAttributesNames(): Set<String> {
return when (this) {
is PackageSearchModuleVariant.Attribute.StringAttribute -> setOf(value)
is PackageSearchModuleVariant.Attribute.NestedAttribute -> children.flatMap { it.flatAttributesNames() }.toSet()
}
}

fun List<PackageSearchModuleVariant.Attribute>.mergeAttributes() =
flatMap { it.flatAttributesNames() }
.toSet()
.parseAttributesFromRawStrings()

fun Set<String>.parseAttributesFromRawStrings() = buildList {
var queue = this@parseAttributesFromRawStrings.toList()
for (attributeTitle in KMP_ATTRIBUTES_GROUPS) {
val (targets, rest) = queue
.partition { it.contains(attributeTitle.aggregationKeyword, true) }

if (targets.isEmpty()) continue
add(
this.add(
PackageSearchModuleVariant.Attribute.NestedAttribute(
attributeTitle.displayName,
targets.map { PackageSearchModuleVariant.Attribute.StringAttribute(it) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.jetbrains.packagesearch.plugin.core.data.PackageSearchDeclaredPackage
import com.jetbrains.packagesearch.plugin.core.data.listKMPAttributesNames
import com.jetbrains.packagesearch.plugin.core.extensions.PackageSearchKnownRepositoriesContext
import com.jetbrains.packagesearch.plugin.core.utils.icon
import com.jetbrains.packagesearch.plugin.gradle.buildAttributesFromRawStrings
import com.jetbrains.packagesearch.plugin.gradle.parseAttributesFromRawStrings
import com.jetbrains.packagesearch.plugin.ui.model.getLatestVersion
import org.jetbrains.packagesearch.api.v3.ApiMavenPackage
import org.jetbrains.packagesearch.api.v3.ApiPackage
Expand Down Expand Up @@ -218,7 +218,7 @@ private fun MutableList<InfoPanelContent>.addAttributesFromNames(
if (attributesNames.isNotEmpty()) add(
InfoPanelContent.Attributes.FromPackage(
tabTitleData = InfoPanelContent.TabTitleData(tabTitle = message("packagesearch.ui.toolwindow.sidepanel.platforms")),
attributes = buildAttributesFromRawStrings(attributesNames)
attributes = attributesNames.parseAttributesFromRawStrings()
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ sealed interface PackageListItemEvent {
) : OnHeaderAttributesClick

@Serializable
data class SearchHeaderAttributesClick(
override val eventId: PackageListItem.Header.Id.Remote,
data class SearchHeaderWithVariantsAttributesClick(
override val eventId: PackageListItem.Header.Id.Remote.WithVariant,
val attributesNames: List<String>,
) : OnHeaderAttributesClick

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,19 +629,23 @@ class PackageListViewModel(private val project: Project) : Disposable {
infoPanelViewModel.setDeclaredHeaderAttributes(event.variantName, attributes = attributes)
}

is PackageListItemEvent.InfoPanelEvent.OnHeaderAttributesClick.SearchHeaderAttributesClick -> {
val attributes = module
.variants
.map { it.value.attributes }
.flatten()
.filter { it.value in event.attributesNames }
.distinct()
is PackageListItemEvent.InfoPanelEvent.OnHeaderAttributesClick.SearchHeaderWithVariantsAttributesClick -> {

val variants = module.variants.values.map { it.name } - module.mainVariantName
val variants = event.eventId
.compatibleVariantNames
.mapNotNull { module.variants[it] }

val defaultVariant = variants.firstOrNull { it.isPrimary }?.name
?: variants.firstOrNull()?.name
?: return

val attributes = variants.firstOrNull()
?.attributes
?: return

infoPanelViewModel.setSearchHeaderAttributes(
defaultVariant = module.mainVariantName,
additionalVariants = variants,
defaultVariant = defaultVariant,
additionalVariants = variants.map { it.name } - defaultVariant,
attributes = attributes
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ fun PackageListHeader(
.onClick {
val event =
when (content.id) {
is PackageListItem.Header.Id.Declared.Base -> return@onClick
is PackageListItem.Header.Id.Remote -> PackageListItemEvent.InfoPanelEvent.OnHeaderAttributesClick.SearchHeaderAttributesClick(
is PackageListItem.Header.Id.Declared.Base, is PackageListItem.Header.Id.Remote.Base -> return@onClick
is PackageListItem.Header.Id.Remote.WithVariant -> PackageListItemEvent.InfoPanelEvent.OnHeaderAttributesClick.SearchHeaderWithVariantsAttributesClick(
eventId = content.id,
attributesNames = content.attributes
)
Expand Down

0 comments on commit b788f59

Please sign in to comment.