-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved argument injections handling
- Loading branch information
Showing
5 changed files
with
204 additions
and
36 deletions.
There are no files selected for viewing
72 changes: 59 additions & 13 deletions
72
src/main/kotlin/com/intellij/StyledComponents/InjectionUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,86 @@ | ||
package com.intellij.styledComponents | ||
|
||
import com.intellij.lang.javascript.JSTokenTypes | ||
import com.intellij.lang.javascript.psi.JSExpression | ||
import com.intellij.lang.javascript.psi.ecma6.JSStringTemplateExpression | ||
import com.intellij.openapi.util.Key | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.openapi.util.registry.Registry | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiLanguageInjectionHost | ||
import com.intellij.psi.PsiRecursiveElementWalkingVisitor | ||
import com.intellij.util.ArrayUtil | ||
import com.intellij.util.containers.ContainerUtil | ||
import java.util.* | ||
import kotlin.math.max | ||
|
||
private const val EXTERNAL_FRAGMENT = "EXTERNAL_FRAGMENT" | ||
private const val EXPERIMENTAL_INJECTIONS = "styled.components.experimental.injections" | ||
|
||
fun getInjectionPlaces(quotedLiteral: PsiElement): List<StringPlace> { | ||
if (quotedLiteral is JSStringTemplateExpression) { | ||
val ranges = quotedLiteral.stringRanges | ||
val arguments = quotedLiteral.arguments | ||
|
||
fun getInjectionPlaces(myQuotedLiteral: PsiElement): List<StringPlace> { | ||
if (myQuotedLiteral is JSStringTemplateExpression) { | ||
val templateExpression = myQuotedLiteral | ||
val ranges = templateExpression.stringRanges | ||
if (ranges.isNotEmpty()) { | ||
val result = ArrayList<StringPlace>(ranges.size) | ||
val quotedLiteralNode = myQuotedLiteral.getNode() | ||
val backquote = quotedLiteralNode.findChildByType(JSTokenTypes.BACKQUOTE) | ||
val backquoteOffset = if (backquote != null) backquote.startOffset - quotedLiteralNode.startOffset else -1 | ||
var lastArgumentIndex = -1 | ||
|
||
for (i in ranges.indices) { | ||
val range = ranges[i] | ||
val prefix = if (i == 0 && range.startOffset > backquoteOffset + 1) EXTERNAL_FRAGMENT else null | ||
val suffix = if (i < ranges.size - 1 || range.endOffset < myQuotedLiteral.getTextLength() - 1) EXTERNAL_FRAGMENT else null | ||
|
||
// styled.div`padding: ${'none'}${'IGNORED_ARGUMENT'}${'display'}: none;` | ||
// |_________|_______|____________________|___________|______| | ||
// Text Suffix Ignored Prefix Text | ||
// |__________________| |__________________| | ||
// 1 fragment 2 fragment | ||
|
||
var currentIndex = adjustPrecedingArgumentIndex(lastArgumentIndex, range, arguments) | ||
val prefix = if (currentIndex != lastArgumentIndex) | ||
getArgumentPlaceholder(arguments.elementAtOrNull(currentIndex)) else null | ||
|
||
val suffix = getArgumentPlaceholder(arguments.elementAtOrNull(++currentIndex)) | ||
|
||
lastArgumentIndex = currentIndex | ||
result.add(StringPlace(prefix, range, suffix)) | ||
} | ||
return result | ||
} | ||
if (!ArrayUtil.isEmpty(templateExpression.arguments)) { | ||
if (!ArrayUtil.isEmpty(arguments)) { | ||
return ContainerUtil.emptyList() | ||
} | ||
} | ||
val endOffset = Math.max(myQuotedLiteral.textLength - 1, 1) | ||
|
||
val endOffset = max(quotedLiteral.textLength - 1, 1) | ||
return listOf(StringPlace(null, TextRange.create(1, endOffset), null)) | ||
} | ||
|
||
|
||
private fun adjustPrecedingArgumentIndex(startIndex: Int, range: TextRange, arguments: Array<JSExpression>): Int { | ||
var current = startIndex | ||
|
||
while (true) { | ||
val argument = arguments.getOrNull(current + 1) | ||
if (argument == null || argument.textRangeInParent.startOffset > range.endOffset) return current | ||
current++ | ||
} | ||
} | ||
|
||
private fun getArgumentPlaceholder(argumentExpression: JSExpression?): String? { | ||
if (argumentExpression == null) return null | ||
else if (!Registry.`is`(EXPERIMENTAL_INJECTIONS)) return EXTERNAL_FRAGMENT | ||
|
||
var hasChildInjection = false | ||
argumentExpression.accept(object : PsiRecursiveElementWalkingVisitor() { | ||
override fun visitElement(element: PsiElement?) { | ||
if (element is PsiLanguageInjectionHost && StyledComponentsInjector.matchInjectionTarget(element) != null) { | ||
hasChildInjection = true | ||
stopWalking() | ||
} | ||
|
||
super.visitElement(element) | ||
} | ||
}) | ||
|
||
return if (hasChildInjection) null else EXTERNAL_FRAGMENT | ||
} | ||
|
||
data class StringPlace(val prefix: String?, val range: TextRange, val suffix: String?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters