Skip to content

Commit

Permalink
feature: better double variant support for dark: (#69)
Browse files Browse the repository at this point in the history
* feature: better double variant support for dark:

* update: changelog and version
  • Loading branch information
WalrusSoup authored Feb 16, 2023
1 parent 9b51cc8 commit 1a7853e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

## [Unreleased]

## [2.0.4]
## [2.0.5]
### Added
- Performance enhancement: tasks now run in background threads with full progress indication
- Improved handling of double variants when using dark: such as `dark:hover:bg-red-500`
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pluginGroup = com.github.walrussoup.tailwindformatternext
pluginName = tailwind-formatter-next
# SemVer format -> https://semver.org
pluginVersion = 2.0.4
pluginVersion = 2.0.5

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 213
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ class TailwindSorter(classOrder: List<String>, isCustomConfiguration: Boolean) :
if(variantApplied.contains("[")) {
return (variantsStartAt + variantOrder.size * classOrder.size + calculateProperOrder(classApplied));
}
// split by : to see if there are two variants on the left
if (variantApplied.split(":").count() >= 2) {
// if there are two variants, split it by two and add them together to calculate the proper, newly formed offset of both variants
// This is 100% a guess where they should naturally be located
val firstVariant = variantApplied.split(":")[0]
val secondVariant = variantApplied.split(":")[1]
return (variantsStartAt + (variantOrder.indexOf(firstVariant) + variantOrder.indexOf(secondVariant)) * classOrder.size + calculateProperOrder(classApplied))
}
// if its a variant with an existing order in the spec, keep that
return (variantsStartAt + variantOrder.indexOf(variantApplied) * classOrder.size + calculateProperOrder(classApplied))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,18 @@ class SorterTest : BasePlatformTestCase() {
assertEquals(expectedOrder, it)
}
}

fun testCanSortDoubleVariants() {
val outOfOrderVariants = "text-red-500 dark:disabled:bg-red-200 dark:hover:bg-red-500 font-semibold font-serif dark:font-thin dark:text-red-200 bg-red-500";
val expectedOrder = "bg-red-500 font-serif font-semibold text-red-500 dark:font-thin dark:text-red-200 dark:hover:bg-red-500 dark:disabled:bg-red-200";

val utility = TailwindUtility();
utility.loadDefaultClassOrder()

val sorter = TailwindSorter(utility.classOrder, false)
val classes: List<String> = outOfOrderVariants.split(" ").toList();
classes.sortedWith(sorter).joinToString(" ").let {
assertEquals(expectedOrder, it)
}
}
}

0 comments on commit 1a7853e

Please sign in to comment.