@@ -2,11 +2,13 @@ package com.emberjs.gts
22
33import com.dmarcotte.handlebars.file.HbFileViewProvider
44import com.dmarcotte.handlebars.parsing.HbTokenTypes
5+ import com.dmarcotte.handlebars.psi.HbMustache
56import com.dmarcotte.handlebars.psi.HbPsiElement
67import com.dmarcotte.handlebars.psi.HbPsiFile
78import com.emberjs.glint.GlintAnnotationError
89import com.emberjs.glint.GlintTypeScriptService
910import com.emberjs.hbs.HbReference
11+ import com.emberjs.hbs.HbsModuleReference
1012import com.emberjs.utils.ifTrue
1113import com.intellij.codeInspection.ProblemHighlightType
1214import com.intellij.lang.Language
@@ -29,6 +31,7 @@ import com.intellij.openapi.project.Project
2931import com.intellij.openapi.util.TextRange
3032import com.intellij.psi.*
3133import com.intellij.psi.util.elementType
34+ import com.intellij.psi.util.parents
3235import com.intellij.psi.xml.XmlTag
3336import com.intellij.psi.xml.XmlTokenType
3437import com.intellij.refactoring.suggested.endOffset
@@ -160,57 +163,57 @@ class HbLintAnnotator() : Annotator {
160163
161164 override fun annotate (element : PsiElement , holder : AnnotationHolder ) {
162165 val file = element.containingFile
163- if (file.viewProvider !is GtsFileViewProvider && file.viewProvider !is HbFileViewProvider ) {
166+ if (file.viewProvider !is GtsFileViewProvider ) {
164167 return
165168 }
166169 val tsFile = file.viewProvider.getPsi(JavaScriptSupportLoader .TYPESCRIPT )
167- if (tsFile != null ) {
168- if (element is XmlTag && element.reference?.resolve() == null ) {
169- val candidates = getCandidates(file, element.name)
170- val nameElement = element.children.find { it.elementType == XmlTokenType .XML_NAME } ? : return
171- val closeNameElement = element.children.findLast { it.elementType == XmlTokenType .XML_NAME }
172- val message = (((element.name.startsWith(" :" ) || file.viewProvider is HbFileViewProvider )
173- .ifTrue { JavaScriptBundle .message(" javascript.unresolved.symbol.message" , Object ()) + " '${element.name} '" }
174- ? : (JavaScriptBundle .message(" js.inspection.missing.import" , Object ()) + " for '${element.name} '" )))
175- if (closeNameElement != null && closeNameElement.textRange.endOffset == element.endOffset - 1 ) {
176- holder.newSilentAnnotation(HighlightSeverity .ERROR )
177- .range(closeNameElement.textRange)
178- .highlightType(ProblemHighlightType .LIKE_UNKNOWN_SYMBOL )
179- .tooltip(message)
180- .create()
181- }
182- val annotation = holder.newAnnotation(HighlightSeverity .ERROR , message)
183- .range(nameElement.textRange)
170+ if (element is XmlTag && element.reference?.resolve() == null ) {
171+ val candidates = tsFile?.let { getCandidates(file, element.name) }
172+ val nameElement = element.children.find { it.elementType == XmlTokenType .XML_NAME } ? : return
173+ val closeNameElement = element.children.findLast { it.elementType == XmlTokenType .XML_NAME }
174+ val message = (((element.name.startsWith(" :" ) || file.viewProvider is HbFileViewProvider )
175+ .ifTrue { JavaScriptBundle .message(" javascript.unresolved.symbol.message" , Object ()) + " '${element.name} '" }
176+ ? : (JavaScriptBundle .message(" js.inspection.missing.import" , Object ()) + " for '${element.name} '" )))
177+ if (closeNameElement != null && closeNameElement.textRange.endOffset == element.endOffset - 1 ) {
178+ holder.newSilentAnnotation(HighlightSeverity .ERROR )
179+ .range(closeNameElement.textRange)
184180 .highlightType(ProblemHighlightType .LIKE_UNKNOWN_SYMBOL )
185181 .tooltip(message)
186- candidates.forEach { c ->
182+ .create()
183+ }
184+ val annotation = holder.newAnnotation(HighlightSeverity .ERROR , message)
185+ .range(nameElement.textRange)
186+ .highlightType(ProblemHighlightType .LIKE_UNKNOWN_SYMBOL )
187+ .tooltip(message)
188+ candidates?.forEach { c ->
189+ val icwe = JSImportCandidateWithExecutor (c, ES6AddImportExecutor (tsFile))
190+ val fix = GtsImportFix (element, icwe, JSImportModuleFix .HintMode .SINGLE )
191+ annotation.withFix(fix)
192+ }
193+ annotation.needsUpdateOnTyping()
194+ annotation.create()
195+ }
196+ if (element is HbPsiElement && element.elementType == HbTokenTypes .ID && (element.reference?.resolve() == null && element.references.find { it is HbReference || it is HbsModuleReference }?.resolve() == null )) {
197+ val insideImport = element.parents(false ).find { it is HbMustache && it.children.getOrNull(1 )?.text == " import" } != null
198+ if (insideImport) return
199+ val name = element.text
200+ val message = JavaScriptBundle .message(" javascript.unresolved.symbol.message" , Object ()) + " '${name} '"
201+ val candidates = tsFile?.let { getCandidates(element.containingFile, name) }
202+ val annotation = holder.newAnnotation(HighlightSeverity .ERROR , message)
203+ .range(element.textRange)
204+ .highlightType(ProblemHighlightType .LIKE_UNKNOWN_SYMBOL )
205+ .tooltip(message)
206+ val prevSiblingIsSep = element.parent.prevSibling.elementType == HbTokenTypes .SEP ||
207+ element.prevSibling.elementType == HbTokenTypes .SEP
208+ if (! prevSiblingIsSep) {
209+ candidates?.forEach { c ->
187210 val icwe = JSImportCandidateWithExecutor (c, ES6AddImportExecutor (tsFile))
188211 val fix = GtsImportFix (element, icwe, JSImportModuleFix .HintMode .SINGLE )
189212 annotation.withFix(fix)
190213 }
191- annotation.needsUpdateOnTyping()
192- annotation.create()
193- }
194- if (element is HbPsiElement && element.elementType == HbTokenTypes .ID && (element.reference?.resolve() == null && element.references.find { it is HbReference }?.resolve() == null )) {
195- val name = element.text
196- val message = JavaScriptBundle .message(" javascript.unresolved.symbol.message" , Object ()) + " '${name} '"
197- val candidates = getCandidates(element.containingFile, name)
198- val annotation = holder.newAnnotation(HighlightSeverity .ERROR , message)
199- .range(element.textRange)
200- .highlightType(ProblemHighlightType .LIKE_UNKNOWN_SYMBOL )
201- .tooltip(message)
202- val prevSiblingIsSep = element.parent.prevSibling.elementType == HbTokenTypes .SEP ||
203- element.prevSibling.elementType == HbTokenTypes .SEP
204- if (! prevSiblingIsSep) {
205- candidates.forEach { c ->
206- val icwe = JSImportCandidateWithExecutor (c, ES6AddImportExecutor (tsFile))
207- val fix = GtsImportFix (element, icwe, JSImportModuleFix .HintMode .SINGLE )
208- annotation.withFix(fix)
209- }
210- }
211- annotation.needsUpdateOnTyping()
212- annotation.create()
213214 }
215+ annotation.needsUpdateOnTyping()
216+ annotation.create()
214217 }
215218 }
216219}
0 commit comments