Skip to content

Commit

Permalink
#45 react-i18next Trans support: annotation & code to translation ref…
Browse files Browse the repository at this point in the history
…erence
  • Loading branch information
nyavro committed Aug 22, 2020
1 parent 048f3b2 commit 62c2716
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ class ReferencesAccumulator(private val key: String) {
entry: PsiElement, _:Int ->
val typeName = entry.node.elementType.toString()
if (entry.text.unQuote().startsWith(key)) {
if (listOf("JS:STRING_LITERAL", "quoted string").any { item -> typeName.contains(item) }) {
if (listOf("JS:STRING_LITERAL", "quoted string", "JS:STRING_TEMPLATE_EXPRESSION").any { typeName.contains(it) }) {
res.add(entry)
} else if (typeName == "JS:STRING_TEMPLATE_PART") {
res.add(entry.parent)
} else if (typeName == "JS:STRING_TEMPLATE_EXPRESSION") {
res.add(entry)
}
}
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import com.intellij.lang.javascript.psi.JSCallExpression
import com.intellij.lang.javascript.psi.JSLiteralExpression
import com.intellij.openapi.util.TextRange
import com.intellij.patterns.ElementPattern
import com.intellij.patterns.XmlPatterns
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.xml.XmlAttribute

/**
* Vue language components factory
Expand Down Expand Up @@ -56,7 +56,7 @@ internal class JsCallContext: CallContext {
pattern.accepts(element) ||
pattern.accepts(PsiTreeUtil.findFirstParent(element, { it.parent?.type() == "JS:ARGUMENT_LIST" }))
} ||
(element is XmlAttribute) && element.name == "i18nKey"
XmlPatterns.xmlAttributeValue("i18nKey").accepts(element)
}

internal class JsReferenceAssistant: ReferenceAssistant {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class JsxReferenceAssistant: ReferenceAssistant {
private val parser: KeyParser = KeyParser()

override fun pattern(): ElementPattern<out PsiElement> {
return XmlPatterns.xmlAttribute("i18nKey")
return XmlPatterns.xmlAttributeValue("i18nKey")
}
override fun extractKey(element: PsiElement): FullKey? {
val config = Settings.getInstance(element.project).config()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package com.eny.i18n.plugin.parser

import com.eny.i18n.plugin.utils.KeyElement
import com.eny.i18n.plugin.utils.nullableToList
import com.intellij.patterns.XmlPatterns
import com.intellij.psi.PsiElement
import com.intellij.psi.xml.XmlAttribute
import com.intellij.psi.xml.XmlAttributeValue

/**
* Extracts i18n key from js string literal
*/
class XmlAttributeKeyExtractor: KeyExtractor {

override fun canExtract(element: PsiElement): Boolean = (element is XmlAttribute) && element.name == "i18nKey"
override fun canExtract(element: PsiElement): Boolean = XmlPatterns.xmlAttributeValue("i18nKey").accepts(element)

override fun extract(element: PsiElement): Pair<List<KeyElement>, List<String>?> =
Pair(
(element as? XmlAttribute)
(element as? XmlAttributeValue)
?.value
.nullableToList()
.map {KeyElement.literal(it)},
Expand Down

0 comments on commit 62c2716

Please sign in to comment.