From 22f8a5dcaf93e243b9d41c2384a49d5d48022420 Mon Sep 17 00:00:00 2001 From: dcsmf <1148202591@qq.com> Date: Wed, 14 Jun 2023 16:55:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AA=E5=81=9A=E5=AE=8C=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../action/FormatMethodWithPyramidAction.kt | 13 +++++++++--- .../utils/MethodUtil.kt | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/com/github/dcsmf/intellijformatmethodsplugin/utils/MethodUtil.kt diff --git a/src/main/kotlin/com/github/dcsmf/intellijformatmethodsplugin/action/FormatMethodWithPyramidAction.kt b/src/main/kotlin/com/github/dcsmf/intellijformatmethodsplugin/action/FormatMethodWithPyramidAction.kt index dd08556..c9c377c 100644 --- a/src/main/kotlin/com/github/dcsmf/intellijformatmethodsplugin/action/FormatMethodWithPyramidAction.kt +++ b/src/main/kotlin/com/github/dcsmf/intellijformatmethodsplugin/action/FormatMethodWithPyramidAction.kt @@ -4,6 +4,7 @@ import com.github.dcsmf.intellijformatmethodsplugin.bundle.TextBundle import com.github.dcsmf.intellijformatmethodsplugin.model.InsertType import com.github.dcsmf.intellijformatmethodsplugin.model.SelectSortModel import com.github.dcsmf.intellijformatmethodsplugin.utils.ElementUtil +import com.github.dcsmf.intellijformatmethodsplugin.utils.MethodUtil.getJvmStyleSignature import com.github.dcsmf.intellijformatmethodsplugin.utils.NotifyUtil import com.github.dcsmf.intellijformatmethodsplugin.utils.SortUtil import com.intellij.openapi.actionSystem.AnAction @@ -78,15 +79,21 @@ class FormatMethodWithPyramidAction : AnAction() { if (methods.isEmpty()) { return 0 } + var sortModel: SelectSortModel? = null if (start != -1 || end != -1) { sortModel = SortUtil.getSelectSortModel(currentPsiClass, start, end, methods.toList()) methods = methods.filter { it.textRange.endOffset in start..end }.toTypedArray() } val sortedMethods = methods.stream().sorted { o1, o2 -> - o1.getSignature(PsiSubstitutor.EMPTY).toString().length.compareTo( - o2.getSignature(PsiSubstitutor.EMPTY).toString().length - ) + if (o1.isConstructor) { + return@sorted 1 + } + val s1 = getJvmStyleSignature(o1) + val s2 = getJvmStyleSignature(o2) + +// s1.length.compareTo(s2.length) + 1 }.collect(Collectors.toList()) if (SortUtil.isSameAfterSort(methods, sortedMethods)) { return 0 diff --git a/src/main/kotlin/com/github/dcsmf/intellijformatmethodsplugin/utils/MethodUtil.kt b/src/main/kotlin/com/github/dcsmf/intellijformatmethodsplugin/utils/MethodUtil.kt new file mode 100644 index 0000000..75c65b5 --- /dev/null +++ b/src/main/kotlin/com/github/dcsmf/intellijformatmethodsplugin/utils/MethodUtil.kt @@ -0,0 +1,20 @@ +package com.github.dcsmf.intellijformatmethodsplugin.utils + +import com.intellij.psi.PsiMethod + +object MethodUtil { + /** + * 构建带权限标识符、返回类型、泛型声明、函数名、参数列表和throws的JVM风格函数定义 + * default void buildDefaultField(String userId, @NotNull Group group, T t) throws Exception {System.out.println(t);} + * + */ + @JvmStatic + fun getJvmStyleSignature(method: PsiMethod): String { + if(method.isConstructor){ + val returnTypeElement=method.returnTypeElement + } + return with(method) { + modifierList.text + } + } +} \ No newline at end of file