generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/me/mattco/serenityos/ipc/psi/mixins/IPCEndpointMixin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package me.mattco.serenityos.ipc.psi.mixins | ||
|
||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.PsiManager | ||
import com.intellij.psi.search.FileTypeIndex | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import com.jetbrains.cidr.lang.OCFileType | ||
import com.jetbrains.cidr.lang.psi.OCElement | ||
import com.jetbrains.cidr.lang.psi.OCFile | ||
import com.jetbrains.cidr.lang.psi.OCStructLike | ||
import com.jetbrains.cidr.lang.search.scopes.OCSearchScope | ||
import me.mattco.serenityos.ipc.psi.IPCNamedElement | ||
import me.mattco.serenityos.ipc.psi.api.IPCEndpoint | ||
import me.mattco.serenityos.ipc.psi.multiRef | ||
|
||
abstract class IPCEndpointMixin(node: ASTNode) : IPCNamedElement(node), IPCEndpoint { | ||
override fun getReference() = multiRef { resolveCppDecl() } | ||
|
||
private fun resolveCppDecl(): List<OCElement> { | ||
val endpointName = identifier.text | ||
|
||
val scope = OCSearchScope.getProjectSourcesScope(project) | ||
val psiManager = PsiManager.getInstance(project) | ||
|
||
// We will only ever resolve to header files. Additionally, IPC files have some naming | ||
// conventions that we can use to avoid searching thousands of files | ||
val files = FileTypeIndex.getFiles(OCFileType.INSTANCE, scope) | ||
.filter { | ||
val name = it.nameWithoutExtension | ||
it.extension == "h" && ("Client" in name || "Server" in name || "Connection" in name) | ||
} | ||
|
||
val matchingEndpoints = mutableListOf<OCStructLike>() | ||
|
||
for (file in files) { | ||
val cppFile = psiManager.findFile(file) as OCFile | ||
val structs = PsiTreeUtil.findChildrenOfType(cppFile, OCStructLike::class.java) | ||
for (struct in structs) { | ||
for (base in struct.baseClausesList?.baseClauses.orEmpty()) { | ||
val baseText = base.text | ||
if (endpointName !in baseText) continue | ||
if (baseText.startsWith("public IPC::ConnectionFromClient") | ||
|| baseText.startsWith("public IPC::ConnectionToServer") | ||
) { | ||
matchingEndpoints.add(struct) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return matchingEndpoints | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters