Here’s the code that’s attempting to add a comment...
# intellij-plugins
r
Here’s the code that’s attempting to add a comment to a function body,
Copy code
fun main() {
  val project = KotlinCoreEnvironment
    .createForProduction(
      Disposer.newDisposable(),
      CompilerConfiguration(),
      EnvironmentConfigFiles.JVM_CONFIG_FILES
    ).project
  val psiFileFactory = PsiFileFactory.getInstance(project)

  val kotlinLanguage = Language.findLanguageByID("kotlin")!!
  val justTheMainFunction = """
    fun main() {
      println("Hello!")
    }
  """.trimIndent()
  val ktFile = psiFileFactory.createFileFromText(kotlinLanguage, justTheMainFunction) as KtFile

  val elementFactory = PsiElementFactory.SERVICE.getInstance(project)
  val comment = elementFactory.createCommentFromText("// This is a comment", null)

  val mainFunction = ktFile.children[2].children[0].children[0] as KtNamedFunction
  val modifiedMainFunction = mainFunction.add(comment)

  println(modifiedMainFunction.text)
}