1. 程式人生 > >IDEA 外掛開發,工具方法

IDEA 外掛開發,工具方法


    public static PsiClass getPsiClass(AnActionEvent e) {
        PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        if (psiFile == null || editor == null) {
            return null;
        }
        int offset = editor.getCaretModel().getOffset();
        PsiElement element = psiFile.findElementAt(offset);
        return PsiTreeUtil.getParentOfType(element, PsiClass.class);
    }

    /**
     * 獲取類的所有屬性
     *
     * @param e
     * @return
     */
    public static List<PsiField> getFields(AnActionEvent e) {
        PsiClass clazz = getPsiClass(e);
        return getFields(clazz);
    }

    public static List<PsiField> getFields(PsiClass clazz) {
        List<PsiField> result = new ArrayList<>();
        PsiField[] fields = clazz.getFields();
        for (PsiField field : fields) {
            String fieldName = field.getName();
            if (!Constants.EXCLUSION_FIELD.contains(fieldName)) {
                result.add(field);
            }
        }
        return result;
    }

    public static ClassBean getClassBean(AnActionEvent e) {
        List<PsiMethod> list = new ArrayList<>();
        PsiClass clazz = getPsiClass(e);
        String classFullName = clazz.getQualifiedName();
        PsiMethod[] methodList = clazz.getMethods();
        list.addAll(Arrays.asList(methodList));
        return new ClassBean(classFullName, list);
    }

    public static boolean isInterface(AnActionEvent e) {
        PsiClass clazz = getPsiClass(e);
        return clazz.isInterface();
    }

    public static PsiMethod[] getMethod(String getMethodName, AnActionEvent e) {
        PsiClass clazz = getPsiClass(e);
        PsiMethod[] methods = clazz.findMethodsByName(getMethodName, true);
        //boolean isPublic = field.hasModifierProperty(PsiModifier.PUBLIC);
        //if (isPublic) { return true; }
        return methods;
    }

    public static PsiClass getClassByName(String className, PsiClass containingClass, Project project) {
        PsiClass[] classes = PsiShortNamesCache.getInstance(project).getClassesByName(className,
            GlobalSearchScope.projectScope(project));
        if (classes.length == 0) {
            return null;
        }
        if (classes.length == 1) {
            return classes[0];
        }
        PsiJavaFile javaFile = (PsiJavaFile)containingClass.getContainingFile();
        PsiImportList importList = javaFile.getImportList();

        //獲取所有引用
        Set<String> importedPackageSet = new HashSet();
        for (PsiImportStatement statement : importList.getImportStatements()) {
            importedPackageSet.add(statement.getQualifiedName());
        }
        for (PsiClass targetClass : classes) {
            PsiJavaFile targetClassContainingFile = (PsiJavaFile)targetClass.getContainingFile();
            String packageName = targetClassContainingFile.getPackageName();
            if (importedPackageSet.contains(packageName + "." + targetClass.getName())) {
                return targetClass;
            }
        }
        return null;
    }



    public static MethodDoc getDocComments(PsiDocComment docComment) {
        MethodDoc methodDoc = new MethodDoc();

        if (docComment == null || CommonUtil.isEmpty(docComment.getText())) {
            methodDoc.setMethodDoc(METHOD_DOC);
        } else {
            Map<String, String> result = new HashMap();
            try {
                String[] textArray = docComment.getText().split("\n");
                for (int i = 0; i < textArray.length; i++) {
                    String line = textArray[i].trim();
                    line = line.replaceAll("(?:\\*|\\/\\*\\*|@param|@return|\\/)", "");
                    if (!CommonUtil.isEmpty(line)) {
                        ArrayList<String> list = new ArrayList<>();
                        list.addAll(Arrays.asList(line.split(" ")));
                        Iterator iter = list.iterator();
                        while (iter.hasNext()) {
                            if (CommonUtil.isEmpty((String)iter.next())) {
                                iter.remove();
                            }
                        }
                        if (list.size() == 2) {
                            result.put(list.get(0), list.get(1));
                        }
                        if (i == 1) {
                            methodDoc.setMethodDoc(line);
                        }
                    }
                }
            } catch (Exception e) {
                LOGGER.error("[getDocComments] error,e={}", e);
            }
            methodDoc.setFieldDoc(result);
        }
        return methodDoc;
    }

 public static final NotificationGroup GROUP_DISPLAY_ID_INFO = new NotificationGroup("com.alibaba.scrat.tools",
        NotificationDisplayType.STICKY_BALLOON, true);

    public static void toClipboard(String str, Project project) {
        StringSelection selection = new StringSelection(str);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, selection);
        Notification notification = GROUP_DISPLAY_ID_INFO.createNotification(SUCCESS_TO_CLIPBOARD,
            NotificationType.INFORMATION);
        Notifications.Bus.notify(notification, project);
    }

    public static void fail(Project project) {
        Notification notification = GROUP_DISPLAY_ID_INFO.createNotification("失敗", NotificationType.ERROR);
        Notifications.Bus.notify(notification, project);
    }

 public static SettingData getSettingData() {
        if (PropertiesComponent.getInstance().isValueSet(SAVE_KEY)) {
            String res = PropertiesComponent.getInstance().getValue(SAVE_KEY);
            Gson gson = new Gson();
            return gson.fromJson(res, SettingData.class);
        }
        return new SettingData();
    }

    public static void setSettingData(SettingData settingData) {
        Gson gson = new Gson();
        String values = gson.toJson(settingData);
        PropertiesComponent.getInstance().setValue(SAVE_KEY, values);
    }