Skip to content

Commit

Permalink
feat(usage): make context map to usage
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 7, 2022
1 parent 7419cc6 commit 228f8eb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/main/grammars/FeakinParser.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ contextDeclaration ::= CONTEXT_KEYWORD IDENTIFIER contextBody
implements = [
"com.feakin.intellij.psi.FkNamedElement"
"com.feakin.intellij.psi.FkNameIdentifierOwner"
// "com.feakin.intellij.psi.ext.FkMandatoryReferenceElement"
"com.feakin.intellij.psi.ext.FkMandatoryReferenceElement"
]
mixin = "com.feakin.intellij.stubs.ext.FkContextDeclMixin"
stubClass = "com.feakin.intellij.stubs.FkContextDeclStub"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
package com.feakin.intellij.resolve.ref.impl

import com.feakin.intellij.psi.FkContextDeclaration
import com.feakin.intellij.psi.FkContextName
import com.feakin.intellij.psi.FkElement
import com.feakin.intellij.resolve.indexes.FkNamedElementIndex
import com.feakin.intellij.FkFile
import com.feakin.intellij.FkFileType
import com.feakin.intellij.psi.*
import com.feakin.intellij.resolve.ref.FkReferenceBase
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiManager
import com.intellij.psi.search.FileTypeIndex
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.PsiTreeUtil

// todo: change to contextNameRef
//class FkContextDeclReferenceImpl(
// element: FkContextDeclaration
//) : FkReferenceBase<FkContextDeclaration>(element) {
// override fun multiResolve(): List<FkElement> {
// val collection =
// FkNamedElementIndex.findElementsByName(element.identifier.text, element.project, element.resolveScope)
// .filter { it is FkContextName }
// .toCollection(mutableListOf())
//
// return collection
// }
//
// override fun isReferenceTo(element: PsiElement): Boolean {
// return element is FkContextDeclaration && super.isReferenceTo(element)
// }
//}
class FkContextDeclReferenceImpl(
element: FkContextDeclaration
) : FkReferenceBase<FkContextDeclaration>(element) {
override fun multiResolve(): List<FkElement> {
val project = element.project
val scope = GlobalSearchScope.allScope(project)
val collection = mutableListOf<FkElement>()

val virtualFiles: Collection<VirtualFile> = FileTypeIndex.getFiles(FkFileType, scope)
virtualFiles.forEach { virtualFile ->
val fkFile = PsiManager.getInstance(project).findFile(virtualFile) as FkFile
val fkDeclarations = PsiTreeUtil.getChildrenOfType(fkFile, FkDeclaration::class.java)

val contextMaps: Array<out FkContextMapDeclaration>? = fkDeclarations?.mapNotNull {
PsiTreeUtil.getChildrenOfType(it, FkContextMapDeclaration::class.java)?.toList()
}?.flatten()?.toTypedArray()

val mapBodys: Array<out FkContextMapBody>? = contextMaps?.mapNotNull {
PsiTreeUtil.getChildrenOfType(it, FkContextMapBody::class.java)?.toList()
}?.flatten()?.toTypedArray()

mapBodys?.forEach { contextMapDeclaration ->
PsiTreeUtil.getChildrenOfType(contextMapDeclaration, FkContextName::class.java)
?.forEach { contextName ->
if (contextName.text == element.name) {
collection.add(contextName)
}
}
}
}

return collection
}

override fun isReferenceTo(element: PsiElement): Boolean {
return element is FkContextDeclaration && super.isReferenceTo(element)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ open class FkCommandConfigurationEditor(project: Project) : SettingsEditor<FkCom
@Suppress("UnstableApiUsage")
override fun createEditor(): JComponent = panel {
row("Command:") {
cell(command).horizontalAlign(HorizontalAlign.FILL)
cell(command)
.horizontalAlign(HorizontalAlign.FILL)
.resizableColumn()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package com.feakin.intellij.stubs.ext

import com.feakin.intellij.psi.FkContextDeclaration
import com.feakin.intellij.psi.FkStubbedNamedElementImpl
import com.feakin.intellij.resolve.ref.FkReference
import com.feakin.intellij.resolve.ref.impl.FkContextDeclReferenceImpl
import com.feakin.intellij.resolve.ref.impl.FkContextNameReferenceImpl
import com.feakin.intellij.stubs.FkContextDeclStub
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement
import com.intellij.psi.stubs.IStubElementType

abstract class FkContextDeclMixin : FkStubbedNamedElementImpl<FkContextDeclStub>, FkContextDeclaration {
constructor(node: ASTNode) : super(node)

constructor(stub: FkContextDeclStub, nodeType: IStubElementType<*, *>) : super(stub, nodeType)

// override fun getReference(): FkReference = FkContextDeclReferenceImpl(this)
override fun getReference(): FkReference = FkContextDeclReferenceImpl(this)

// override val referenceNameElement: PsiElement
// get() = checkNotNull(identifier) {
// "Context must contain identifier: $this $text at ${containingFile.virtualFile.path}"
// }
override val referenceNameElement: PsiElement get() = identifier

// override val referenceName: String get() = greenStub?.name ?: super.referenceName
override val referenceName: String get() = greenStub?.name ?: super.referenceName
}

0 comments on commit 228f8eb

Please sign in to comment.