generated from jetbrains-academy/kotlin-course-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Valerii
committed
Dec 5, 2023
1 parent
b0f3619
commit b2cbe7f
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<KtNamedFunction>() | ||
val sortedMethods = methods.sortedBy { it.name }.map { it.copy() as KtNamedFunction } | ||
|
||
methods.zip(sortedMethods).forEach { (original, sortedCopy) -> | ||
original.replace(sortedCopy) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type: edu | ||
files: | ||
- name: src/Task.kt | ||
visible: true | ||
- name: test/Tests.kt | ||
visible: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
<div class="hint"> | ||
|
||
Text of your hint | ||
|
||
</div> | ||
|
||
- 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)`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<KtNamedFunction>() | ||
val sorted = methods.sortedBy { it.name } | ||
assertEquals(sorted, methods) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ content: | |
- Accessing PSI Elements | ||
- Count number of classes | ||
- editPSI | ||
- addPSI |