Skip to content

Commit

Permalink
add ui for sort methods and also change the function signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerii committed Jan 15, 2024
1 parent 39db65e commit 9d27f4a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
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)
}
}
}
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
}
}
}
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>
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ files:
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/access/PsiElementsSorter.kt
visible: true
- name: src/main/resources/META-INF/plugin.xml
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/SortMethodsAction.kt
visible: false

0 comments on commit 9d27f4a

Please sign in to comment.