Skip to content

Commit

Permalink
fix issue with references of html tags and attributes (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx authored Feb 16, 2024
1 parent 4d00604 commit 6c675b2
Show file tree
Hide file tree
Showing 33 changed files with 192 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.emberjs.icons.EmberIconProvider
import com.emberjs.icons.EmberIcons
import com.intellij.ide.actions.CreateFileFromTemplateDialog
import com.intellij.openapi.project.Project
import javax.swing.Icon

/**
* This class is used to expose some of the `protected` properties of the `CreateFileFromTemplateDialog`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.emberjs.cli

import com.intellij.ide.util.projectWizard.WizardContext
import com.intellij.lang.javascript.boilerplate.JavaScriptNewTemplatesFactoryBase
import com.intellij.openapi.module.WebModuleBuilder
import com.intellij.platform.ProjectTemplate
import com.intellij.platform.ProjectTemplatesFactory

class EmberCliProjectTemplatesFactory : JavaScriptNewTemplatesFactoryBase() {
override fun createTemplates(context: WizardContext?): Array<ProjectTemplate> = arrayOf(EmberCliProjectGenerator())
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/com/emberjs/cli/YesThread.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.emberjs.cli

import com.emberjs.utils.isAlive

class YesThread(private val process: Process) : Thread("yes") {

override fun run() {
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/com/emberjs/glint/GlintLanguageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package com.emberjs.glint
import com.dmarcotte.handlebars.file.HbFileType
import com.dmarcotte.handlebars.psi.HbPsiFile
import com.emberjs.gts.GtsFileType
import com.emberjs.hbs.HbReference
import com.emberjs.hbs.EmberReference
import com.emberjs.utils.EmberUtils
import com.emberjs.utils.emberRoot
import com.emberjs.utils.originalVirtualFile
import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.intention.IntentionAction
Expand All @@ -32,12 +31,10 @@ import com.intellij.lang.typescript.lsp.LspAnnotationError
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.util.text.HtmlBuilder
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.platform.lsp.api.LspServerManager
import com.intellij.platform.lsp.impl.LspServerImpl
import com.intellij.platform.lsp.impl.highlighting.DiagnosticAndQuickFixes
Expand Down Expand Up @@ -144,7 +141,7 @@ class GlintTypeScriptService(project: Project) : BaseLspTypeScriptService(projec
if (element is LeafPsiElement) {
element = element.parent!!
}
if (element.reference is HbReference || element.references.find { it is HbReference } != null) {
if (element.reference is EmberReference || element.references.find { it is EmberReference } != null) {
currentlyChecking = null
return emptyArray()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.emberjs.glint

import com.emberjs.gts.GtsFileType
import com.intellij.lang.javascript.service.JSLanguageServiceProvider
import com.intellij.lang.typescript.compiler.TypeScriptService
import com.intellij.lang.typescript.compiler.languageService.TypeScriptServerServiceImpl
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileEditor.FileEditorManagerListener
import com.intellij.openapi.vfs.VirtualFile
Expand Down
4 changes: 0 additions & 4 deletions src/main/kotlin/com/emberjs/glint/GlintStatusBarWidget.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.emberjs.glint

import com.emberjs.utils.EmberUtils
import com.emberjs.utils.emberRoot
import com.intellij.lang.javascript.JavaScriptBundle
import com.intellij.lang.typescript.compiler.languageService.TypeScriptMessageBus
import com.intellij.lang.typescript.tsconfig.TypeScriptStatusBarWidget
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.ui.popup.JBPopupFactory.ActionSelectionAid
import com.intellij.openapi.ui.popup.ListPopup
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/com/emberjs/gts/GtsSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.dmarcotte.handlebars.HbLanguage
import com.dmarcotte.handlebars.parsing.HbLexer
import com.dmarcotte.handlebars.parsing.HbParseDefinition
import com.dmarcotte.handlebars.parsing.HbTokenTypes
import com.emberjs.hbs.EmberReference
import com.emberjs.icons.EmberIcons
import com.emberjs.index.EmberNameIndex
import com.emberjs.resolver.EmberName
Expand Down Expand Up @@ -66,6 +67,7 @@ import com.intellij.psi.formatter.xml.XmlTagBlock
import com.intellij.psi.html.HtmlTag
import com.intellij.psi.impl.source.PsiFileImpl
import com.intellij.psi.impl.source.html.HtmlDocumentImpl
import com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference
import com.intellij.psi.impl.source.tree.LeafElement
import com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import com.intellij.psi.search.FilenameIndex
Expand Down Expand Up @@ -467,6 +469,15 @@ class GtsFileViewProvider(manager: PsiManager, virtualFile: VirtualFile, eventSy
return element
}

override fun findReferenceAt(offset: Int): PsiReference? {
val ref = super.findReferenceAt(offset)
if (ref is PsiMultiReference) {
val r = ref.references.find { it is EmberReference && it.resolve() != null }
if (r !== null) return r
}
return ref
}

override fun getBaseLanguage(): Language {
return baseLang
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/com/emberjs/gts/HbLinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import com.dmarcotte.handlebars.parsing.HbTokenTypes
import com.dmarcotte.handlebars.psi.*
import com.emberjs.glint.GlintAnnotationError
import com.emberjs.glint.GlintTypeScriptService
import com.emberjs.hbs.HbReference
import com.emberjs.hbs.HbsModuleReference
import com.emberjs.hbs.EmberReference
import com.emberjs.utils.ifTrue
import com.intellij.codeInsight.intention.PriorityAction
import com.intellij.codeInspection.ProblemHighlightType
Expand Down Expand Up @@ -224,7 +223,7 @@ class HbLintAnnotator() : Annotator {
annotation.create()
}
}
if (element is HbPsiElement && element.elementType == HbTokenTypes.ID && (element.reference?.resolve() == null && !element.references.any { (it is HbReference || it is HbsModuleReference) && it.resolve() != null })) {
if (element is HbPsiElement && element.elementType == HbTokenTypes.ID && (element.reference?.resolve() == null && !element.references.any { it is EmberReference && it.resolve() != null })) {
val insideImport = element.parents(false).find { it is HbMustache && it.children.getOrNull(1)?.text == "import"} != null
if (insideImport) return
if (element.parent is HbHash) return
Expand Down
18 changes: 4 additions & 14 deletions src/main/kotlin/com/emberjs/hbs/HbReference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import com.intellij.psi.xml.XmlTag
import com.intellij.refactoring.rename.BindablePsiReference


abstract class HbReference(element: PsiElement): PsiReferenceBase<PsiElement>(element) {
interface EmberReference {}


abstract class HbReference(element: PsiElement): PsiReferenceBase<PsiElement>(element), EmberReference {
override fun isReferenceTo(other: PsiElement): Boolean {
var res = resolve()
if (res is EmberNamedElement) {
Expand Down Expand Up @@ -88,19 +91,6 @@ open class RangedReference(element: PsiElement, val targetPsi: PsiElement?, val
}

override fun handleElementRename(newElementName: String): PsiElement {
if (element is XmlAttribute) {
val attr = element as XmlAttribute
var newName = ""
if (attr.name.startsWith("|")) {
newName = "|"
}
newName += newElementName
if (attr.name.endsWith("|")) {
newName += "|"
}
attr.name = newName
return element
}
if (element is HbStatementsImpl) {
val tag = element.containingFile.viewProvider.getPsi(Language.findLanguageByID("HTML")!!).findElementAt(range.startOffset)!!.parent as XmlTag
tag.name = newElementName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package com.emberjs.hbs
import com.dmarcotte.handlebars.psi.impl.HbPathImpl
import com.emberjs.lookup.EmberLookupInternalElementBuilder
import com.emberjs.utils.originalVirtualFile
import com.emberjs.utils.parents
import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.injected.editor.VirtualFileWindow
import com.intellij.javascript.nodejs.reference.NodeModuleManager
import com.intellij.psi.PsiManager
import com.intellij.psi.util.parentsWithSelf
import com.intellij.util.ProcessingContext



class HbsBuiltinHelperCompletionProvider(val helpers: List<String>) : CompletionProvider<CompletionParameters>() {
val lookupElements = helpers

Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/com/emberjs/hbs/HbsFindUsagesProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.intellij.lang.findUsages.FindUsagesProvider
import com.intellij.lang.javascript.JavaScriptFileType
import com.intellij.lang.javascript.TypeScriptFileType
import com.intellij.psi.PsiElement
import com.intellij.psi.css.CssFileType
import com.intellij.psi.tree.TokenSet
import com.intellij.psi.util.elementType

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/emberjs/hbs/HbsLocalCompletion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class HbsLocalCompletion : CompletionProvider<CompletionParameters>() {
if (anything is PsiElement && anything.references.find { it is HbsLocalReference } != null) {
resolve((anything.references.find { it is HbsLocalReference } as HbsLocalReference).resolveYield(), result)
resolve((anything.references.find { it is HbsLocalReference } as HbsLocalReference).resolved as? PropertySignature, result)
resolve(anything.references.find { it is HbReference }!!.resolve(), result)
resolve(anything.references.find { it is EmberReference }!!.resolve(), result)
}

if (anything is PsiElement && anything.reference is HbsLocalReference) {
Expand Down Expand Up @@ -312,7 +312,7 @@ class HbsLocalCompletion : CompletionProvider<CompletionParameters>() {
}
}

if (it is JSFunction && it.parent !is JSVariable) {
if (it is JSFunction && it.parent !is JSVariable && it.name != null) {
val useScope = JSUseScopeProvider.getBlockScopeElement(it)
if (useScope.isAncestor(tpl)) {
result.addElement(LookupElementBuilder.create(it.name!!))
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/emberjs/hbs/HbsLocalReference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class HbsLocalReference(private val leaf: PsiElement, val resolved: Any?) : HbRe

// as html tag
val htmlView = element.containingFile.viewProvider.getPsi(Language.findLanguageByID("HTML")!!)
val angleBracketBlocks = PsiTreeUtil.collectElements(htmlView, { it is XmlAttribute && it.text.startsWith("|") })
val angleBracketBlocks = PsiTreeUtil.collectElements(htmlView) { it is XmlAttribute && it.text.startsWith("|") }
.filter{ (it.parent as HtmlTag).attributes.map { it.text }.joinToString(" ").contains(Regex("\\|.*\\b$name\\b.*\\|")) }
.map { it.parent }

Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/com/emberjs/hbs/HbsModuleReference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.emberjs.utils.EmberUtils
import com.emberjs.utils.originalVirtualFile
import com.intellij.injected.editor.VirtualFileWindow
import com.intellij.javascript.nodejs.reference.NodeModuleManager
import com.intellij.lang.Language
import com.intellij.lang.javascript.JavaScriptSupportLoader
import com.intellij.lang.javascript.psi.JSObjectLiteralExpression
import com.intellij.lang.javascript.psi.JSReferenceExpression
Expand All @@ -17,7 +16,7 @@ import com.intellij.psi.PsiElementResolveResult.createResults
import com.intellij.psi.search.ProjectScope

open class HbsModuleReference(element: PsiElement, val moduleType: String) :
PsiPolyVariantReferenceBase<PsiElement>(element, TextRange(0, element.textLength), true) {
PsiPolyVariantReferenceBase<PsiElement>(element, TextRange(0, element.textLength), true), EmberReference {

val project = element.project
private val scope = ProjectScope.getAllScope(project)
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/com/emberjs/hbs/HbsParameterInfoHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.emberjs.hbs
import com.dmarcotte.handlebars.parsing.HbTokenTypes
import com.dmarcotte.handlebars.psi.HbParam
import com.dmarcotte.handlebars.psi.HbPsiElement
import com.emberjs.utils.*
import com.intellij.codeInsight.hints.InlayInfo
import com.emberjs.utils.EmberUtils
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.lang.javascript.psi.*
import com.intellij.lang.javascript.psi.JSFunction
import com.intellij.lang.javascript.psi.JSParameterListElement
import com.intellij.lang.javascript.psi.JSRecordType
import com.intellij.lang.javascript.psi.JSType
import com.intellij.lang.javascript.psi.ecma6.TypeScriptCallSignature
import com.intellij.lang.javascript.psi.ecma6.TypeScriptVariable
import com.intellij.lang.javascript.psi.types.JSArrayType
Expand All @@ -15,7 +17,6 @@ import com.intellij.lang.parameterInfo.*
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.elementType
import com.intellij.refactoring.suggested.startOffset


class HbsParameterInfoHandler : ParameterInfoHandler<PsiElement, Any?> {
Expand Down
17 changes: 0 additions & 17 deletions src/main/kotlin/com/emberjs/hbs/HbsParameterNameHints.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
package com.emberjs.hbs

import com.dmarcotte.handlebars.HbLanguage
import com.dmarcotte.handlebars.parsing.HbTokenTypes
import com.dmarcotte.handlebars.psi.HbParam
import com.emberjs.utils.EmberUtils
import com.emberjs.utils.ifTrue
import com.intellij.codeInsight.hints.HintInfo
import com.intellij.codeInsight.hints.InlayInfo
import com.intellij.codeInsight.hints.InlayParameterHintsProvider
import com.intellij.codeInsight.hints.Option
import com.intellij.lang.javascript.psi.JSDestructuringArray
import com.intellij.lang.javascript.psi.JSFunction
import com.intellij.lang.javascript.psi.JSRecordType
import com.intellij.lang.javascript.psi.JSType
import com.intellij.lang.javascript.psi.ecma6.TypeScriptCallSignature
import com.intellij.lang.javascript.psi.ecma6.TypeScriptTupleType
import com.intellij.lang.javascript.psi.ecma6.TypeScriptVariable
import com.intellij.lang.javascript.psi.ecma6.impl.TypeScriptTupleTypeImpl
import com.intellij.lang.javascript.psi.ecmal4.JSClass
import com.intellij.lang.javascript.psi.types.JSGenericTypeImpl
import com.intellij.lang.javascript.psi.types.JSSimpleRecordTypeImpl
import com.intellij.lang.javascript.psi.types.JSTupleType
import com.intellij.lang.javascript.psi.types.JSTypeImpl
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.SyntaxTraverser
import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.elementType
import com.intellij.refactoring.suggested.startOffset
import java.lang.Integer.max
import java.util.*
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/com/emberjs/hbs/HbsReferenceContributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.emberjs.index.EmberNameIndex
import com.emberjs.psi.EmberNamedElement
import com.emberjs.translations.EmberTranslationHbsReferenceProvider
import com.emberjs.utils.*
import com.intellij.lang.Language
import com.intellij.lang.javascript.JavaScriptSupportLoader
import com.intellij.lang.javascript.psi.JSElementBase
import com.intellij.lang.javascript.psi.JSLiteralExpression
Expand Down
Loading

0 comments on commit 6c675b2

Please sign in to comment.