ragunathjawahar
08/07/2019, 4:29 AMfun 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)
}