From b2cbe7fe552b42760bf05592875b3a69b0174061 Mon Sep 17 00:00:00 2001 From: Valerii Date: Tue, 5 Dec 2023 20:44:42 +0100 Subject: [PATCH] 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