Add a PsiElement without adding text to the PsiFile

203 views Asked by At

I'm trying to add a method (PsiMethod) to a class (PsiClass) so that IDEA shows this method when typing. I did this, but I ran into a problem: when I add PsiMethod to PsiClass, the text of this method appears in the file, and I don't need it. I need to add a method so that it is highlighted by IDEA, but it is not displayed in the file as text.

How can this be done?

Here is my code how I add PsiMethod to PsiClass:

val module = ModuleManager.getInstance(project).modules.first()
val file = FilenameIndex
        .getFilesByName(
            project, 
            "TestPsiFile.java",
            module.moduleContentScope)
        .first()
val newMethod = PsiElementFactory.getInstance(project).createMethod("testMethod", PsiType.VOID)

WriteCommandAction.runWriteCommandAction(project) {
        file.children
            .filter { it.elementType == JavaElementType.CLASS }
            .map { it.add(newMethod) }
}

Link to this question in the Jetbrains Community: link

0

There are 0 answers