0

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

DJ. XYZ
  • 99
  • 1
  • 9
  • I'd recommend you raise this issue at https://intellij-support.jetbrains.com/hc/en-us/community/topics/200366979-IntelliJ-IDEA-Open-API-and-Plugin-Development so that the IntelliJ platform team could take a look at it. – Egor Klepikov Jul 14 '21 at 14:20
  • Thank you, I did it! – DJ. XYZ Jul 14 '21 at 14:32

0 Answers0