Skip to content

Commit fb57e40

Browse files
authored
improvements (#28)
- fix: fix more gts referencing issues - fix: internal helpers references - fix: remove duplicate eslint messages for gts files - feature: resolve ember proxies
1 parent f38f5f1 commit fb57e40

File tree

10 files changed

+185
-56
lines changed

10 files changed

+185
-56
lines changed

CHANGELOG.md

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

33
Changelog
44
===============================================================================
5+
## 2023.1.18
6+
- fix: fix more gts referencing issues
7+
- fix: internal helpers references
8+
- fix: remove duplicate eslint messages for gts files
9+
- feature: resolve ember proxies
10+
511
## 2023.1.17
612
- fix: gts formatting template without class
713

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.17"
16+
version = "2023.1.18"
1717

1818
// Configure project's dependencies
1919
repositories {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.emberjs.gts
2+
3+
import com.intellij.lang.javascript.JavaScriptSupportLoader
4+
import com.intellij.lang.javascript.linter.JSLinterInput
5+
import com.intellij.lang.javascript.linter.eslint.EslintExternalAnnotator
6+
import com.intellij.lang.javascript.linter.eslint.EslintState
7+
import com.intellij.openapi.editor.colors.EditorColorsScheme
8+
import com.intellij.openapi.vfs.VirtualFile
9+
import com.intellij.psi.PsiFile
10+
11+
12+
class FakeVirtualVile(val virtualFile: VirtualFile): VirtualFile() {
13+
14+
override fun isInLocalFileSystem() = true
15+
override fun getExtension() = "ts"
16+
override fun getName() = virtualFile.name
17+
override fun getFileSystem() = virtualFile.fileSystem
18+
override fun getPath() = virtualFile.path.replace(".gts", ".ts")
19+
override fun isWritable() = virtualFile.isWritable
20+
override fun isDirectory() = virtualFile.isDirectory
21+
override fun isValid() = virtualFile.isValid
22+
override fun getParent() = virtualFile.parent
23+
override fun getChildren() = virtualFile.children
24+
override fun getOutputStream(requestor: Any?, newModificationStamp: Long, newTimeStamp: Long) = virtualFile.getOutputStream(requestor, newModificationStamp, newTimeStamp)
25+
override fun contentsToByteArray() = virtualFile.contentsToByteArray()
26+
override fun getTimeStamp() = virtualFile.timeStamp
27+
override fun getLength() = virtualFile.length
28+
override fun refresh(asynchronous: Boolean, recursive: Boolean, postRunnable: Runnable?) = virtualFile.refresh(asynchronous, recursive, postRunnable)
29+
override fun getInputStream() = virtualFile.inputStream
30+
override fun getFileType() = virtualFile.fileType
31+
override fun getModificationStamp() = 0.0.toLong()
32+
}
33+
34+
class FakeFile(val psiFile: PsiFile): PsiFile by psiFile {
35+
override fun getVirtualFile() = FakeVirtualVile(psiFile.virtualFile)
36+
37+
}
38+
39+
class GtsEslintExternalAnnotator: EslintExternalAnnotator() {
40+
41+
override fun createInfo(psiFile: PsiFile, state: EslintState, colorsScheme: EditorColorsScheme?): JSLinterInput<EslintState> {
42+
return super.createInfo(FakeFile(psiFile.viewProvider.getPsi(JavaScriptSupportLoader.TYPESCRIPT)), state, colorsScheme)
43+
}
44+
override fun acceptPsiFile(file: PsiFile): Boolean {
45+
return file is GtsFile
46+
}
47+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class HbLintAnnotator() : Annotator {
196196
annotation.needsUpdateOnTyping()
197197
annotation.create()
198198
}
199-
if (element is HbPsiElement && element.elementType == HbTokenTypes.ID && (element.reference?.resolve() == null && element.references.find { it is HbReference || it is HbsModuleReference }?.resolve() == null)) {
199+
if (element is HbPsiElement && element.elementType == HbTokenTypes.ID && (element.reference?.resolve() == null && !element.references.any { (it is HbReference || it is HbsModuleReference) && it.resolve() != null })) {
200200
val insideImport = element.parents(false).find { it is HbMustache && it.children.getOrNull(1)?.text == "import"} != null
201201
if (insideImport) return
202202
val name = element.text

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ import com.intellij.util.ProcessingContext
4444

4545
class HbsLocalCompletion : CompletionProvider<CompletionParameters>() {
4646

47-
fun resolveJsType(jsType: JSType?, result: MutableList<LookupElement>, suffix: String = "") {
47+
fun resolveJsType(type: JSType?, result: MutableList<LookupElement>, suffix: String = "") {
48+
val jsType = EmberUtils.handleEmberProxyTypes(type) ?: type
4849
val jsRecordType = jsType?.asRecordType()
50+
type?.asRecordType()?.propertyNames?.map { LookupElementBuilder.create(it + suffix) }?.toCollection(result)
4951
if (jsRecordType is JSRecordTypeImpl) {
5052
val names = jsRecordType.propertyNames
5153
result.addAll(names.map { LookupElementBuilder.create(it + suffix) })
@@ -221,7 +223,7 @@ class HbsLocalCompletion : CompletionProvider<CompletionParameters>() {
221223
rootFolder = rootFolder?.findChild(it) ?: rootFolder
222224
}
223225
if (rootFolder != null) {
224-
val validExtensions = arrayOf("css", "js", "ts")
226+
val validExtensions = arrayOf("css", "js", "ts", "gts", "gjs")
225227
val names = rootFolder!!.children.filter { it.isDirectory || validExtensions.contains(it.name.split(".").last()) }
226228
.map {
227229
val name = it.name

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.dmarcotte.handlebars.psi.*
55
import com.dmarcotte.handlebars.psi.impl.HbOpenBlockMustacheImpl
66
import com.dmarcotte.handlebars.psi.impl.HbStatementsImpl
77
import com.emberjs.glint.GlintLanguageServiceProvider
8-
import com.emberjs.gts.GtsFileViewProvider
8+
import com .emberjs.gts.GtsFileViewProvider
99
import com.emberjs.psi.EmberNamedAttribute
1010
import com.emberjs.psi.EmberNamedElement
1111
import com.emberjs.refactoring.SimpleNodeFactory
@@ -269,7 +269,19 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
269269
return any
270270
}
271271

272+
if (any is JSRecordTypeImpl.PropertySignatureImpl) {
273+
if (path.isEmpty()) {
274+
return any.jsType?.sourceElement
275+
}
276+
return resolveToJs(any.jsType, path)
277+
}
278+
272279
var jsType: JSType? = null
280+
281+
if (any is JSType) {
282+
jsType = any
283+
}
284+
273285
if (any is JSTypedEntity) {
274286
jsType = any.jsType
275287
}
@@ -288,6 +300,11 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
288300
return resolveToJs(res, path, resolveIncomplete, recursionCounter + 1)
289301
}
290302
}
303+
if (jsType is JSRecordTypeImpl && jsType.findPropertySignature(path.first()) != null) {
304+
val elem = jsType.findPropertySignature(path.first())
305+
return resolveToJs(elem, path.subList(1, max(path.lastIndex, 1)), resolveIncomplete, recursionCounter + 1)
306+
}
307+
jsType = EmberUtils.handleEmberProxyTypes(jsType) ?: jsType
291308
jsType = jsType.asRecordType()
292309
if (jsType is JSRecordTypeImpl) {
293310
val elem = jsType.findPropertySignature(path.first())
@@ -363,7 +380,7 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe
363380
if (closeMustache != null) {
364381
val blockWrapper = closeMustache.parent
365382
val openId = PsiTreeUtil.collectElements(blockWrapper) { HbsPatterns.BLOCK_MUSTACHE_NAME_ID.accepts(it) }.firstOrNull()
366-
val ref = openId?.reference ?: openId?.references?.firstOrNull()
383+
val ref = openId?.reference ?: openId?.references?.find { it.resolve() != null }
367384
return HbsLocalReference(element, ref?.resolve())
368385
}
369386

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ open class HbsModuleReference(element: PsiElement, val moduleType: String) :
5757
}
5858

5959
if (moduleType == "component") {
60-
if (internalHelpers.properties.map { it.name }.contains(text)) {
61-
val prop = internalHelpers.properties.find { it.name == text }
60+
if (internalComponents.properties.map { it.name }.contains(text)) {
61+
val prop = internalComponents.properties.find { it.name == text }
6262
return createResults((prop?.jsType?.sourceElement as JSReferenceExpression).resolve())
6363
}
6464
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ fun filter(element: PsiElement, fn: (PsiElement) -> PsiReference?): PsiReference
2929
return null
3030
}
3131
if (element.containingFile.viewProvider is GtsFileViewProvider) {
32-
return null
32+
if (!InternalsWithoutBlock.contains(element.text) && !InternalsWithBlock.contains(element.text)) {
33+
return null
34+
}
3335
}
3436
val res = HbsLocalReference.createReference(element)
3537
if (res?.resolve() != null && (res.resolve() as? EmberNamedElement)?.target != element) {
@@ -290,6 +292,7 @@ class HbsReferenceContributor : PsiReferenceContributor() {
290292
register(PlatformPatterns.psiElement(XmlAttribute::class.java)) { toAttributeReference(it as XmlAttribute) }
291293
register(HbsPatterns.SIMPLE_MUSTACHE_NAME_ID) { filter(it) { HbsComponentReference(it) } }
292294
register(HbsPatterns.BLOCK_MUSTACHE_NAME_ID) { filter(it) { HbsComponentReference(it) } }
295+
register(HbsPatterns.BLOCK_MUSTACHE_NAME_ID) { filter(it) { HbsModuleReference(it, "helper") } }
293296
register(HbsPatterns.MUSTACHE_ID) { HbsLocalReference.createReference(it) }
294297
register(HbsPatterns.SIMPLE_MUSTACHE_NAME_ID) { filter(it) { HbsModuleReference(it, "helper") } }
295298
register(HbsPatterns.SIMPLE_MUSTACHE_NAME_ID) { filter(it) { HbsModuleReference(it, "modifier") } }

0 commit comments

Comments
 (0)