From a2b30a91070f269992860ad615f4b5f811912174 Mon Sep 17 00:00:00 2001 From: "anastasia.birillo" Date: Fri, 1 Dec 2023 21:53:56 +0100 Subject: [PATCH 1/8] Remove JUnit5 --- build.gradle.kts | 12 ++--- .../findNumberOfClassesTask/src/Task.kt | 4 +- .../findNumberOfClassesTask/test/Tests.kt | 1 + .../programmingTask/test/Tests.kt | 49 ------------------- 4 files changed, 9 insertions(+), 57 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4f9b668..b3dd8c7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -69,11 +69,11 @@ configure(subprojects) { // By default, only the core module is included implementation("org.jetbrains.academy.test.system:core:2.0.5") - val junitJupiterVersion = "5.9.0" - implementation("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion") - runtimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion") - implementation("org.junit.jupiter:junit-jupiter-params:$junitJupiterVersion") - runtimeOnly("org.junit.platform:junit-platform-console:1.9.0") +// val junitJupiterVersion = "5.9.0" +// implementation("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion") +// runtimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion") +// implementation("org.junit.jupiter:junit-jupiter-params:$junitJupiterVersion") +// runtimeOnly("org.junit.platform:junit-platform-console:1.9.0") val detektVersion = "1.22.0" implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detektVersion") @@ -95,7 +95,7 @@ configure(subprojects) { // This part is necessary for the JetBrains Academy plugin withType { - useJUnitPlatform() +// useJUnitPlatform() outputs.upToDateWhen { false } diff --git a/courseSection/courseLesson/findNumberOfClassesTask/src/Task.kt b/courseSection/courseLesson/findNumberOfClassesTask/src/Task.kt index d2357ec..be45e55 100644 --- a/courseSection/courseLesson/findNumberOfClassesTask/src/Task.kt +++ b/courseSection/courseLesson/findNumberOfClassesTask/src/Task.kt @@ -1,7 +1,7 @@ -import com.intellij.psi.PsiClass import com.intellij.psi.PsiFile import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.psi.KtClass fun countPsiClasses(psiFile: PsiFile): Int { - return PsiTreeUtil.findChildrenOfType(psiFile, PsiClass::class.java).toList().size + return PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java).toList().size } diff --git a/courseSection/courseLesson/findNumberOfClassesTask/test/Tests.kt b/courseSection/courseLesson/findNumberOfClassesTask/test/Tests.kt index 6958605..f0dc447 100644 --- a/courseSection/courseLesson/findNumberOfClassesTask/test/Tests.kt +++ b/courseSection/courseLesson/findNumberOfClassesTask/test/Tests.kt @@ -1,6 +1,7 @@ import com.intellij.testFramework.fixtures.BasePlatformTestCase class Test : BasePlatformTestCase() { + fun testSolution() { val file = myFixture.configureByText("MyClass.kt", """ class MyFirstClass { diff --git a/courseSection/courseLesson/programmingTask/test/Tests.kt b/courseSection/courseLesson/programmingTask/test/Tests.kt index 02855ec..7a7f663 100644 --- a/courseSection/courseLesson/programmingTask/test/Tests.kt +++ b/courseSection/courseLesson/programmingTask/test/Tests.kt @@ -4,55 +4,6 @@ import org.jetbrains.academy.kotlin.template.runMainFunction import org.jetbrains.academy.kotlin.template.throwInternalCourseError import org.jetbrains.academy.test.system.core.invokeWithArgs import org.jetbrains.academy.test.system.core.models.classes.findClassSafe -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.BeforeAll -import org.junit.jupiter.api.Test -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.Arguments -import org.junit.jupiter.params.provider.MethodSource class Test { - companion object { - private lateinit var mainClazz: Class<*> - - @JvmStatic - @BeforeAll - fun beforeAll() { - mainClazz = mainClass.findClassSafe() ?: throwInternalCourseError() - } - - private const val HELLO = "Hello" - - @JvmStatic - fun invokeSayHelloArguments() = listOf( - Arguments.of(1, List(1) { HELLO }.joinToString(System.lineSeparator())), - Arguments.of(2, List(2) { HELLO }.joinToString(System.lineSeparator())), - Arguments.of(3, List(3) { HELLO }.joinToString(System.lineSeparator())) - ) - } - @Test - fun invokeSayHelloFunction() { - mainClass.checkMethod(mainClazz, invokeSayHelloFunction) - } - - @ParameterizedTest - @MethodSource("invokeSayHelloArguments") - fun invokeSayHelloImplementation( - howManyTimes: Int, - output: String, - ) { - val userMethod = mainClass.findMethod(mainClazz, invokeSayHelloFunction) - Assertions.assertEquals( - output, - userMethod.invokeWithArgs(howManyTimes, clazz = mainClazz)?.toString()?.trimIndent(), - "For howManyTimes = $howManyTimes the function ${invokeSayHelloFunction.name} should return $output" - ) - } - @Test - fun testSolution() { - Assertions.assertEquals( - "How many times should I print $HELLO?${newLineSeparator}$HELLO${newLineSeparator}$HELLO".trimIndent(), - runMainFunction(::main, "2").trimIndent() - ) - } } \ No newline at end of file From feef77a50a4ee78780ba34be74f6febd49816ff9 Mon Sep 17 00:00:00 2001 From: Maria Tigina Date: Sat, 2 Dec 2023 00:05:47 +0100 Subject: [PATCH 2/8] Removed junit dependencies --- build.gradle.kts | 8 -------- courseSection/courseLesson/programmingTask/test/Tests.kt | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b3dd8c7..af8b216 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -69,12 +69,6 @@ configure(subprojects) { // By default, only the core module is included implementation("org.jetbrains.academy.test.system:core:2.0.5") -// val junitJupiterVersion = "5.9.0" -// implementation("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion") -// runtimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion") -// implementation("org.junit.jupiter:junit-jupiter-params:$junitJupiterVersion") -// runtimeOnly("org.junit.platform:junit-platform-console:1.9.0") - val detektVersion = "1.22.0" implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detektVersion") implementation("io.gitlab.arturbosch.detekt:detekt-formatting:$detektVersion") @@ -95,8 +89,6 @@ configure(subprojects) { // This part is necessary for the JetBrains Academy plugin withType { -// useJUnitPlatform() - outputs.upToDateWhen { false } addTestListener(object : TestListener { diff --git a/courseSection/courseLesson/programmingTask/test/Tests.kt b/courseSection/courseLesson/programmingTask/test/Tests.kt index 7a7f663..34f7582 100644 --- a/courseSection/courseLesson/programmingTask/test/Tests.kt +++ b/courseSection/courseLesson/programmingTask/test/Tests.kt @@ -6,4 +6,4 @@ import org.jetbrains.academy.test.system.core.invokeWithArgs import org.jetbrains.academy.test.system.core.models.classes.findClassSafe class Test { -} \ No newline at end of file +} From 2b32e3ac829c54aa70b525cec33dd650e130910b Mon Sep 17 00:00:00 2001 From: Maria Tigina Date: Mon, 4 Dec 2023 17:46:11 +0100 Subject: [PATCH 3/8] Refactored first tasks for PSI section --- README.md | 2 +- .../findNumberOfClassesTask/task.md | 6 ---- .../introductionToPsiTask/task.md | 33 ------------------- courseSection/courseLesson/lesson-info.yaml | 7 ---- .../academy/kotlin/template/InvisibleFile.kt | 5 --- .../jetbrains/academy/kotlin/template/Main.kt | 11 ------- .../programmingTask/task-info.yaml | 20 ----------- .../courseLesson/programmingTask/task.md | 6 ---- .../test/InvokeSayHelloFunction.kt | 12 ------- .../programmingTask/test/MainClass.kt | 8 ----- .../programmingTask/test/Tests.kt | 9 ----- .../jetbrains/academy/kotlin/template/Main.kt | 5 --- .../courseLesson/quizTask/task-info.yaml | 12 ------- courseSection/courseLesson/quizTask/task.md | 2 -- .../academy/kotlin/template/InvisibleFile.kt | 5 --- .../jetbrains/academy/kotlin/template/Main.kt | 5 --- .../courseLesson/theoryTask/task-info.yaml | 7 ---- courseSection/courseLesson/theoryTask/task.md | 14 -------- .../Accessing PSI Elements}/src/Main.kt | 1 - .../Accessing PSI Elements}/task-info.yaml | 0 .../psi/Accessing PSI Elements/task.md | 17 ++++++++++ .../Count number of classes}/src/Task.kt | 2 +- .../Count number of classes}/task-info.yaml | 4 +++ .../psi/Count number of classes/task.md | 6 ++++ .../Count number of classes}/test/Tests.kt | 2 +- .../psi/Try PSI Viewer/src/Person.kt | 5 +++ .../psi/Try PSI Viewer/task-info.yaml | 15 +++++++++ courseSection/psi/Try PSI Viewer/task.md | 12 +++++++ courseSection/psi/What is PSI/src/Person.kt | 8 +++++ courseSection/psi/What is PSI/task-info.yaml | 4 +++ courseSection/psi/What is PSI/task.md | 26 +++++++++++++++ courseSection/psi/lesson-info.yaml | 6 ++++ courseSection/section-info.yaml | 2 +- 33 files changed, 107 insertions(+), 172 deletions(-) delete mode 100644 courseSection/courseLesson/findNumberOfClassesTask/task.md delete mode 100644 courseSection/courseLesson/introductionToPsiTask/task.md delete mode 100644 courseSection/courseLesson/lesson-info.yaml delete mode 100644 courseSection/courseLesson/programmingTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt delete mode 100644 courseSection/courseLesson/programmingTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt delete mode 100644 courseSection/courseLesson/programmingTask/task-info.yaml delete mode 100644 courseSection/courseLesson/programmingTask/task.md delete mode 100644 courseSection/courseLesson/programmingTask/test/InvokeSayHelloFunction.kt delete mode 100644 courseSection/courseLesson/programmingTask/test/MainClass.kt delete mode 100644 courseSection/courseLesson/programmingTask/test/Tests.kt delete mode 100644 courseSection/courseLesson/quizTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt delete mode 100644 courseSection/courseLesson/quizTask/task-info.yaml delete mode 100644 courseSection/courseLesson/quizTask/task.md delete mode 100644 courseSection/courseLesson/theoryTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt delete mode 100644 courseSection/courseLesson/theoryTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt delete mode 100644 courseSection/courseLesson/theoryTask/task-info.yaml delete mode 100644 courseSection/courseLesson/theoryTask/task.md rename courseSection/{courseLesson/introductionToPsiTask => psi/Accessing PSI Elements}/src/Main.kt (50%) rename courseSection/{courseLesson/introductionToPsiTask => psi/Accessing PSI Elements}/task-info.yaml (100%) create mode 100644 courseSection/psi/Accessing PSI Elements/task.md rename courseSection/{courseLesson/findNumberOfClassesTask => psi/Count number of classes}/src/Task.kt (81%) rename courseSection/{courseLesson/findNumberOfClassesTask => psi/Count number of classes}/task-info.yaml (52%) create mode 100644 courseSection/psi/Count number of classes/task.md rename courseSection/{courseLesson/findNumberOfClassesTask => psi/Count number of classes}/test/Tests.kt (87%) create mode 100644 courseSection/psi/Try PSI Viewer/src/Person.kt create mode 100644 courseSection/psi/Try PSI Viewer/task-info.yaml create mode 100644 courseSection/psi/Try PSI Viewer/task.md create mode 100644 courseSection/psi/What is PSI/src/Person.kt create mode 100644 courseSection/psi/What is PSI/task-info.yaml create mode 100644 courseSection/psi/What is PSI/task.md create mode 100644 courseSection/psi/lesson-info.yaml diff --git a/README.md b/README.md index 7df84c4..5d61292 100644 --- a/README.md +++ b/README.md @@ -259,7 +259,7 @@ In the `.github/workflows` directory, you can find definitions for the following [file:gradle.properties]: ./gradle.properties [file:course-info.yaml]: ./course-info.yaml [file:courseignore]: .courseignore -[file:course.lesson.tests]: ./courseSection/courseLesson/programmingTask/test/Tests.kt +[file:course.lesson.tests]: courseSection/psi/programmingTask/test/Tests.kt [file:course.framework.lesson.tests]: ./courseSection/courseFrameworkLesson/programmingTask/test/Tests.kt [gradle]: https://gradle.org diff --git a/courseSection/courseLesson/findNumberOfClassesTask/task.md b/courseSection/courseLesson/findNumberOfClassesTask/task.md deleted file mode 100644 index 98dd830..0000000 --- a/courseSection/courseLesson/findNumberOfClassesTask/task.md +++ /dev/null @@ -1,6 +0,0 @@ - -You need to implement a function classCounter which will count number of classes in the kotlin file. -
-[Try to find method in official docs to get the psi file](https://plugins.jetbrains.com/docs/intellij/psi-files.html) -
- diff --git a/courseSection/courseLesson/introductionToPsiTask/task.md b/courseSection/courseLesson/introductionToPsiTask/task.md deleted file mode 100644 index 5589c45..0000000 --- a/courseSection/courseLesson/introductionToPsiTask/task.md +++ /dev/null @@ -1,33 +0,0 @@ - -# Intro to Psi - ---- -PSI stands for Program Structure Interface. It's a part of IntelliJ Platform SDK. -It represents the entire structure of the code in a project as a tree of elements, called a PSI tree. - -**PSI Tree Structure** -* The root is a **PsiFile** element, representing an entire file. -* Each node/element in the tree represents a syntactic or structural part of the code, like expressions, statements, and declarations. - -**Types of PSI Elements** -* PsiFile: Represents an entire file. -* PsiClass: Represents a class in the code. -* PsiMethod: Represents a method. -* PsiVariable: Represents a variable. -* PsiExpression: Represents an expression, and so on. - -**Accessing PSI Elements** - -**PsiTreeUtil** is a utility class in the IntelliJ Platform SDK that provides methods for navigating and querying the PSI tree of a project. - -It's a part of the `com.intellij.psi.util package`. - -The **findChildrenOfType** method is used to find all children of a specified type within a given PSI element. -It's particularly useful when you need to locate all instances of a particular element type, such as classes, methods, or variables, within a file or a code block. - -Syntax: -`public static @Unmodifiable @NotNull Collection findChildrenOfType(@Nullable PsiElement element, @NotNull Class aClass)` - -**Parameters** -* element: The PSI element within which to search for children. This could be a PsiFile, a PsiClass, or any other PSI element. -* aClass: The class type of the elements you are searching for. For example, PsiClass.class to find all classes. \ No newline at end of file diff --git a/courseSection/courseLesson/lesson-info.yaml b/courseSection/courseLesson/lesson-info.yaml deleted file mode 100644 index 7244454..0000000 --- a/courseSection/courseLesson/lesson-info.yaml +++ /dev/null @@ -1,7 +0,0 @@ -custom_name: Course lesson -content: - - theoryTask - - quizTask - - programmingTask - - introductionToPsiTask - - findNumberOfClassesTask diff --git a/courseSection/courseLesson/programmingTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt b/courseSection/courseLesson/programmingTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt deleted file mode 100644 index 2795778..0000000 --- a/courseSection/courseLesson/programmingTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.jetbrains.academy.kotlin.template - -// This file will be hidden in the student mode -@Suppress("FunctionOnlyReturningConstant") -fun sayHello() = "Hello" diff --git a/courseSection/courseLesson/programmingTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt b/courseSection/courseLesson/programmingTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt deleted file mode 100644 index a8ce7a8..0000000 --- a/courseSection/courseLesson/programmingTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt +++ /dev/null @@ -1,11 +0,0 @@ -package org.jetbrains.academy.kotlin.template - -fun invokeSayHello(howManyTimes: Int): String { - return List(howManyTimes) { sayHello() }.joinToString(System.lineSeparator()) -} - -fun main() { - println("How many times should I print Hello?") - val howManyTimes = readln().toInt() - println(invokeSayHello(howManyTimes)) -} diff --git a/courseSection/courseLesson/programmingTask/task-info.yaml b/courseSection/courseLesson/programmingTask/task-info.yaml deleted file mode 100644 index 120fb8e..0000000 --- a/courseSection/courseLesson/programmingTask/task-info.yaml +++ /dev/null @@ -1,20 +0,0 @@ -type: edu -custom_name: Course programming task -files: - - name: test/Tests.kt - visible: false - - name: src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt - visible: true - placeholders: - - offset: 95 - length: 81 - placeholder_text: TODO("Write your solution here") - - offset: 193 - length: 133 - placeholder_text: // Write your solution here - - name: src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt - visible: false - - name: test/InvokeSayHelloFunction.kt - visible: false - - name: test/MainClass.kt - visible: false diff --git a/courseSection/courseLesson/programmingTask/task.md b/courseSection/courseLesson/programmingTask/task.md deleted file mode 100644 index 02677b9..0000000 --- a/courseSection/courseLesson/programmingTask/task.md +++ /dev/null @@ -1,6 +0,0 @@ -This task ask the user to implement something and run tests from the [Tests.kt](./test/Tests.kt) file. -You need to add _placeholders_ into the places where the user needs to implement something (see [task-info.yaml](./task-info.yaml)). - -This task also demonstrates how you can use the [kotlin test framework](https://github.com/jetbrains-academy/kotlin-test-framework) -to test functions or classes without using the correct implementation in the tests. -It allows you to avoid using the correct implementation in tests directly. \ No newline at end of file diff --git a/courseSection/courseLesson/programmingTask/test/InvokeSayHelloFunction.kt b/courseSection/courseLesson/programmingTask/test/InvokeSayHelloFunction.kt deleted file mode 100644 index df96949..0000000 --- a/courseSection/courseLesson/programmingTask/test/InvokeSayHelloFunction.kt +++ /dev/null @@ -1,12 +0,0 @@ -import org.jetbrains.academy.test.system.core.models.TestKotlinType -import org.jetbrains.academy.test.system.core.models.Visibility -import org.jetbrains.academy.test.system.core.models.method.TestMethod -import org.jetbrains.academy.test.system.core.models.variable.TestVariable - -internal val invokeSayHelloFunction = TestMethod( - "invokeSayHello", - TestKotlinType("String"), - arguments = listOf( - TestVariable("howManyTimes", "Int"), - ), -) diff --git a/courseSection/courseLesson/programmingTask/test/MainClass.kt b/courseSection/courseLesson/programmingTask/test/MainClass.kt deleted file mode 100644 index 216684e..0000000 --- a/courseSection/courseLesson/programmingTask/test/MainClass.kt +++ /dev/null @@ -1,8 +0,0 @@ -import org.jetbrains.academy.test.system.core.models.classes.TestClass - -internal val mainClass = TestClass( - classPackage = "org.jetbrains.academy.kotlin.template", - customMethods = listOf( - invokeSayHelloFunction, - ), -) diff --git a/courseSection/courseLesson/programmingTask/test/Tests.kt b/courseSection/courseLesson/programmingTask/test/Tests.kt deleted file mode 100644 index 34f7582..0000000 --- a/courseSection/courseLesson/programmingTask/test/Tests.kt +++ /dev/null @@ -1,9 +0,0 @@ -import org.jetbrains.academy.kotlin.template.main -import org.jetbrains.academy.kotlin.template.newLineSeparator -import org.jetbrains.academy.kotlin.template.runMainFunction -import org.jetbrains.academy.kotlin.template.throwInternalCourseError -import org.jetbrains.academy.test.system.core.invokeWithArgs -import org.jetbrains.academy.test.system.core.models.classes.findClassSafe - -class Test { -} diff --git a/courseSection/courseLesson/quizTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt b/courseSection/courseLesson/quizTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt deleted file mode 100644 index ee3a607..0000000 --- a/courseSection/courseLesson/quizTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.jetbrains.academy.kotlin.template - -fun main() { - // Write your solution here -} diff --git a/courseSection/courseLesson/quizTask/task-info.yaml b/courseSection/courseLesson/quizTask/task-info.yaml deleted file mode 100644 index fb0a18e..0000000 --- a/courseSection/courseLesson/quizTask/task-info.yaml +++ /dev/null @@ -1,12 +0,0 @@ -type: choice -is_multiple_choice: false -options: - - text: Correct - is_correct: true - - text: Incorrect - is_correct: false -message_incorrect: This text will be shown if the user made a mistake. -files: - - name: src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt - visible: true -custom_name: Course quiz task diff --git a/courseSection/courseLesson/quizTask/task.md b/courseSection/courseLesson/quizTask/task.md deleted file mode 100644 index 17189e3..0000000 --- a/courseSection/courseLesson/quizTask/task.md +++ /dev/null @@ -1,2 +0,0 @@ -You cannot change the question text **Select one option from the list**, you need to put your question into this file. -You can set up all quiz options in the [task-info](./task-info.yaml) file. \ No newline at end of file diff --git a/courseSection/courseLesson/theoryTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt b/courseSection/courseLesson/theoryTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt deleted file mode 100644 index 2795778..0000000 --- a/courseSection/courseLesson/theoryTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.jetbrains.academy.kotlin.template - -// This file will be hidden in the student mode -@Suppress("FunctionOnlyReturningConstant") -fun sayHello() = "Hello" diff --git a/courseSection/courseLesson/theoryTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt b/courseSection/courseLesson/theoryTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt deleted file mode 100644 index ee3a607..0000000 --- a/courseSection/courseLesson/theoryTask/src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.jetbrains.academy.kotlin.template - -fun main() { - // Write your solution here -} diff --git a/courseSection/courseLesson/theoryTask/task-info.yaml b/courseSection/courseLesson/theoryTask/task-info.yaml deleted file mode 100644 index 0fe3de8..0000000 --- a/courseSection/courseLesson/theoryTask/task-info.yaml +++ /dev/null @@ -1,7 +0,0 @@ -type: theory -custom_name: Course theory task -files: - - name: src/main/kotlin/org/jetbrains/academy/kotlin/template/Main.kt - visible: true - - name: src/main/kotlin/org/jetbrains/academy/kotlin/template/InvisibleFile.kt - visible: false diff --git a/courseSection/courseLesson/theoryTask/task.md b/courseSection/courseLesson/theoryTask/task.md deleted file mode 100644 index 5f5ba82..0000000 --- a/courseSection/courseLesson/theoryTask/task.md +++ /dev/null @@ -1,14 +0,0 @@ -It is an example of a theory lesson. - -
- -You can use hints to explain something to the students. -Please, leave an empty line to be able to apply Markdown formatting like **this**. - -Invisible files are hidden in the student mode. -You can use them to define the helper functions etc in the course. - -To mark a file as an invisible file you need to set up the `false` value for the corresponding file in the [task-info](./task-info.yaml) file. -
- -Theory tasks do not contain any tests. diff --git a/courseSection/courseLesson/introductionToPsiTask/src/Main.kt b/courseSection/psi/Accessing PSI Elements/src/Main.kt similarity index 50% rename from courseSection/courseLesson/introductionToPsiTask/src/Main.kt rename to courseSection/psi/Accessing PSI Elements/src/Main.kt index 1084f30..2d5aa02 100644 --- a/courseSection/courseLesson/introductionToPsiTask/src/Main.kt +++ b/courseSection/psi/Accessing PSI Elements/src/Main.kt @@ -1,4 +1,3 @@ -package org.jetbrains.academy.kotlin.template fun main() { // Write your solution here } diff --git a/courseSection/courseLesson/introductionToPsiTask/task-info.yaml b/courseSection/psi/Accessing PSI Elements/task-info.yaml similarity index 100% rename from courseSection/courseLesson/introductionToPsiTask/task-info.yaml rename to courseSection/psi/Accessing PSI Elements/task-info.yaml diff --git a/courseSection/psi/Accessing PSI Elements/task.md b/courseSection/psi/Accessing PSI Elements/task.md new file mode 100644 index 0000000..910edc5 --- /dev/null +++ b/courseSection/psi/Accessing PSI Elements/task.md @@ -0,0 +1,17 @@ +# Accessing PSI Elements + +--- +[com.intellij.psi.util.PsiTreeUtil](https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/psi/util/PsiTreeUtil.java) +is a utility class in the IntelliJ Platform SDK that provides methods for navigating and querying the PSI tree of a project. + +The [com.intellij.psi.util.PsiTreeUtil.findChildrenOfType](https://github.com/JetBrains/intellij-community/blob/30cfa651ac2b9c50163368b56ee87ce1944543ec/platform/core-api/src/com/intellij/psi/util/PsiTreeUtil.java#L197C64-L197C64) method is used to find all children of a specified type within a given PSI element. +It's particularly useful when you need to locate all instances of a particular element type, such as classes, methods, or variables, within a file or a code block. + +**Syntax:** +```java +public static @Unmodifiable @NotNull Collection findChildrenOfType(@Nullable PsiElement element, @NotNull Class aClass) +``` + +**Parameters:** +* **element**: The PSI element within which to search for children. This could be a PsiFile, a PsiClass, or any other PSI element. +* **aClass**: The class type of the elements you are searching for. For example, PsiClass.class to find all classes. \ No newline at end of file diff --git a/courseSection/courseLesson/findNumberOfClassesTask/src/Task.kt b/courseSection/psi/Count number of classes/src/Task.kt similarity index 81% rename from courseSection/courseLesson/findNumberOfClassesTask/src/Task.kt rename to courseSection/psi/Count number of classes/src/Task.kt index be45e55..09ab51e 100644 --- a/courseSection/courseLesson/findNumberOfClassesTask/src/Task.kt +++ b/courseSection/psi/Count number of classes/src/Task.kt @@ -2,6 +2,6 @@ import com.intellij.psi.PsiFile import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.psi.KtClass -fun countPsiClasses(psiFile: PsiFile): Int { +fun countKtClasses(psiFile: PsiFile): Int { return PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java).toList().size } diff --git a/courseSection/courseLesson/findNumberOfClassesTask/task-info.yaml b/courseSection/psi/Count number of classes/task-info.yaml similarity index 52% rename from courseSection/courseLesson/findNumberOfClassesTask/task-info.yaml rename to courseSection/psi/Count number of classes/task-info.yaml index 3fe721d..b32e778 100644 --- a/courseSection/courseLesson/findNumberOfClassesTask/task-info.yaml +++ b/courseSection/psi/Count number of classes/task-info.yaml @@ -2,5 +2,9 @@ type: edu files: - name: src/Task.kt visible: true + placeholders: + - offset: 158 + length: 85 + placeholder_text: TODO() - name: test/Tests.kt visible: false diff --git a/courseSection/psi/Count number of classes/task.md b/courseSection/psi/Count number of classes/task.md new file mode 100644 index 0000000..c75a7d2 --- /dev/null +++ b/courseSection/psi/Count number of classes/task.md @@ -0,0 +1,6 @@ + +You need to implement a function `countKtClasses` which will count number of kotlin classes declared in the given kotlin PSI file. +
+Try to use `KtClass::class.java` value for aClass parameter for `findChildrenOfType` +
+ diff --git a/courseSection/courseLesson/findNumberOfClassesTask/test/Tests.kt b/courseSection/psi/Count number of classes/test/Tests.kt similarity index 87% rename from courseSection/courseLesson/findNumberOfClassesTask/test/Tests.kt rename to courseSection/psi/Count number of classes/test/Tests.kt index f0dc447..060787c 100644 --- a/courseSection/courseLesson/findNumberOfClassesTask/test/Tests.kt +++ b/courseSection/psi/Count number of classes/test/Tests.kt @@ -11,7 +11,7 @@ class Test : BasePlatformTestCase() { } """.trimIndent()) - val psiClassesCount = countPsiClasses(file) + val psiClassesCount = countKtClasses(file) assertEquals(2, psiClassesCount) } } diff --git a/courseSection/psi/Try PSI Viewer/src/Person.kt b/courseSection/psi/Try PSI Viewer/src/Person.kt new file mode 100644 index 0000000..1f47410 --- /dev/null +++ b/courseSection/psi/Try PSI Viewer/src/Person.kt @@ -0,0 +1,5 @@ +class Person(private val name: String, private val age: Int) { + fun introduceTo(friend: Person) { + println("Hi, ${friend.name}! My name is $name and I'm $age years old.") + } +} diff --git a/courseSection/psi/Try PSI Viewer/task-info.yaml b/courseSection/psi/Try PSI Viewer/task-info.yaml new file mode 100644 index 0000000..1cd6521 --- /dev/null +++ b/courseSection/psi/Try PSI Viewer/task-info.yaml @@ -0,0 +1,15 @@ +type: choice +is_multiple_choice: true +options: + - text: org.jetbrains.kotlin.psi.KtClass + is_correct: true + - text: org.jetbrains.kotlin.psi.KtNamedFunction + is_correct: true + - text: org.jetbrains.kotlin.psi.KtConstantExpression + is_correct: false + - text: org.jetbrains.kotlin.psi.KtNamedReferenceExpression + is_correct: true +files: + - name: src/Person.kt + visible: true +local_check: true diff --git a/courseSection/psi/Try PSI Viewer/task.md b/courseSection/psi/Try PSI Viewer/task.md new file mode 100644 index 0000000..b6d2483 --- /dev/null +++ b/courseSection/psi/Try PSI Viewer/task.md @@ -0,0 +1,12 @@ +# Try PSI Viewer + +--- +Follow a `useful tools` link in [PSI Documentation main page](https://plugins.jetbrains.com/docs/intellij/psi.html) +and find an action which provides an ability to see and analyze PSI tree of opened file. + +Now, I’d want you, **Jonsi**, to look at to PSI tree of [Person.kt](courseSection/psi/Try PSI Viewer/src/Person.kt) file +and select PSI elements presented in it? + +
+Use Tools | View PSI Structure of Current File... action to access PSI tree +
\ No newline at end of file diff --git a/courseSection/psi/What is PSI/src/Person.kt b/courseSection/psi/What is PSI/src/Person.kt new file mode 100644 index 0000000..c400991 --- /dev/null +++ b/courseSection/psi/What is PSI/src/Person.kt @@ -0,0 +1,8 @@ +package org.jetbrains.academy.kotlin.template + +class Person(private val name: String, private val age: Int) { + + fun introduceTo(friend: Person) { + println("Hi, ${friend.name}! My name is $name and I'm $age years old.") + } +} diff --git a/courseSection/psi/What is PSI/task-info.yaml b/courseSection/psi/What is PSI/task-info.yaml new file mode 100644 index 0000000..f50926e --- /dev/null +++ b/courseSection/psi/What is PSI/task-info.yaml @@ -0,0 +1,4 @@ +type: theory +files: + - name: src/Person.kt + visible: true diff --git a/courseSection/psi/What is PSI/task.md b/courseSection/psi/What is PSI/task.md new file mode 100644 index 0000000..2e23990 --- /dev/null +++ b/courseSection/psi/What is PSI/task.md @@ -0,0 +1,26 @@ +# What is PSI? + +--- +PSI stands for Program Structure Interface. It's a part of IntelliJ Platform SDK, please have a brief look into +[PSI Documentation](https://plugins.jetbrains.com/docs/intellij/psi.html) on IntelliJ Platform SDK Documentation web page. +As you probably read there, PSI represents the entire structure of the code in a project as a tree of elements, +called a PSI tree. + +### PSI Tree Structure +* The root is a [PSI File](https://plugins.jetbrains.com/docs/intellij/psi-files.html) element, representing an entire file. +* Each node or [PSI Element](https://plugins.jetbrains.com/docs/intellij/psi-elements.html) in the tree represents a syntactic or structural part of the code, like expressions, +* statements, and declarations. + +### Types of PSI Elements +* PsiFile: Represents an entire file. +* PsiClass: Represents a class in the code. +* PsiMethod: Represents a method. +* PsiVariable: Represents a variable. +* PsiExpression: Represents an expression, and so on. + +Each language (Java/Kotlin/Python) has their own implementations of [PsiElement](https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/psi/PsiElement.java), +so for example for file abstraction there are following options: +* [com.intellij.psi.PsiFile](https://github.com/JetBrains/intellij-community/blob/idea/232.10227.8/platform/core-api/src/com/intellij/psi/PsiFile.java): Represents an Java file. +* [org.jetbrains.kotlin.psi.KtFile](https://github.com/JetBrains/kotlin/blob/master/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt): Represents a Kotlin file. +* [com.jetbrains.python.psi.PyFile](https://github.com/JetBrains/intellij-community/blob/master/python/python-psi-api/src/com/jetbrains/python/psi/PyFile.java#L28): Represents a Python file. + diff --git a/courseSection/psi/lesson-info.yaml b/courseSection/psi/lesson-info.yaml new file mode 100644 index 0000000..4de8801 --- /dev/null +++ b/courseSection/psi/lesson-info.yaml @@ -0,0 +1,6 @@ +custom_name: PSI +content: + - What is PSI + - Try PSI Viewer + - Accessing PSI Elements + - Count number of classes diff --git a/courseSection/section-info.yaml b/courseSection/section-info.yaml index 358ed2a..4ba83da 100644 --- a/courseSection/section-info.yaml +++ b/courseSection/section-info.yaml @@ -1,3 +1,3 @@ custom_name: Course section content: - - courseLesson + - psi From b0f3619c23a49bc46760678dec1c99c70aa8dfa0 Mon Sep 17 00:00:00 2001 From: Valerii Date: Tue, 5 Dec 2023 15:23:47 +0100 Subject: [PATCH 4/8] editing psi task done --- courseSection/psi/editPSI/src/Task.kt | 18 +++++++++ courseSection/psi/editPSI/task-info.yaml | 6 +++ courseSection/psi/editPSI/task.md | 48 ++++++++++++++++++++++++ courseSection/psi/editPSI/test/Tests.kt | 24 ++++++++++++ courseSection/psi/lesson-info.yaml | 1 + 5 files changed, 97 insertions(+) create mode 100644 courseSection/psi/editPSI/src/Task.kt create mode 100644 courseSection/psi/editPSI/task-info.yaml create mode 100644 courseSection/psi/editPSI/task.md create mode 100644 courseSection/psi/editPSI/test/Tests.kt diff --git a/courseSection/psi/editPSI/src/Task.kt b/courseSection/psi/editPSI/src/Task.kt new file mode 100644 index 0000000..7794366 --- /dev/null +++ b/courseSection/psi/editPSI/src/Task.kt @@ -0,0 +1,18 @@ +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.psi.PsiFile +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.psi.KtNamedFunction + + +fun editFunctionName(psiFile: PsiFile, currentName: String, newName: String) { + val project = psiFile.project + + WriteCommandAction.runWriteCommandAction(project) { + val functions = PsiTreeUtil.findChildrenOfType(psiFile, KtNamedFunction::class.java) + for (function in functions) { + if (function.name == currentName) { + function.setName(newName) + } + } + } +} \ No newline at end of file diff --git a/courseSection/psi/editPSI/task-info.yaml b/courseSection/psi/editPSI/task-info.yaml new file mode 100644 index 0000000..3fe721d --- /dev/null +++ b/courseSection/psi/editPSI/task-info.yaml @@ -0,0 +1,6 @@ +type: edu +files: + - name: src/Task.kt + visible: true + - name: test/Tests.kt + visible: false diff --git a/courseSection/psi/editPSI/task.md b/courseSection/psi/editPSI/task.md new file mode 100644 index 0000000..fa7499b --- /dev/null +++ b/courseSection/psi/editPSI/task.md @@ -0,0 +1,48 @@ + +This is a task description file. +Its content will be displayed to a learner +in the **Task Description** window. + +It supports both Markdown and HTML. +To toggle the format, you can rename **task.md** +to **task.html**, or vice versa. +The default task description format can be changed +in **Preferences | Tools | Education**, +but this will not affect any existing task description files. + +The following features are available in +**task.md/task.html** which are specific to the JetBrains Academy plugin: + +- Hints can be added anywhere in the task text. + Type "hint" and press Tab. + Hints should be added to an empty line in the task text. + In hints you can use both HTML and Markdown. +
+ +Text of your hint + +
+ +- You may need to refer your learners to a particular lesson, +task, or file. To achieve this, you can use the in-course links. +Specify the path using the `[link_text](course://lesson1/task1/file1)` format. + +- You can insert shortcuts in the task description. +While **task.html/task.md** is open, right-click anywhere +on the **Editor** tab and choose the **Insert shortcut** option +from the context menu. +For example: &shortcut:FileStructurePopup;. + +- Insert the %`IDE_NAME`% macro, +which will be replaced by the actual IDE name. +For example, **%IDE_NAME%**. + +- Insert PSI elements, by using links like +`[element_description](psi_element://link.to.element)`. +To get such a link, right-click the class or method +and select **Copy Reference**. +Then press &shortcut:EditorPaste; to insert the link where appropriate. +For example, a [link to the "contains" method](psi_element://java.lang.String#contains). + +- You can add link to file using **full path** like this: + `[file_link](file://lesson1/task1/file.txt)`. \ No newline at end of file diff --git a/courseSection/psi/editPSI/test/Tests.kt b/courseSection/psi/editPSI/test/Tests.kt new file mode 100644 index 0000000..ad304c4 --- /dev/null +++ b/courseSection/psi/editPSI/test/Tests.kt @@ -0,0 +1,24 @@ +import com.intellij.psi.util.PsiTreeUtil +import com.intellij.testFramework.fixtures.BasePlatformTestCase +import org.jetbrains.kotlin.psi.KtNamedFunction + +class Test : BasePlatformTestCase() { + + fun testSolution() { + var file = myFixture.configureByText("MyClass.kt", """ + fun sayHello() { + println("Hello") + } + + fun sayGoodbye() { + println("Goodbye") + } + """.trimIndent()) + + editFunctionName(file, "sayHello", "greetings") + + val functions = PsiTreeUtil.findChildrenOfType(file, KtNamedFunction::class.java) + val hasNewFunction = functions.any { it.name == "greetings" } + assertTrue(hasNewFunction) + } +} \ No newline at end of file diff --git a/courseSection/psi/lesson-info.yaml b/courseSection/psi/lesson-info.yaml index 4de8801..444479f 100644 --- a/courseSection/psi/lesson-info.yaml +++ b/courseSection/psi/lesson-info.yaml @@ -4,3 +4,4 @@ content: - Try PSI Viewer - Accessing PSI Elements - Count number of classes + - editPSI From b2cbe7fe552b42760bf05592875b3a69b0174061 Mon Sep 17 00:00:00 2001 From: Valerii Date: Tue, 5 Dec 2023 20:44:42 +0100 Subject: [PATCH 5/8] sort methods done --- courseSection/psi/addPSI/src/Task.kt | 24 +++++++++++++ courseSection/psi/addPSI/task-info.yaml | 6 ++++ courseSection/psi/addPSI/task.md | 48 +++++++++++++++++++++++++ courseSection/psi/addPSI/test/Tests.kt | 35 ++++++++++++++++++ courseSection/psi/lesson-info.yaml | 1 + 5 files changed, 114 insertions(+) create mode 100644 courseSection/psi/addPSI/src/Task.kt create mode 100644 courseSection/psi/addPSI/task-info.yaml create mode 100644 courseSection/psi/addPSI/task.md create mode 100644 courseSection/psi/addPSI/test/Tests.kt diff --git a/courseSection/psi/addPSI/src/Task.kt b/courseSection/psi/addPSI/src/Task.kt new file mode 100644 index 0000000..84d90e0 --- /dev/null +++ b/courseSection/psi/addPSI/src/Task.kt @@ -0,0 +1,24 @@ +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.psi.PsiFile +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.psi.KtClass +import org.jetbrains.kotlin.psi.KtNamedFunction + + +// type your solution here + +fun sortMethods(psiFile: PsiFile) { + val project = psiFile.project + WriteCommandAction.runWriteCommandAction(project) { + val classes = PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java) + + for (ktClass in classes){ + val methods = ktClass.declarations.filterIsInstance() + val sortedMethods = methods.sortedBy { it.name }.map { it.copy() as KtNamedFunction } + + methods.zip(sortedMethods).forEach { (original, sortedCopy) -> + original.replace(sortedCopy) + } + } + } +} \ No newline at end of file diff --git a/courseSection/psi/addPSI/task-info.yaml b/courseSection/psi/addPSI/task-info.yaml new file mode 100644 index 0000000..3fe721d --- /dev/null +++ b/courseSection/psi/addPSI/task-info.yaml @@ -0,0 +1,6 @@ +type: edu +files: + - name: src/Task.kt + visible: true + - name: test/Tests.kt + visible: false diff --git a/courseSection/psi/addPSI/task.md b/courseSection/psi/addPSI/task.md new file mode 100644 index 0000000..fa7499b --- /dev/null +++ b/courseSection/psi/addPSI/task.md @@ -0,0 +1,48 @@ + +This is a task description file. +Its content will be displayed to a learner +in the **Task Description** window. + +It supports both Markdown and HTML. +To toggle the format, you can rename **task.md** +to **task.html**, or vice versa. +The default task description format can be changed +in **Preferences | Tools | Education**, +but this will not affect any existing task description files. + +The following features are available in +**task.md/task.html** which are specific to the JetBrains Academy plugin: + +- Hints can be added anywhere in the task text. + Type "hint" and press Tab. + Hints should be added to an empty line in the task text. + In hints you can use both HTML and Markdown. +
+ +Text of your hint + +
+ +- You may need to refer your learners to a particular lesson, +task, or file. To achieve this, you can use the in-course links. +Specify the path using the `[link_text](course://lesson1/task1/file1)` format. + +- You can insert shortcuts in the task description. +While **task.html/task.md** is open, right-click anywhere +on the **Editor** tab and choose the **Insert shortcut** option +from the context menu. +For example: &shortcut:FileStructurePopup;. + +- Insert the %`IDE_NAME`% macro, +which will be replaced by the actual IDE name. +For example, **%IDE_NAME%**. + +- Insert PSI elements, by using links like +`[element_description](psi_element://link.to.element)`. +To get such a link, right-click the class or method +and select **Copy Reference**. +Then press &shortcut:EditorPaste; to insert the link where appropriate. +For example, a [link to the "contains" method](psi_element://java.lang.String#contains). + +- You can add link to file using **full path** like this: + `[file_link](file://lesson1/task1/file.txt)`. \ No newline at end of file diff --git a/courseSection/psi/addPSI/test/Tests.kt b/courseSection/psi/addPSI/test/Tests.kt new file mode 100644 index 0000000..eeb3cc4 --- /dev/null +++ b/courseSection/psi/addPSI/test/Tests.kt @@ -0,0 +1,35 @@ +import com.intellij.psi.util.PsiTreeUtil +import com.intellij.testFramework.fixtures.BasePlatformTestCase +import org.jetbrains.kotlin.psi.KtClass +import org.jetbrains.kotlin.psi.KtNamedFunction + +class Test : BasePlatformTestCase() { + + fun testSolution() { + var file = myFixture.configureByText("MyClass.kt", """ + class MyClass { + fun abcd() { + println("Hello") + } + + fun zhas() { + println("Goodbye") + } + + fun mid() { + println("Goodbye") + } + } + + """.trimIndent()) + + sortMethods(file) + val classes = PsiTreeUtil.findChildrenOfType(file, KtClass::class.java) + + for (ktClass in classes){ + val methods = ktClass.declarations.filterIsInstance() + val sorted = methods.sortedBy { it.name } + assertEquals(sorted, methods) + } + } +} \ No newline at end of file diff --git a/courseSection/psi/lesson-info.yaml b/courseSection/psi/lesson-info.yaml index 444479f..c1a9433 100644 --- a/courseSection/psi/lesson-info.yaml +++ b/courseSection/psi/lesson-info.yaml @@ -5,3 +5,4 @@ content: - Accessing PSI Elements - Count number of classes - editPSI + - addPSI From c2e562ce700506c8a880e5721eb9f4c9b6892f7d Mon Sep 17 00:00:00 2001 From: Valerii Date: Fri, 8 Dec 2023 11:11:49 +0100 Subject: [PATCH 6/8] add annotations done --- courseSection/psi/addPSI/src/Task.kt | 50 ++++++++++++++++++++++++++ courseSection/psi/addPSI/test/Tests.kt | 30 ++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/courseSection/psi/addPSI/src/Task.kt b/courseSection/psi/addPSI/src/Task.kt index 84d90e0..4a2d58c 100644 --- a/courseSection/psi/addPSI/src/Task.kt +++ b/courseSection/psi/addPSI/src/Task.kt @@ -1,6 +1,11 @@ import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.project.Project +import com.intellij.psi.JavaPsiFacade import com.intellij.psi.PsiFile +import com.intellij.psi.PsiMethod +import com.intellij.psi.PsiType import com.intellij.psi.util.PsiTreeUtil +import com.intellij.psi.util.TypeConversionUtil import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtNamedFunction @@ -21,4 +26,49 @@ fun sortMethods(psiFile: PsiFile) { } } } +} + +fun addAnnotations(psiFile: PsiFile){ + val project = psiFile.project + WriteCommandAction.runWriteCommandAction(project) { + val psiMethods = PsiTreeUtil.findChildrenOfType(psiFile, PsiMethod::class.java) + + for (psiMethod in psiMethods) { + if (shouldAddOverrideAnnotation(psiMethod)) { + addOverrideAnnotation(psiMethod, project) + } + } + } +} + +private fun addOverrideAnnotation(psiMethod: PsiMethod, project: Project) { + val psiElementFactory = JavaPsiFacade.getElementFactory(project) + val overrideAnnotation = psiElementFactory.createAnnotationFromText("@Override", null) + psiMethod.modifierList.addBefore(overrideAnnotation, psiMethod.modifierList.firstChild) +} + +private fun shouldAddOverrideAnnotation(psiMethod: PsiMethod): Boolean { + // Logic to determine if this method overrides a method from its superclass + val containingClass = psiMethod.containingClass ?: return false + + val superClasses = containingClass.supers + val methodName = psiMethod.name + val parameterTypes = psiMethod.parameterList.parameters.map { it.type } + + for (superClass in superClasses) { + val superMethods = superClass.methods + for (superMethod in superMethods) { + if (superMethod.name == methodName + && superMethod.parameterList.parameters.map { it.type } == parameterTypes + && isReturnTypeCompatible(superMethod.returnType, psiMethod.returnType)) { + return true + } + } + } + return false; +} + +private fun isReturnTypeCompatible(superReturnType: PsiType?, subReturnType: PsiType?): Boolean { + if (superReturnType == null || subReturnType == null) return false + return TypeConversionUtil.isAssignable(superReturnType, subReturnType) } \ No newline at end of file diff --git a/courseSection/psi/addPSI/test/Tests.kt b/courseSection/psi/addPSI/test/Tests.kt index eeb3cc4..53938f5 100644 --- a/courseSection/psi/addPSI/test/Tests.kt +++ b/courseSection/psi/addPSI/test/Tests.kt @@ -1,5 +1,9 @@ +import com.intellij.ide.highlighter.JavaFileType +import com.intellij.psi.PsiClass +import com.intellij.psi.PsiMethod import com.intellij.psi.util.PsiTreeUtil import com.intellij.testFramework.fixtures.BasePlatformTestCase +import junit.framework.TestCase import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtNamedFunction @@ -32,4 +36,30 @@ class Test : BasePlatformTestCase() { assertEquals(sorted, methods) } } + + fun testAnnotation() { + val javaFile = myFixture.configureByText( + JavaFileType.INSTANCE, """ + class SuperClass { + public void testMethod() {} + } + + class SubClass extends SuperClass { + + public void testMethod() {} + } + """.trimIndent()) + + addAnnotations(javaFile) + val subClass = PsiTreeUtil.findChildrenOfType(javaFile, PsiClass::class.java) + .first { it.name == "SubClass" } + val methods = PsiTreeUtil.findChildrenOfType(subClass, PsiMethod::class.java) + + val testMethod = methods.first { it.name == "testMethod" } + val hasOverrideAnnotation = testMethod.modifierList.findAnnotation("Override") != null + + // Assert that the @Override annotation is present + assertTrue("The method 'testMethod' should have @Override annotation", hasOverrideAnnotation) + + } } \ No newline at end of file From 8f54dc12f571740556a21dcaf38d8ea424cf7fb9 Mon Sep 17 00:00:00 2001 From: Valerii Date: Mon, 18 Dec 2023 11:38:13 +0100 Subject: [PATCH 7/8] add packages --- .../{ => jetbrains/plugindev/course/access}/Main.kt | 2 ++ .../psi/Accessing PSI Elements/task-info.yaml | 2 +- .../{ => jetbrains/plugindev/course/addpsi}/Task.kt | 7 +++++-- courseSection/psi/addPSI/task-info.yaml | 4 ++-- .../{ => jetbrains/plugindev/course/addpsi}/Tests.kt | 3 ++- .../jetbrains/plugindev/course/classcounter}/Task.kt | 2 ++ courseSection/psi/classCounter/task-info.yaml | 10 ++++++++++ .../jetbrains/plugindev/course/classcounter}/Tests.kt | 2 ++ .../{ => jetbrains/plugindev/course/editpsi}/Task.kt | 2 ++ courseSection/psi/editPSI/task-info.yaml | 4 ++-- .../{ => jetbrains/plugindev/course/editpsi}/Tests.kt | 2 ++ courseSection/psi/lesson-info.yaml | 2 +- 12 files changed, 33 insertions(+), 9 deletions(-) rename courseSection/psi/Accessing PSI Elements/src/{ => jetbrains/plugindev/course/access}/Main.kt (52%) rename courseSection/psi/addPSI/src/{ => jetbrains/plugindev/course/addpsi}/Task.kt (96%) rename courseSection/psi/addPSI/test/{ => jetbrains/plugindev/course/addpsi}/Tests.kt (97%) rename courseSection/psi/{Count number of classes/src => classCounter/src/jetbrains/plugindev/course/classcounter}/Task.kt (83%) create mode 100644 courseSection/psi/classCounter/task-info.yaml rename courseSection/psi/{Count number of classes/test => classCounter/test/jetbrains/plugindev/course/classcounter}/Tests.kt (89%) rename courseSection/psi/editPSI/src/{ => jetbrains/plugindev/course/editpsi}/Task.kt (93%) rename courseSection/psi/editPSI/test/{ => jetbrains/plugindev/course/editpsi}/Tests.kt (94%) diff --git a/courseSection/psi/Accessing PSI Elements/src/Main.kt b/courseSection/psi/Accessing PSI Elements/src/jetbrains/plugindev/course/access/Main.kt similarity index 52% rename from courseSection/psi/Accessing PSI Elements/src/Main.kt rename to courseSection/psi/Accessing PSI Elements/src/jetbrains/plugindev/course/access/Main.kt index 2d5aa02..5af6e4c 100644 --- a/courseSection/psi/Accessing PSI Elements/src/Main.kt +++ b/courseSection/psi/Accessing PSI Elements/src/jetbrains/plugindev/course/access/Main.kt @@ -1,3 +1,5 @@ +package jetbrains.plugindev.course.access + fun main() { // Write your solution here } diff --git a/courseSection/psi/Accessing PSI Elements/task-info.yaml b/courseSection/psi/Accessing PSI Elements/task-info.yaml index cc8f611..49a98d6 100644 --- a/courseSection/psi/Accessing PSI Elements/task-info.yaml +++ b/courseSection/psi/Accessing PSI Elements/task-info.yaml @@ -1,4 +1,4 @@ type: theory files: - - name: src/Main.kt + - name: src/jetbrains/plugindev/course/access/Main.kt visible: true diff --git a/courseSection/psi/addPSI/src/Task.kt b/courseSection/psi/addPSI/src/jetbrains/plugindev/course/addpsi/Task.kt similarity index 96% rename from courseSection/psi/addPSI/src/Task.kt rename to courseSection/psi/addPSI/src/jetbrains/plugindev/course/addpsi/Task.kt index 4a2d58c..7265ff7 100644 --- a/courseSection/psi/addPSI/src/Task.kt +++ b/courseSection/psi/addPSI/src/jetbrains/plugindev/course/addpsi/Task.kt @@ -1,3 +1,5 @@ +package jetbrains.plugindev.course.addpsi + import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.project.Project import com.intellij.psi.JavaPsiFacade @@ -60,12 +62,13 @@ private fun shouldAddOverrideAnnotation(psiMethod: PsiMethod): Boolean { for (superMethod in superMethods) { if (superMethod.name == methodName && superMethod.parameterList.parameters.map { it.type } == parameterTypes - && isReturnTypeCompatible(superMethod.returnType, psiMethod.returnType)) { + && isReturnTypeCompatible(superMethod.returnType, psiMethod.returnType) + ) { return true } } } - return false; + return false } private fun isReturnTypeCompatible(superReturnType: PsiType?, subReturnType: PsiType?): Boolean { diff --git a/courseSection/psi/addPSI/task-info.yaml b/courseSection/psi/addPSI/task-info.yaml index 3fe721d..6927d9a 100644 --- a/courseSection/psi/addPSI/task-info.yaml +++ b/courseSection/psi/addPSI/task-info.yaml @@ -1,6 +1,6 @@ type: edu files: - - name: src/Task.kt + - name: src/jetbrains/plugindev/course/addpsi/Task.kt visible: true - - name: test/Tests.kt + - name: test/jetbrains/plugindev/course/addpsi/Tests.kt visible: false diff --git a/courseSection/psi/addPSI/test/Tests.kt b/courseSection/psi/addPSI/test/jetbrains/plugindev/course/addpsi/Tests.kt similarity index 97% rename from courseSection/psi/addPSI/test/Tests.kt rename to courseSection/psi/addPSI/test/jetbrains/plugindev/course/addpsi/Tests.kt index 53938f5..c5b7fff 100644 --- a/courseSection/psi/addPSI/test/Tests.kt +++ b/courseSection/psi/addPSI/test/jetbrains/plugindev/course/addpsi/Tests.kt @@ -1,9 +1,10 @@ +package jetbrains.plugindev.course.addpsi + import com.intellij.ide.highlighter.JavaFileType import com.intellij.psi.PsiClass import com.intellij.psi.PsiMethod import com.intellij.psi.util.PsiTreeUtil import com.intellij.testFramework.fixtures.BasePlatformTestCase -import junit.framework.TestCase import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtNamedFunction diff --git a/courseSection/psi/Count number of classes/src/Task.kt b/courseSection/psi/classCounter/src/jetbrains/plugindev/course/classcounter/Task.kt similarity index 83% rename from courseSection/psi/Count number of classes/src/Task.kt rename to courseSection/psi/classCounter/src/jetbrains/plugindev/course/classcounter/Task.kt index 09ab51e..3ec0236 100644 --- a/courseSection/psi/Count number of classes/src/Task.kt +++ b/courseSection/psi/classCounter/src/jetbrains/plugindev/course/classcounter/Task.kt @@ -1,3 +1,5 @@ +package jetbrains.plugindev.course.classcounter + import com.intellij.psi.PsiFile import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.psi.KtClass diff --git a/courseSection/psi/classCounter/task-info.yaml b/courseSection/psi/classCounter/task-info.yaml new file mode 100644 index 0000000..2736283 --- /dev/null +++ b/courseSection/psi/classCounter/task-info.yaml @@ -0,0 +1,10 @@ +type: edu +files: + - name: src/jetbrains/plugindev/course/classcounter/Task.kt + visible: true + placeholders: + - offset: 160 + length: 85 + placeholder_text: TODO() + - name: test/jetbrains/plugindev/course/classcounter/Tests.kt + visible: false diff --git a/courseSection/psi/Count number of classes/test/Tests.kt b/courseSection/psi/classCounter/test/jetbrains/plugindev/course/classcounter/Tests.kt similarity index 89% rename from courseSection/psi/Count number of classes/test/Tests.kt rename to courseSection/psi/classCounter/test/jetbrains/plugindev/course/classcounter/Tests.kt index 060787c..9b1ad89 100644 --- a/courseSection/psi/Count number of classes/test/Tests.kt +++ b/courseSection/psi/classCounter/test/jetbrains/plugindev/course/classcounter/Tests.kt @@ -1,3 +1,5 @@ +package jetbrains.plugindev.course.classcounter + import com.intellij.testFramework.fixtures.BasePlatformTestCase class Test : BasePlatformTestCase() { diff --git a/courseSection/psi/editPSI/src/Task.kt b/courseSection/psi/editPSI/src/jetbrains/plugindev/course/editpsi/Task.kt similarity index 93% rename from courseSection/psi/editPSI/src/Task.kt rename to courseSection/psi/editPSI/src/jetbrains/plugindev/course/editpsi/Task.kt index 7794366..357abf5 100644 --- a/courseSection/psi/editPSI/src/Task.kt +++ b/courseSection/psi/editPSI/src/jetbrains/plugindev/course/editpsi/Task.kt @@ -1,3 +1,5 @@ +package jetbrains.plugindev.course.editpsi + import com.intellij.openapi.command.WriteCommandAction import com.intellij.psi.PsiFile import com.intellij.psi.util.PsiTreeUtil diff --git a/courseSection/psi/editPSI/task-info.yaml b/courseSection/psi/editPSI/task-info.yaml index 3fe721d..f551a04 100644 --- a/courseSection/psi/editPSI/task-info.yaml +++ b/courseSection/psi/editPSI/task-info.yaml @@ -1,6 +1,6 @@ type: edu files: - - name: src/Task.kt + - name: src/jetbrains/plugindev/course/editpsi/Task.kt visible: true - - name: test/Tests.kt + - name: test/jetbrains/plugindev/course/editpsi/Tests.kt visible: false diff --git a/courseSection/psi/editPSI/test/Tests.kt b/courseSection/psi/editPSI/test/jetbrains/plugindev/course/editpsi/Tests.kt similarity index 94% rename from courseSection/psi/editPSI/test/Tests.kt rename to courseSection/psi/editPSI/test/jetbrains/plugindev/course/editpsi/Tests.kt index ad304c4..9cf7146 100644 --- a/courseSection/psi/editPSI/test/Tests.kt +++ b/courseSection/psi/editPSI/test/jetbrains/plugindev/course/editpsi/Tests.kt @@ -1,3 +1,5 @@ +package jetbrains.plugindev.course.editpsi + import com.intellij.psi.util.PsiTreeUtil import com.intellij.testFramework.fixtures.BasePlatformTestCase import org.jetbrains.kotlin.psi.KtNamedFunction diff --git a/courseSection/psi/lesson-info.yaml b/courseSection/psi/lesson-info.yaml index c1a9433..d2cfc68 100644 --- a/courseSection/psi/lesson-info.yaml +++ b/courseSection/psi/lesson-info.yaml @@ -3,6 +3,6 @@ content: - What is PSI - Try PSI Viewer - Accessing PSI Elements - - Count number of classes + - classCounter - editPSI - addPSI From fd56d767ada91675aa4cbe501326ded8ab40f92e Mon Sep 17 00:00:00 2001 From: Valerii Date: Wed, 27 Dec 2023 14:10:30 +0100 Subject: [PATCH 8/8] fix --- .../jetbrains/plugindev/course/access/Main.kt | 0 .../task-info.yaml | 1 + .../task.md | 0 .../ij/plugin/course/annotations}/Task.kt | 23 +--------- .../psi/AddAnnotations/task-info.yaml | 11 +++++ .../psi/{addPSI => AddAnnotations}/task.md | 0 .../ij/plugin/course/annotations}/Tests.kt | 35 ++-------------- .../plugindev/course/classcounter/Task.kt | 0 .../task-info.yaml | 5 ++- .../task.md | 1 + .../plugindev/course/classcounter/Tests.kt | 0 .../plugindev/course/editpsi/Task.kt | 13 ++++++ .../psi/{editPSI => EditPSI}/task-info.yaml | 5 +++ .../plugindev/course/editpsi/Tests.kt | 7 +++- .../{What is PSI => IntroToPsi}/src/Person.kt | 0 .../task-info.yaml | 1 + .../psi/{What is PSI => IntroToPsi}/task.md | 0 .../{addPSI => SortMethods}/task-info.yaml | 5 +++ .../plugindev/course/addpsi/Tests.kt | 42 +++++++++++++++++++ .../src/Person.kt | 0 .../task-info.yaml | 2 + .../{Try PSI Viewer => TryPsiViewer}/task.md | 0 .../plugindev/course/editpsi/Task.kt | 20 --------- courseSection/psi/lesson-info.yaml | 13 +++--- 24 files changed, 100 insertions(+), 84 deletions(-) rename courseSection/psi/{Accessing PSI Elements => AccessingPsiElements}/src/jetbrains/plugindev/course/access/Main.kt (100%) rename courseSection/psi/{Accessing PSI Elements => AccessingPsiElements}/task-info.yaml (72%) rename courseSection/psi/{Accessing PSI Elements => AccessingPsiElements}/task.md (100%) rename courseSection/psi/{addPSI/src/jetbrains/plugindev/course/addpsi => AddAnnotations/src/org/jetbrains/academy/ij/plugin/course/annotations}/Task.kt (74%) create mode 100644 courseSection/psi/AddAnnotations/task-info.yaml rename courseSection/psi/{addPSI => AddAnnotations}/task.md (100%) rename courseSection/psi/{addPSI/test/jetbrains/plugindev/course/addpsi => AddAnnotations/test/org/jetbrains/academy/ij/plugin/course/annotations}/Tests.kt (55%) rename courseSection/psi/{classCounter => ClassCounter}/src/jetbrains/plugindev/course/classcounter/Task.kt (100%) rename courseSection/psi/{classCounter => ClassCounter}/task-info.yaml (75%) rename courseSection/psi/{Count number of classes => ClassCounter}/task.md (99%) rename courseSection/psi/{classCounter => ClassCounter}/test/jetbrains/plugindev/course/classcounter/Tests.kt (100%) create mode 100644 courseSection/psi/EditPSI/src/jetbrains/plugindev/course/editpsi/Task.kt rename courseSection/psi/{editPSI => EditPSI}/task-info.yaml (56%) rename courseSection/psi/{editPSI => EditPSI}/test/jetbrains/plugindev/course/editpsi/Tests.kt (79%) rename courseSection/psi/{What is PSI => IntroToPsi}/src/Person.kt (100%) rename courseSection/psi/{What is PSI => IntroToPsi}/task-info.yaml (70%) rename courseSection/psi/{What is PSI => IntroToPsi}/task.md (100%) rename courseSection/psi/{addPSI => SortMethods}/task-info.yaml (56%) create mode 100644 courseSection/psi/SortMethods/test/jetbrains/plugindev/course/addpsi/Tests.kt rename courseSection/psi/{Try PSI Viewer => TryPsiViewer}/src/Person.kt (100%) rename courseSection/psi/{Try PSI Viewer => TryPsiViewer}/task-info.yaml (93%) rename courseSection/psi/{Try PSI Viewer => TryPsiViewer}/task.md (100%) delete mode 100644 courseSection/psi/editPSI/src/jetbrains/plugindev/course/editpsi/Task.kt diff --git a/courseSection/psi/Accessing PSI Elements/src/jetbrains/plugindev/course/access/Main.kt b/courseSection/psi/AccessingPsiElements/src/jetbrains/plugindev/course/access/Main.kt similarity index 100% rename from courseSection/psi/Accessing PSI Elements/src/jetbrains/plugindev/course/access/Main.kt rename to courseSection/psi/AccessingPsiElements/src/jetbrains/plugindev/course/access/Main.kt diff --git a/courseSection/psi/Accessing PSI Elements/task-info.yaml b/courseSection/psi/AccessingPsiElements/task-info.yaml similarity index 72% rename from courseSection/psi/Accessing PSI Elements/task-info.yaml rename to courseSection/psi/AccessingPsiElements/task-info.yaml index 49a98d6..6daedda 100644 --- a/courseSection/psi/Accessing PSI Elements/task-info.yaml +++ b/courseSection/psi/AccessingPsiElements/task-info.yaml @@ -1,4 +1,5 @@ type: theory +custom_name: Accessing PSI Elements files: - name: src/jetbrains/plugindev/course/access/Main.kt visible: true diff --git a/courseSection/psi/Accessing PSI Elements/task.md b/courseSection/psi/AccessingPsiElements/task.md similarity index 100% rename from courseSection/psi/Accessing PSI Elements/task.md rename to courseSection/psi/AccessingPsiElements/task.md diff --git a/courseSection/psi/addPSI/src/jetbrains/plugindev/course/addpsi/Task.kt b/courseSection/psi/AddAnnotations/src/org/jetbrains/academy/ij/plugin/course/annotations/Task.kt similarity index 74% rename from courseSection/psi/addPSI/src/jetbrains/plugindev/course/addpsi/Task.kt rename to courseSection/psi/AddAnnotations/src/org/jetbrains/academy/ij/plugin/course/annotations/Task.kt index 7265ff7..ee0e83d 100644 --- a/courseSection/psi/addPSI/src/jetbrains/plugindev/course/addpsi/Task.kt +++ b/courseSection/psi/AddAnnotations/src/org/jetbrains/academy/ij/plugin/course/annotations/Task.kt @@ -1,4 +1,4 @@ -package jetbrains.plugindev.course.addpsi +package org.jetbrains.academy.ij.plugin.course.annotations import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.project.Project @@ -8,27 +8,6 @@ import com.intellij.psi.PsiMethod import com.intellij.psi.PsiType import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.TypeConversionUtil -import org.jetbrains.kotlin.psi.KtClass -import org.jetbrains.kotlin.psi.KtNamedFunction - - -// type your solution here - -fun sortMethods(psiFile: PsiFile) { - val project = psiFile.project - WriteCommandAction.runWriteCommandAction(project) { - val classes = PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java) - - for (ktClass in classes){ - val methods = ktClass.declarations.filterIsInstance() - val sortedMethods = methods.sortedBy { it.name }.map { it.copy() as KtNamedFunction } - - methods.zip(sortedMethods).forEach { (original, sortedCopy) -> - original.replace(sortedCopy) - } - } - } -} fun addAnnotations(psiFile: PsiFile){ val project = psiFile.project diff --git a/courseSection/psi/AddAnnotations/task-info.yaml b/courseSection/psi/AddAnnotations/task-info.yaml new file mode 100644 index 0000000..ec87197 --- /dev/null +++ b/courseSection/psi/AddAnnotations/task-info.yaml @@ -0,0 +1,11 @@ +type: edu +custom_name: Psi Manipulation Advanced +files: + - name: src/org/jetbrains/academy/ij/plugin/course/annotations/Task.kt + visible: true + placeholders: + - offset: 871 + length: 247 + placeholder_text: TODO() + - name: test/org/jetbrains/academy/ij/plugin/course/annotations/Tests.kt + visible: false diff --git a/courseSection/psi/addPSI/task.md b/courseSection/psi/AddAnnotations/task.md similarity index 100% rename from courseSection/psi/addPSI/task.md rename to courseSection/psi/AddAnnotations/task.md diff --git a/courseSection/psi/addPSI/test/jetbrains/plugindev/course/addpsi/Tests.kt b/courseSection/psi/AddAnnotations/test/org/jetbrains/academy/ij/plugin/course/annotations/Tests.kt similarity index 55% rename from courseSection/psi/addPSI/test/jetbrains/plugindev/course/addpsi/Tests.kt rename to courseSection/psi/AddAnnotations/test/org/jetbrains/academy/ij/plugin/course/annotations/Tests.kt index c5b7fff..e9fac4e 100644 --- a/courseSection/psi/addPSI/test/jetbrains/plugindev/course/addpsi/Tests.kt +++ b/courseSection/psi/AddAnnotations/test/org/jetbrains/academy/ij/plugin/course/annotations/Tests.kt @@ -1,42 +1,14 @@ -package jetbrains.plugindev.course.addpsi +package org.jetbrains.academy.ij.plugin.course.annotations + import com.intellij.ide.highlighter.JavaFileType import com.intellij.psi.PsiClass import com.intellij.psi.PsiMethod import com.intellij.psi.util.PsiTreeUtil import com.intellij.testFramework.fixtures.BasePlatformTestCase -import org.jetbrains.kotlin.psi.KtClass -import org.jetbrains.kotlin.psi.KtNamedFunction - -class Test : BasePlatformTestCase() { - - fun testSolution() { - var file = myFixture.configureByText("MyClass.kt", """ - class MyClass { - fun abcd() { - println("Hello") - } - - fun zhas() { - println("Goodbye") - } - - fun mid() { - println("Goodbye") - } - } - - """.trimIndent()) - sortMethods(file) - val classes = PsiTreeUtil.findChildrenOfType(file, KtClass::class.java) - for (ktClass in classes){ - val methods = ktClass.declarations.filterIsInstance() - val sorted = methods.sortedBy { it.name } - assertEquals(sorted, methods) - } - } +class Test : BasePlatformTestCase() { fun testAnnotation() { val javaFile = myFixture.configureByText( @@ -46,7 +18,6 @@ class Test : BasePlatformTestCase() { } class SubClass extends SuperClass { - public void testMethod() {} } """.trimIndent()) diff --git a/courseSection/psi/classCounter/src/jetbrains/plugindev/course/classcounter/Task.kt b/courseSection/psi/ClassCounter/src/jetbrains/plugindev/course/classcounter/Task.kt similarity index 100% rename from courseSection/psi/classCounter/src/jetbrains/plugindev/course/classcounter/Task.kt rename to courseSection/psi/ClassCounter/src/jetbrains/plugindev/course/classcounter/Task.kt diff --git a/courseSection/psi/classCounter/task-info.yaml b/courseSection/psi/ClassCounter/task-info.yaml similarity index 75% rename from courseSection/psi/classCounter/task-info.yaml rename to courseSection/psi/ClassCounter/task-info.yaml index 2736283..0ab9c53 100644 --- a/courseSection/psi/classCounter/task-info.yaml +++ b/courseSection/psi/ClassCounter/task-info.yaml @@ -1,10 +1,11 @@ type: edu +custom_name: Count number of classes files: - name: src/jetbrains/plugindev/course/classcounter/Task.kt visible: true placeholders: - - offset: 160 - length: 85 + - offset: 211 + length: 81 placeholder_text: TODO() - name: test/jetbrains/plugindev/course/classcounter/Tests.kt visible: false diff --git a/courseSection/psi/Count number of classes/task.md b/courseSection/psi/ClassCounter/task.md similarity index 99% rename from courseSection/psi/Count number of classes/task.md rename to courseSection/psi/ClassCounter/task.md index c75a7d2..60b833d 100644 --- a/courseSection/psi/Count number of classes/task.md +++ b/courseSection/psi/ClassCounter/task.md @@ -1,6 +1,7 @@ You need to implement a function `countKtClasses` which will count number of kotlin classes declared in the given kotlin PSI file.
+ Try to use `KtClass::class.java` value for aClass parameter for `findChildrenOfType`
diff --git a/courseSection/psi/classCounter/test/jetbrains/plugindev/course/classcounter/Tests.kt b/courseSection/psi/ClassCounter/test/jetbrains/plugindev/course/classcounter/Tests.kt similarity index 100% rename from courseSection/psi/classCounter/test/jetbrains/plugindev/course/classcounter/Tests.kt rename to courseSection/psi/ClassCounter/test/jetbrains/plugindev/course/classcounter/Tests.kt diff --git a/courseSection/psi/EditPSI/src/jetbrains/plugindev/course/editpsi/Task.kt b/courseSection/psi/EditPSI/src/jetbrains/plugindev/course/editpsi/Task.kt new file mode 100644 index 0000000..ed209f2 --- /dev/null +++ b/courseSection/psi/EditPSI/src/jetbrains/plugindev/course/editpsi/Task.kt @@ -0,0 +1,13 @@ +package jetbrains.plugindev.course.editpsi + +import com.intellij.openapi.command.WriteCommandAction +import org.jetbrains.kotlin.psi.KtNamedFunction + + +fun editFunctionName(function: KtNamedFunction, newName: String) { + val project = function.project + + WriteCommandAction.runWriteCommandAction(project) { + function.setName(newName) + } +} \ No newline at end of file diff --git a/courseSection/psi/editPSI/task-info.yaml b/courseSection/psi/EditPSI/task-info.yaml similarity index 56% rename from courseSection/psi/editPSI/task-info.yaml rename to courseSection/psi/EditPSI/task-info.yaml index f551a04..a079302 100644 --- a/courseSection/psi/editPSI/task-info.yaml +++ b/courseSection/psi/EditPSI/task-info.yaml @@ -1,6 +1,11 @@ type: edu +custom_name: Rename function using PSI files: - name: src/jetbrains/plugindev/course/editpsi/Task.kt visible: true + placeholders: + - offset: 220 + length: 127 + placeholder_text: TODO() - name: test/jetbrains/plugindev/course/editpsi/Tests.kt visible: false diff --git a/courseSection/psi/editPSI/test/jetbrains/plugindev/course/editpsi/Tests.kt b/courseSection/psi/EditPSI/test/jetbrains/plugindev/course/editpsi/Tests.kt similarity index 79% rename from courseSection/psi/editPSI/test/jetbrains/plugindev/course/editpsi/Tests.kt rename to courseSection/psi/EditPSI/test/jetbrains/plugindev/course/editpsi/Tests.kt index 9cf7146..fbbd25d 100644 --- a/courseSection/psi/editPSI/test/jetbrains/plugindev/course/editpsi/Tests.kt +++ b/courseSection/psi/EditPSI/test/jetbrains/plugindev/course/editpsi/Tests.kt @@ -17,9 +17,12 @@ class Test : BasePlatformTestCase() { } """.trimIndent()) - editFunctionName(file, "sayHello", "greetings") - val functions = PsiTreeUtil.findChildrenOfType(file, KtNamedFunction::class.java) + val functionToRename = functions.find { it.name == "sayHello" } + if (functionToRename != null) { + editFunctionName(functionToRename, "greetings") + } + val hasNewFunction = functions.any { it.name == "greetings" } assertTrue(hasNewFunction) } diff --git a/courseSection/psi/What is PSI/src/Person.kt b/courseSection/psi/IntroToPsi/src/Person.kt similarity index 100% rename from courseSection/psi/What is PSI/src/Person.kt rename to courseSection/psi/IntroToPsi/src/Person.kt diff --git a/courseSection/psi/What is PSI/task-info.yaml b/courseSection/psi/IntroToPsi/task-info.yaml similarity index 70% rename from courseSection/psi/What is PSI/task-info.yaml rename to courseSection/psi/IntroToPsi/task-info.yaml index f50926e..e5fdfa5 100644 --- a/courseSection/psi/What is PSI/task-info.yaml +++ b/courseSection/psi/IntroToPsi/task-info.yaml @@ -1,4 +1,5 @@ type: theory +custom_name: What is PSI? files: - name: src/Person.kt visible: true diff --git a/courseSection/psi/What is PSI/task.md b/courseSection/psi/IntroToPsi/task.md similarity index 100% rename from courseSection/psi/What is PSI/task.md rename to courseSection/psi/IntroToPsi/task.md diff --git a/courseSection/psi/addPSI/task-info.yaml b/courseSection/psi/SortMethods/task-info.yaml similarity index 56% rename from courseSection/psi/addPSI/task-info.yaml rename to courseSection/psi/SortMethods/task-info.yaml index 6927d9a..19e1c16 100644 --- a/courseSection/psi/addPSI/task-info.yaml +++ b/courseSection/psi/SortMethods/task-info.yaml @@ -1,6 +1,11 @@ type: edu +custom_name: Psi Manipulation Basics files: - name: src/jetbrains/plugindev/course/addpsi/Task.kt visible: true + placeholders: + - offset: 228 + length: 387 + placeholder_text: TODO() - name: test/jetbrains/plugindev/course/addpsi/Tests.kt visible: false diff --git a/courseSection/psi/SortMethods/test/jetbrains/plugindev/course/addpsi/Tests.kt b/courseSection/psi/SortMethods/test/jetbrains/plugindev/course/addpsi/Tests.kt new file mode 100644 index 0000000..3c54bdd --- /dev/null +++ b/courseSection/psi/SortMethods/test/jetbrains/plugindev/course/addpsi/Tests.kt @@ -0,0 +1,42 @@ +package jetbrains.plugindev.course.addpsi + +import com.intellij.ide.highlighter.JavaFileType +import com.intellij.psi.PsiClass +import com.intellij.psi.PsiMethod +import com.intellij.psi.util.PsiTreeUtil +import com.intellij.testFramework.fixtures.BasePlatformTestCase +import org.jetbrains.kotlin.psi.KtClass +import org.jetbrains.kotlin.psi.KtNamedFunction + +class Test : BasePlatformTestCase() { + + fun testSolution() { + var file = myFixture.configureByText("Person.kt", """ + class Person { + fun name() { + println("My name is John") + } + + fun surname() { + println("My surname is Johnson") + } + + fun age() { + println("I am 18 years old") + } + } + + """.trimIndent()) + + val classes = PsiTreeUtil.findChildrenOfType(file, KtClass::class.java) + for (ktClass in classes){ + sortMethods(ktClass) + + val methods = ktClass.declarations.filterIsInstance() + val sorted = methods.sortedBy { it.name } + assertEquals(sorted, methods) + } + } + + +} \ No newline at end of file diff --git a/courseSection/psi/Try PSI Viewer/src/Person.kt b/courseSection/psi/TryPsiViewer/src/Person.kt similarity index 100% rename from courseSection/psi/Try PSI Viewer/src/Person.kt rename to courseSection/psi/TryPsiViewer/src/Person.kt diff --git a/courseSection/psi/Try PSI Viewer/task-info.yaml b/courseSection/psi/TryPsiViewer/task-info.yaml similarity index 93% rename from courseSection/psi/Try PSI Viewer/task-info.yaml rename to courseSection/psi/TryPsiViewer/task-info.yaml index 1cd6521..765a56a 100644 --- a/courseSection/psi/Try PSI Viewer/task-info.yaml +++ b/courseSection/psi/TryPsiViewer/task-info.yaml @@ -1,4 +1,5 @@ type: choice +custom_name: Try Psi Viewer is_multiple_choice: true options: - text: org.jetbrains.kotlin.psi.KtClass @@ -13,3 +14,4 @@ files: - name: src/Person.kt visible: true local_check: true + diff --git a/courseSection/psi/Try PSI Viewer/task.md b/courseSection/psi/TryPsiViewer/task.md similarity index 100% rename from courseSection/psi/Try PSI Viewer/task.md rename to courseSection/psi/TryPsiViewer/task.md diff --git a/courseSection/psi/editPSI/src/jetbrains/plugindev/course/editpsi/Task.kt b/courseSection/psi/editPSI/src/jetbrains/plugindev/course/editpsi/Task.kt deleted file mode 100644 index 357abf5..0000000 --- a/courseSection/psi/editPSI/src/jetbrains/plugindev/course/editpsi/Task.kt +++ /dev/null @@ -1,20 +0,0 @@ -package jetbrains.plugindev.course.editpsi - -import com.intellij.openapi.command.WriteCommandAction -import com.intellij.psi.PsiFile -import com.intellij.psi.util.PsiTreeUtil -import org.jetbrains.kotlin.psi.KtNamedFunction - - -fun editFunctionName(psiFile: PsiFile, currentName: String, newName: String) { - val project = psiFile.project - - WriteCommandAction.runWriteCommandAction(project) { - val functions = PsiTreeUtil.findChildrenOfType(psiFile, KtNamedFunction::class.java) - for (function in functions) { - if (function.name == currentName) { - function.setName(newName) - } - } - } -} \ No newline at end of file diff --git a/courseSection/psi/lesson-info.yaml b/courseSection/psi/lesson-info.yaml index d2cfc68..3aed340 100644 --- a/courseSection/psi/lesson-info.yaml +++ b/courseSection/psi/lesson-info.yaml @@ -1,8 +1,9 @@ custom_name: PSI content: - - What is PSI - - Try PSI Viewer - - Accessing PSI Elements - - classCounter - - editPSI - - addPSI + - IntroToPsi + - TryPsiViewer + - AccessingPsiElements + - ClassCounter + - EditPSI + - SortMethods + - AddAnnotations