11package com.emberjs.utils
22
33
4+ import com.dmarcotte.handlebars.HbLanguage
45import com.dmarcotte.handlebars.parsing.HbTokenTypes
56import com.dmarcotte.handlebars.psi.*
67import com.dmarcotte.handlebars.psi.impl.HbDataImpl
78import com.dmarcotte.handlebars.psi.impl.HbPathImpl
89import com.emberjs.cli.EmberCliFrameworkDetector
10+ import com.emberjs.gts.GtsElementTypes
911import com.emberjs.gts.GtsFile
1012import com.emberjs.gts.GtsFileViewProvider
1113import com.emberjs.hbs.HbReference
@@ -29,7 +31,10 @@ import com.intellij.lang.ecmascript6.psi.ES6ImportSpecifier
2931import com.intellij.lang.ecmascript6.psi.ES6ImportedBinding
3032import com.intellij.lang.ecmascript6.resolve.ES6PsiUtil
3133import com.intellij.lang.injection.InjectedLanguageManager
34+ import com.intellij.lang.javascript.JSLanguageDialect
3235import com.intellij.lang.javascript.JavaScriptSupportLoader
36+ import com.intellij.lang.javascript.JavascriptLanguage
37+ import com.intellij.lang.javascript.dialects.TypeScriptLanguageDialect
3338import com.intellij.lang.javascript.frameworks.modules.JSFileModuleReference
3439import com.intellij.lang.javascript.psi.*
3540import com.intellij.lang.javascript.psi.ecma6.*
@@ -348,10 +353,12 @@ class EmberUtils {
348353 val angleBracketBlock = element.parent as XmlTag
349354 val startIdx = angleBracketBlock.attributes.indexOfFirst { it.text.startsWith(" |" ) }
350355 val endIdx = angleBracketBlock.attributes.size
351- val params = angleBracketBlock.attributes.toList().subList(startIdx, endIdx)
352- val refPsi = params.find { Regex (" \\ |*.*\\ b$name \\ b.*\\ |*" ).matches(it.text) }
353- val blockParamIdx = params.indexOf(refPsi)
354- return followReferences(yieldParams.getOrNull(blockParamIdx), path)
356+ if (startIdx > - 1 && startIdx < endIdx) {
357+ val params = angleBracketBlock.attributes.toList().subList(startIdx, endIdx)
358+ val refPsi = params.find { Regex (" \\ |*.*\\ b$name \\ b.*\\ |*" ).matches(it.text) }
359+ val blockParamIdx = params.indexOf(refPsi)
360+ return followReferences(yieldParams.getOrNull(blockParamIdx), path)
361+ }
355362 }
356363
357364 if (element?.references != null && element.references.isNotEmpty()) {
@@ -450,7 +457,8 @@ class EmberUtils {
450457 return data
451458 }
452459
453- if ((func is Helper || func is Modifier ) && (func as PsiElementDelegate <* >).element as ? JSFunction != null || func?.originalVirtualFile is LightVirtualFile || func?.containingFile is ProjectFile ) {
460+ val ovf = func?.originalVirtualFile
461+ if ((func is Helper || func is Modifier ) && (func as PsiElementDelegate <* >).element as ? JSFunction != null || (ovf is LightVirtualFile && ovf.language is JSLanguageDialect ) || func?.containingFile is ProjectFile ) {
454462 func = ((func as ? PsiElementDelegate <* >)?.element ? : func) as JSFunction
455463 var arrayName: String? = null
456464 var array: JSType ?
@@ -785,6 +793,9 @@ class EmberUtils {
785793 jsTemplate = cls.fields.find { it.name == " layout" } ? : jsTemplate
786794 jsTemplate = jsTemplate ? : cls.fields.find { it.name == " template" }
787795 jsTemplate = followReferences(jsTemplate as PsiElement ? )
796+ jsTemplate = jsTemplate
797+ ? :
798+ PsiTreeUtil .collectElements(cls) { it.elementType == GtsElementTypes .GTS_OUTER_ELEMENT_TYPE && it.parent == cls }.firstOrNull()
788799 }
789800 }
790801
@@ -813,6 +824,13 @@ class EmberUtils {
813824 jsTemplate = PsiFileFactory .getInstance(file.project).createFileFromText(" $name -virtual" , Language .findLanguageByID(" Handlebars" )!! , jsTemplate)
814825 }
815826
827+ var psiRange = template?.textRange
828+
829+ if (jsTemplate is PsiElement && jsTemplate.elementType == GtsElementTypes .GTS_OUTER_ELEMENT_TYPE ) {
830+ template = jsTemplate.containingFile.viewProvider.getPsi(HbLanguage .INSTANCE )
831+ psiRange = jsTemplate.textRange
832+ }
833+
816834 if (dir != null || jsTemplate != null ) {
817835 // co-located
818836 if (name == " component" ) {
@@ -843,16 +861,16 @@ class EmberUtils {
843861 ? : getFileByPath(parentModule, fullPathToHbs.replace(" /components/" , " /templates/components/" ))
844862
845863
846- if (template?.node?.psi != null ) {
847- val args = PsiTreeUtil .collectElementsOfType (template.node.psi, HbDataImpl :: class .java)
864+ if (template?.node?.psi != null && psiRange != null ) {
865+ val args = PsiTreeUtil .collectElements (template.node.psi) { it is HbData && psiRange.contains(it.textRange) }
848866 for (arg in args) {
849867 val argName = arg.text.split(" ." ).first()
850868 if (tplArgs.find { it.value == argName } == null ) {
851869 tplArgs.add(ArgData (argName, " " , AttrPsiReference (arg)))
852870 }
853871 }
854872
855- val yields = PsiTreeUtil .collectElements(template.node.psi, { it is HbPathImpl && it.text == " yield" })
873+ val yields = PsiTreeUtil .collectElements(template.node.psi) { it is HbPathImpl && it.text == " yield" && psiRange.contains(it.textRange) }
856874 for (y in yields) {
857875 tplYields.add(EmberXmlElementDescriptor .YieldReference (y))
858876 }
0 commit comments