From 8c3f0d4fdef4381dfdcd74dc131406ddf250d5ad Mon Sep 17 00:00:00 2001 From: wangchao <760823254@qq.com> Date: Sat, 3 Apr 2021 02:28:56 +0800 Subject: [PATCH] 1.10-unit test --- build.gradle | 1 + .../action/GenerateUnitTestTemplateAction.java | 15 ++++++++++++++- src/main/resources/template/unit-test.vm | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 2ae05d6..3ae9e72 100644 --- a/build.gradle +++ b/build.gradle @@ -20,6 +20,7 @@ dependencies { } // See https://github.com/JetBrains/gradle-intellij-plugin/ +// all versions https://data.services.jetbrains.com/products?fields=code,name,releases.downloads,releases.version,releases.build,releases.type&code=DG,PS,DL,MPS,MPSIIP,RDCPPP,WS,AC,IIC,IIE,GO,CL,PCD,PCC,RD,PCE,IIU,RM,PCP intellij { // plugins 'java' version '2019.1' diff --git a/src/main/java/com/star/easygenerate/action/GenerateUnitTestTemplateAction.java b/src/main/java/com/star/easygenerate/action/GenerateUnitTestTemplateAction.java index 0ab6d7b..00676fe 100644 --- a/src/main/java/com/star/easygenerate/action/GenerateUnitTestTemplateAction.java +++ b/src/main/java/com/star/easygenerate/action/GenerateUnitTestTemplateAction.java @@ -9,6 +9,7 @@ import java.util.stream.Collectors; import com.google.common.collect.Maps; +import com.intellij.codeInsight.navigation.NavigationUtil; import com.intellij.ide.fileTemplates.FileTemplateUtil; import com.intellij.ide.fileTemplates.impl.CustomFileTemplate; import com.intellij.openapi.actionSystem.AnAction; @@ -21,6 +22,8 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.JavaDirectoryService; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClass; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiField; import com.intellij.psi.PsiFile; @@ -28,6 +31,8 @@ import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiModifier; import com.intellij.psi.PsiPackage; +import com.intellij.psi.impl.source.PsiJavaFileImpl; +import com.intellij.psi.search.GlobalSearchScope; import com.intellij.refactoring.util.classMembers.MemberInfo; import com.intellij.util.ResourceUtil; import com.star.easygenerate.dialog.CreateUnitTestDialog; @@ -83,6 +88,14 @@ public void actionPerformed(AnActionEvent e) { } catch (Exception exception) { LOGGER.error("create template error:", exception); } + + // 跳转到类上 + String tgtClassName = ((PsiJavaFileImpl)dialog.getTargetClass().getParent()).getPackageName() + "." + dialog.getClassName(); + PsiClass testClass = JavaPsiFacade.getInstance(project).findClass(tgtClassName, GlobalSearchScope.allScope(project)); + if (testClass == null) { + return; + } + NavigationUtil.activateFileWithPsiElement(testClass, true); } /** @@ -162,7 +175,7 @@ private Map getParamMap(PsiJavaFile psiJavaFile, CreateUnitTestD params.put("user", System.getProperty("user.name")); params.put("date", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE)); params.put("className", className); - params.put("package", dialog.getTargetDirectory().getName()); + params.put("package", ((PsiJavaFileImpl)dialog.getTargetClass().getParent()).getPackageName()); params.put("instanceName", StringUtils.substring(className, 0, 1).toLowerCase() + StringUtils.substring(className, 1)); params.put("testClassName", dialog.getClassName()); params.put("fieldList", fields); diff --git a/src/main/resources/template/unit-test.vm b/src/main/resources/template/unit-test.vm index 9170681..f33037d 100644 --- a/src/main/resources/template/unit-test.vm +++ b/src/main/resources/template/unit-test.vm @@ -10,7 +10,7 @@ import ${field.packageName}; #end /** - * {@link ${package}.${className}}的单元测试类 + * {@link ${className}}的单元测试类 * * @author ${user} * @date ${date}