package com.jetbrains.edu.aiHints.kotlin import com.intellij.openapi.application.runReadAction import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.project.Project import com.intellij.psi.PsiFileFactory import com.intellij.psi.codeStyle.CodeStyleManager import com.jetbrains.edu.learning.course import com.jetbrains.edu.learning.courseFormat.DescriptionFormat import org.jetbrains.kotlin.idea.KotlinLanguage val language = KotlinLanguage.INSTANCE fun String.reformatCode(project: Project): String { val psi = runReadAction { PsiFileFactory.getInstance(project).createFileFromText("file", language, this) } WriteCommandAction.runWriteCommandAction(project) { CodeStyleManager.getInstance(project).reformat(psi) } return runReadAction { psi.text } } fun createKotlinCourse() = course(language = KotlinLanguage.INSTANCE) { lesson { eduTask { kotlinTaskFile( "src/main/kotlin/Main.kt", $$""" fun greet(name: String) = "Hello, ${name}!" fun main() { println("Hello!") } """ ) } eduTask { kotlinTaskFile( "src/main/kotlin/Main.kt", $$""" val stringTemplate = "string" fun greet(name: String) = "Hello, ${name}!" fun main() { val a = "AA" val b = stringTemplate println(a) println("Hello!") } """ ) kotlinTaskFile( "src/main/kotlin/Util.kt", """ val borderSymbol = '#' val separator = ' ' val newLineSymbol = System.lineSeparator() fun getPictureWidth(picture: String) = picture.lines().maxOfOrNull { it.length } ?: 0 fun add(a: Int, b: Int): Int { return a + b } fun sum(vararg numbers: Int): Int { return numbers.sum() } fun applyOperation(a: Int, b: Int, operation: (Int, Int) -> Int) = operation(a, b) fun getPrinter(): () -> Unit = { println("Printing...") } fun nullableLength(s: String?) = s?.length """ ) kotlinTaskFile( "test/Tests.kt", """ import org.junit.Assert import org.junit.Test class Test { @Test fun testSolution() { } } """ ) } } frameworkLesson { theoryTask { kotlinTaskFile( "src/main/kotlin/Main.kt", """ fun main() { println("Hello!") } """ ) } eduTask { kotlinTaskFile( "src/main/kotlin/Main.kt", """ fun myPrint() { println("Hello!") } fun main() { myPrint() } """ ) } } lesson { eduTask { kotlinTaskFile("src/Main.kt", "fun foo(): Int = 5") kotlinTaskFile("test/Tests.kt", """ import org.junit.Assert import org.junit.Test class Tests { @Test fun testSolution() { Assert.assertTrue("foo() should return 42", foo() == 42) } } """) } } lesson { eduTask(taskDescription = """ It's time to write your first program in Kotlin! ### Task Change the output text into `Hello!` and run the program. ```kotlin fun main() { // Some code here } ```