Skip to content

Commit

Permalink
sort methods done
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerii committed Dec 5, 2023
1 parent b0f3619 commit b2cbe7f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
24 changes: 24 additions & 0 deletions courseSection/psi/addPSI/src/Task.kt
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)
}
}
}
}
6 changes: 6 additions & 0 deletions courseSection/psi/addPSI/task-info.yaml
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
48 changes: 48 additions & 0 deletions courseSection/psi/addPSI/task.md
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 &percnt;`IDE_NAME`&percnt; 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)`.
35 changes: 35 additions & 0 deletions courseSection/psi/addPSI/test/Tests.kt
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)
}
}
}
1 change: 1 addition & 0 deletions courseSection/psi/lesson-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ content:
- Accessing PSI Elements
- Count number of classes
- editPSI
- addPSI

0 comments on commit b2cbe7f

Please sign in to comment.