Skip to content

Commit 061a6f6

Browse files
authored
reference fix (#27)
1 parent 9a5fa85 commit 061a6f6

22 files changed

+499
-242
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Changelog
44
===============================================================================
5+
## 2023.1.10
6+
- fix some references
7+
58
## 2023.1.9
69
- fix: some gts html tag references were not resolving
710

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ plugins {
1313

1414

1515
group = "com.emberjs"
16-
version = "2023.1.9"
16+
version = "2023.1.10"
1717

1818
// Configure project's dependencies
1919
repositories {

src/main/kotlin/com/emberjs/glint/GlintLanguageService.kt

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.emberjs.glint
22

33
import com.dmarcotte.handlebars.file.HbFileType
4+
import com.dmarcotte.handlebars.psi.HbPsiElement
45
import com.dmarcotte.handlebars.psi.HbPsiFile
56
import com.emberjs.gts.GtsFileType
67
import com.emberjs.hbs.HbReference
@@ -18,7 +19,6 @@ import com.intellij.lang.javascript.completion.JSInsertHandler
1819
import com.intellij.lang.javascript.integration.JSAnnotationError
1920
import com.intellij.lang.javascript.integration.JSAnnotationError.*
2021
import com.intellij.lang.javascript.psi.JSFunctionType
21-
import com.intellij.lang.javascript.service.JSLanguageService
2222
import com.intellij.lang.javascript.service.JSLanguageServiceProvider
2323
import com.intellij.lang.parameterInfo.CreateParameterInfoContext
2424
import com.intellij.lang.typescript.compiler.TypeScriptService
@@ -33,8 +33,6 @@ import com.intellij.lsp.methods.HoverMethod
3333
import com.intellij.openapi.Disposable
3434
import com.intellij.openapi.diagnostic.Logger
3535
import com.intellij.openapi.editor.Document
36-
import com.intellij.openapi.fileEditor.FileDocumentManager
37-
import com.intellij.openapi.fileEditor.FileEditorManager
3836
import com.intellij.openapi.project.Project
3937
import com.intellij.openapi.project.guessProjectDir
4038
import com.intellij.openapi.util.TextRange
@@ -44,6 +42,8 @@ import com.intellij.psi.PsiElement
4442
import com.intellij.psi.PsiFile
4543
import com.intellij.psi.PsiManager
4644
import com.intellij.psi.impl.source.tree.LeafPsiElement
45+
import com.intellij.psi.util.elementType
46+
import com.intellij.psi.xml.XmlElement
4747
import org.eclipse.lsp4j.CompletionItem
4848
import org.eclipse.lsp4j.Diagnostic
4949
import org.eclipse.lsp4j.DiagnosticSeverity
@@ -157,7 +157,14 @@ class GlintTypeScriptService(private val project: Project) : TypeScriptService,
157157
}
158158
}
159159

160-
override fun getNavigationFor(document: Document, sourceElement: PsiElement): Array<PsiElement>? {
160+
override fun getNavigationFor(document: Document, elem: PsiElement): Array<PsiElement>? {
161+
var sourceElement: PsiElement = elem
162+
if (sourceElement is LeafPsiElement) {
163+
sourceElement = sourceElement.parent
164+
}
165+
if (sourceElement is XmlElement || sourceElement is HbPsiElement) {
166+
return null
167+
}
161168
var element = sourceElement.containingFile.originalFile.findElementAt(sourceElement.textOffset) ?: sourceElement
162169
if (currentlyChecking == null && element.containingFile is HbPsiFile) {
163170
currentlyChecking = sourceElement
@@ -233,28 +240,12 @@ class GlintTypeScriptService(private val project: Project) : TypeScriptService,
233240
override fun highlight(file: PsiFile): CompletableFuture<List<JSAnnotationError>>? {
234241
val server = getDescriptor()?.server ?: return completedFuture(emptyList())
235242
val virtualFile = file.virtualFile
236-
val changedUnsaved = collectChangedUnsavedFiles()
237-
if (changedUnsaved.isNotEmpty()) {
238-
JSLanguageService.saveChangedFilesAndRestartHighlighting(file, changedUnsaved)
239-
return null
240-
}
241243

242244
return completedFuture(server.getDiagnostics(virtualFile)?.map {
243245
GlintAnnotationError(it, virtualFile.canonicalPath)
244246
})
245247
}
246248

247-
private fun collectChangedUnsavedFiles(): Collection<VirtualFile> {
248-
val manager = FileDocumentManager.getInstance()
249-
val openFiles = setOf(*FileEditorManager.getInstance(project).openFiles)
250-
val unsavedDocuments = manager.unsavedDocuments
251-
if (unsavedDocuments.isEmpty()) return emptyList()
252-
253-
return unsavedDocuments
254-
.mapNotNull { manager.getFile(it) }
255-
.filter { vFile -> !openFiles.contains(vFile) && isAcceptable(vFile) }
256-
}
257-
258249
override fun canHighlight(file: PsiFile) = file.fileType is HbFileType ||
259250
file.fileType is TypeScriptFileType ||
260251
file.fileType is GtsFileType ||

src/main/kotlin/com/emberjs/glint/GlintLspSupportProvider.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.intellij.lsp.api.LspServerManager
1818
import com.intellij.lsp.api.LspServerSupportProvider
1919
import com.intellij.openapi.Disposable
2020
import com.intellij.openapi.application.ApplicationManager
21+
import com.intellij.openapi.application.ex.ApplicationInfoEx
2122
import com.intellij.openapi.components.Service
2223
import com.intellij.openapi.project.Project
2324
import com.intellij.openapi.project.guessProjectDir
@@ -43,7 +44,10 @@ class GlintLanguageServerConnectorStdio(server: LspServer, processHandler: OSPro
4344
if (!path.startsWith("/")) {
4445
path = "/$path"
4546
}
46-
return URLEncoder.encode(path, "utf-8").replace("%2F", "/")
47+
return URLEncoder.encode(path, "utf-8")
48+
.replace("%2F", "/")
49+
.replace("%253A", ":")
50+
.replace("%3A", ":")
4751
}
4852

4953
override fun initializeServer() {
@@ -84,7 +88,7 @@ class GlintLspServerDescriptor(private val myProject: Project) : LspServerDescri
8488
?: throw RuntimeException("glint is not installed")
8589
val file = glintPkg.findFileByRelativePath("bin/glint-language-server.js")
8690
?: throw RuntimeException("glint lsp was not found")
87-
// commandLine.addParameter("--inspect")
91+
commandLine.addParameter("--inspect")
8892
commandLine.addParameter(file.path)
8993
commandLine.addParameter("--stdio")
9094
commandLine.addParameter("--clientProcessId=" + OSProcessUtil.getCurrentProcessId().toString())
@@ -122,7 +126,7 @@ class GlintLspServerDescriptor(private val myProject: Project) : LspServerDescri
122126
file.fileType is JavaScriptFileType
123127
}
124128

125-
override val handlePublishDiagnostics = true
129+
override val handlePublishDiagnostics = ApplicationInfoEx.getInstanceEx().fullVersion == "2023.1"
126130
override val useGenericNavigation = false
127131
override val lspCompletionSupport = null
128132

src/main/kotlin/com/emberjs/gts/GtsSupport.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ class GtsFormattingModelBuilder : AbstractXmlTemplateFormattingModelBuilder() {
771771
}, (formattingContext.node as OuterLanguageElement).project, formattingContext.getCodeStyleSettings(), formattingContext.containingFile.fileType, formattingContext.containingFile)
772772
}
773773

774-
var element = formattingContext.psiElement.containingFile.findElementAt(formattingContext.formattingRange.startOffset)!!
774+
var element = formattingContext.psiElement.containingFile.findElementAt(formattingContext.formattingRange.startOffset) ?: formattingContext.psiElement
775775
if (formattingContext.psiElement is PsiFile && formattingContext.formattingRange.startOffset == 0) {
776776
element = formattingContext.containingFile.viewProvider.getPsi(TS)
777777
}

src/main/kotlin/com/emberjs/gts/HbLinter.kt

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package com.emberjs.gts
22

33
import com.dmarcotte.handlebars.file.HbFileViewProvider
44
import com.dmarcotte.handlebars.parsing.HbTokenTypes
5+
import com.dmarcotte.handlebars.psi.HbMustache
56
import com.dmarcotte.handlebars.psi.HbPsiElement
67
import com.dmarcotte.handlebars.psi.HbPsiFile
78
import com.emberjs.glint.GlintAnnotationError
89
import com.emberjs.glint.GlintTypeScriptService
910
import com.emberjs.hbs.HbReference
11+
import com.emberjs.hbs.HbsModuleReference
1012
import com.emberjs.utils.ifTrue
1113
import com.intellij.codeInspection.ProblemHighlightType
1214
import com.intellij.lang.Language
@@ -29,6 +31,7 @@ import com.intellij.openapi.project.Project
2931
import com.intellij.openapi.util.TextRange
3032
import com.intellij.psi.*
3133
import com.intellij.psi.util.elementType
34+
import com.intellij.psi.util.parents
3235
import com.intellij.psi.xml.XmlTag
3336
import com.intellij.psi.xml.XmlTokenType
3437
import 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
}

src/main/kotlin/com/emberjs/hbs/HbReference.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.emberjs.hbs
22

33
import com.dmarcotte.handlebars.psi.impl.HbStatementsImpl
4-
import com.emberjs.xml.EmberAttrDec
54
import com.emberjs.psi.EmberNamedAttribute
65
import com.emberjs.psi.EmberNamedElement
76
import com.emberjs.refactoring.SimpleNodeFactory
7+
import com.emberjs.xml.EmberAttrDec
88
import com.intellij.lang.Language
99
import com.intellij.openapi.util.TextRange
1010
import com.intellij.psi.PsiElement
@@ -19,14 +19,13 @@ abstract class HbReference(element: PsiElement): PsiReferenceBase<PsiElement>(el
1919
override fun isReferenceTo(other: PsiElement): Boolean {
2020
var res = resolve()
2121
if (res is EmberNamedElement) {
22-
res = res.target
22+
res = res.target
2323
}
2424
return element.manager.areElementsEquivalent(res, other) || super.isReferenceTo(other)
2525
}
2626
}
2727

28-
29-
class RangedReference(element: PsiElement, val targetPsi: PsiElement?, val range: TextRange) : HbReference(element) {
28+
open class RangedReference(element: PsiElement, val targetPsi: PsiElement?, val range: TextRange) : HbReference(element) {
3029
private var targetRef: PsiReference? = null
3130
constructor(element: PsiElement, targetRef: PsiReference, range: TextRange) : this(element, null, range) {
3231
this.targetRef = targetRef
@@ -84,6 +83,8 @@ class RangedReference(element: PsiElement, val targetPsi: PsiElement?, val range
8483
}
8584

8685

86+
class ImportNameReference(element: PsiElement, psiElement: PsiElement?, textRange: TextRange): RangedReference(element, psiElement, textRange)
87+
8788

8889
class HbsLocalRenameReference(private val leaf: PsiElement, val target: PsiElement?) : HbReference(leaf) {
8990
val named = target?.let { EmberNamedElement(it) }

src/main/kotlin/com/emberjs/hbs/HbsLocalReference.kt

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,6 @@ import com.intellij.psi.xml.XmlTag
4242
import com.intellij.refactoring.suggested.startOffset
4343
import kotlin.math.max
4444

45-
class ImportNameReferences(element: PsiElement) : PsiPolyVariantReferenceBase<PsiElement>(element, TextRange(0, element.textLength), true) {
46-
override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
47-
val names = element.text.split(",")
48-
val named = names.map {
49-
if (it.contains(" as ")) {
50-
it.split(" as ").first()
51-
} else {
52-
it
53-
}
54-
}
55-
val mustache = element.parents.find { it is HbMustache }!!
56-
val path = mustache.children.findLast { it is HbParam }
57-
val fileRef = path?.references?.firstOrNull()?.resolve()
58-
if (fileRef is PsiDirectory) {
59-
return named
60-
.map { fileRef.findFile(it) ?: fileRef.findSubdirectory(it) }
61-
.filterNotNull()
62-
.map { PsiElementResolveResult(it) }
63-
.toTypedArray()
64-
}
65-
if (fileRef == null) {
66-
return emptyArray()
67-
}
68-
val ref = EmberUtils.resolveToEmber(fileRef)
69-
return ref?.let { arrayOf(PsiElementResolveResult(it)) } ?: arrayOf()
70-
}
71-
}
72-
7345

7446
class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbReference(leaf) {
7547
private var namedXml: EmberNamedAttribute? = null
@@ -239,8 +211,8 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
239211
}
240212

241213
val refYield = EmberUtils.findTagYieldAttribute(any)
242-
if (refYield != null && refYield.descriptor?.declaration != null) {
243-
return resolveToJs(refYield.descriptor?.declaration, path, resolveIncomplete, recursionCounter + 1)
214+
if (refYield != null && refYield.declaration != null) {
215+
return resolveToJs(refYield.declaration, path, resolveIncomplete, recursionCounter + 1)
244216
}
245217
}
246218

@@ -388,6 +360,13 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
388360

389361
val name = element.text.replace("IntellijIdeaRulezzz", "")
390362

363+
val closeMustache = PsiTreeUtil.collectParents(element, HbCloseBlockMustache::class.java, false) { it is HbBlockWrapper }.firstOrNull()
364+
if (closeMustache != null) {
365+
val blockWrapper = closeMustache.parent
366+
val openId = PsiTreeUtil.collectElements(blockWrapper) { HbsPatterns.BLOCK_MUSTACHE_NAME_ID.accepts(it) }.firstOrNull()
367+
return openId?.reference ?: openId?.references?.firstOrNull()
368+
}
369+
391370
val sibling = PsiTreeUtil.findSiblingBackward(element, HbTokenTypes.ID, null)
392371
if (name == "this" && sibling == null) {
393372
val cls = EmberUtils.findBackingJsClass(element)

0 commit comments

Comments
 (0)