Skip to content

Commit

Permalink
Last fixes?
Browse files Browse the repository at this point in the history
- Fix another read action issue
- Fix cssVariables setting never used for React
- Fix cssVariables being misused for Vue
- Fix imports replacements for Svelte
  • Loading branch information
WarningImHack3r committed May 18, 2024
1 parent c2dd583 commit c799dae
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ReactConfig(
override val config: String,
override val css: String,
override val baseColor: String,
private val cssVariables: Boolean = true,
val cssVariables: Boolean = true,
val prefix: String = ""
) : Config.Tailwind()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ class ReactSource(project: Project) : Source<ReactConfig>(project, ReactConfig.s
val modifier = if (`class`.contains(":")) `class`.substringBeforeLast(":") + ":" else ""
val className = `class`.substringAfterLast(":")
val twPrefix = config.tailwind.prefix
if (config.tailwind.cssVariables) {
return@replacer "$modifier$twPrefix$className"
}
if (className == "border") {
return@replacer "${modifier}${twPrefix}border ${modifier}${twPrefix}border-border"
return@replacer "$modifier${twPrefix}border $modifier${twPrefix}border-border"
}
val prefix = prefixesToReplace.find { className.startsWith(it) }
?: return@replacer "$modifier$twPrefix$className"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class SvelteSource(project: Project) : Source<SvelteConfig>(project, SvelteConfi
runReadAction { file.accept(importsPackagesReplacementVisitor) }
importsPackagesReplacementVisitor.replaceImports { `package` ->
`package`
.replace(Regex("\\\$lib/registry/[^/]+"), config.aliases.components)
.replace("\$lib/utils", config.aliases.components)
.replace(Regex("^${"$"}lib/registry/[^/]+"), config.aliases.components)

Check warning on line 93 in src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/impl/SvelteSource.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Begin or end anchor in unexpected position

Anchor `$` in unexpected position
.replace("\$lib/utils", config.aliases.utils)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,36 +100,37 @@ class VueSource(project: Project) : Source<VueConfig>(project, VueConfig.seriali
`package`
}

if (!config.tailwind.cssVariables) {
val prefixesToReplace = listOf("bg-", "text-", "border-", "ring-offset-", "ring-")
val prefixesToReplace = listOf("bg-", "text-", "border-", "ring-offset-", "ring-")

val inlineColors = fetchColors().jsonObject["inlineColors"]?.jsonObject
?: throw Exception("Inline colors not found")
val lightColors = inlineColors.jsonObject["light"]?.jsonObject?.let { lightColors ->
lightColors.keys.associateWith { lightColors[it]?.jsonPrimitive?.content ?: "" }
} ?: emptyMap()
val darkColors = inlineColors.jsonObject["dark"]?.jsonObject?.let { darkColors ->
darkColors.keys.associateWith { darkColors[it]?.jsonPrimitive?.content ?: "" }
} ?: emptyMap()
val inlineColors = fetchColors().jsonObject["inlineColors"]?.jsonObject
?: throw Exception("Inline colors not found")
val lightColors = inlineColors.jsonObject["light"]?.jsonObject?.let { lightColors ->
lightColors.keys.associateWith { lightColors[it]?.jsonPrimitive?.content ?: "" }
} ?: emptyMap()
val darkColors = inlineColors.jsonObject["dark"]?.jsonObject?.let { darkColors ->
darkColors.keys.associateWith { darkColors[it]?.jsonPrimitive?.content ?: "" }
} ?: emptyMap()

val classReplacementVisitor = VueClassReplacementVisitor(project)
runReadAction { file.accept(classReplacementVisitor) }
classReplacementVisitor.replaceClasses replacer@{ `class` ->
val modifier = if (`class`.contains(":")) `class`.substringBeforeLast(":") + ":" else ""
val className = `class`.substringAfterLast(":")
val twPrefix = config.tailwind.prefix
if (className == "border") {
return@replacer "${modifier}${twPrefix}border ${modifier}${twPrefix}border-border"
}
val prefix = prefixesToReplace.find { className.startsWith(it) }
?: return@replacer "$modifier$twPrefix$className"
val color = className.substringAfter(prefix)
val lightColor = lightColors[color]
val darkColor = darkColors[color]
if (lightColor != null && darkColor != null) {
"$modifier$twPrefix$prefix$lightColor dark:$modifier$twPrefix$prefix$darkColor"
} else "$modifier$twPrefix$className"
val classReplacementVisitor = VueClassReplacementVisitor(project)
runReadAction { file.accept(classReplacementVisitor) }
classReplacementVisitor.replaceClasses replacer@{ `class` ->
val modifier = if (`class`.contains(":")) `class`.substringBeforeLast(":") + ":" else ""
val className = `class`.substringAfterLast(":")
val twPrefix = config.tailwind.prefix
if (config.tailwind.cssVariables) {
return@replacer "$modifier$twPrefix$className"
}
if (className == "border") {
return@replacer "$modifier${twPrefix}border $modifier${twPrefix}border-border"
}
val prefix = prefixesToReplace.find { className.startsWith(it) }
?: return@replacer "$modifier$twPrefix$className"
val color = className.substringAfter(prefix)
val lightColor = lightColors[color]
val darkColor = darkColors[color]
if (lightColor != null && darkColor != null) {
"$modifier$twPrefix$prefix$lightColor dark:$modifier$twPrefix$prefix$darkColor"
} else "$modifier$twPrefix$className"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ abstract class ClassReplacementVisitor(project: Project) : PsiRecursiveElementVi
}

private fun replaceClassName(element: PsiElement, newText: (String) -> String) {
val quote = when (element.text.first()) {
'\'', '`', '"' -> element.text.first().toString()
val text = runReadAction { element.text }
val quote = when (text.first()) {
'\'', '`', '"' -> text.first().toString()
else -> ""
}
val classes = element.text
val classes = text
.split(" ")
.filter { it.isNotEmpty() }
.joinToString(" ") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ class ImportsPackagesReplacementVisitor(project: Project) : PsiRecursiveElementV
}

private fun replaceImport(element: PsiElement, newText: (String) -> String) {
val quote = when (element.text.first()) {
'\'', '`', '"' -> element.text.first().toString()
val text = runReadAction { element.text }
val quote = when (text.first()) {
'\'', '`', '"' -> text.first().toString()
else -> ""
}
val newImport = element.text.let {
val newImport = text.let {
if (quote.isEmpty()) {
// Cannot happen, but just in case
newText(it)
Expand Down

0 comments on commit c799dae

Please sign in to comment.