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.
add ui for sort methods and also change the function signature
- Loading branch information
Valerii
committed
Jan 15, 2024
1 parent
39db65e
commit 9d27f4a
Showing
4 changed files
with
75 additions
and
12 deletions.
There are no files selected for viewing
18 changes: 6 additions & 12 deletions
18
...gTask/src/main/kotlin/org/jetbrains/academy/plugin/course/dev/access/PsiElementsSorter.kt
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 |
---|---|---|
@@ -1,23 +1,17 @@ | ||
package org.jetbrains.academy.plugin.course.dev.access | ||
|
||
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 | ||
|
||
fun sortMethods(psiFile: PsiFile) { | ||
val project = psiFile.project | ||
fun sortMethods(ktClass: KtClass) { | ||
val project = ktClass.project | ||
WriteCommandAction.runWriteCommandAction(project) { | ||
val classes = PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java) | ||
val methods = ktClass.declarations.filterIsInstance<KtNamedFunction>() | ||
val sortedMethods = methods.sortedBy { it.name }.map { it.copy() as KtNamedFunction } | ||
|
||
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) | ||
} | ||
methods.zip(sortedMethods).forEach { (original, sortedCopy) -> | ||
original.replace(sortedCopy) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...mmingTask/src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/SortMethodsAction.kt
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,42 @@ | ||
package org.jetbrains.academy.plugin.course.dev.ui | ||
|
||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.actionSystem.CommonDataKeys | ||
import com.intellij.openapi.command.WriteCommandAction | ||
import org.jetbrains.academy.plugin.course.dev.access.sortMethods | ||
import org.jetbrains.kotlin.psi.KtClass | ||
|
||
class SortMethodsAction : AnAction() { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
val project = e.project ?: return | ||
val editor = e.getData(CommonDataKeys.EDITOR) ?: return | ||
val psiFile = e.getData(CommonDataKeys.PSI_FILE) ?: return | ||
|
||
val caret = editor.caretModel.currentCaret | ||
val element = psiFile.findElementAt(caret.offset) ?: return | ||
|
||
val ktClass = element.parent as? KtClass ?: return | ||
|
||
// Call your method sorting function here | ||
// sortMethodsInClass(ktClass) | ||
|
||
WriteCommandAction.runWriteCommandAction(project) { | ||
// Execute your sorting logic here | ||
sortMethods(ktClass) | ||
} | ||
} | ||
|
||
override fun update(event: AnActionEvent) { | ||
val presentation = event.presentation | ||
presentation.isEnabledAndVisible = false | ||
|
||
val editor = event.getData(CommonDataKeys.EDITOR) ?: return | ||
val psiFile = event.getData(CommonDataKeys.PSI_FILE) ?: return | ||
val elementAtCaret = psiFile.findElementAt(editor.caretModel.offset) | ||
|
||
if (elementAtCaret?.parent is KtClass) { | ||
presentation.isEnabledAndVisible = true | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ditingPsiElementsLesson/sortMethodsProgrammingTask/src/main/resources/META-INF/plugin.xml
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,23 @@ | ||
<idea-plugin> | ||
<id>jetbrains.academy.plugin.course.dev.ui.demo</id> | ||
<name>IDE Development Course Demo</name> | ||
<vendor>JetBrains</vendor> | ||
|
||
<depends>com.intellij.modules.platform</depends> | ||
<depends>org.jetbrains.kotlin</depends> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
<notificationGroup id="IdeDevCourseDemo" displayType="BALLOON"/> | ||
|
||
</extensions> | ||
<actions> | ||
<!-- Add your action here --> | ||
<action id="SortMethods" | ||
class="org.jetbrains.academy.plugin.course.dev.ui.SortMethodsAction" | ||
text="Sort Methods" | ||
description="Sort methods inside this class"> | ||
<add-to-group group-id="EditorPopupMenu" anchor="last"/> | ||
</action> | ||
</actions> | ||
|
||
</idea-plugin> |
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