Skip to content

Commit

Permalink
支持 swagger 修改注释
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhihang committed Aug 5, 2021
1 parent 4be8239 commit c090567
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 82 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/liuzhihang/doc/view/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.liuzhihang.doc.view.constant.Constant;
import com.liuzhihang.doc.view.constant.LombokConstant;
import com.liuzhihang.doc.view.constant.SpringConstant;
import com.liuzhihang.doc.view.constant.ValidationConstant;
Expand All @@ -27,7 +26,7 @@
* @date 2020/2/27 19:02
*/
@Data
@State(name = "DocViewSettingsComponent", storages = {@Storage(Constant.DOC_VIEW + "/settings.xml")})
@State(name = "DocViewSettingsComponent", storages = {@Storage("DocViewSettings.xml")})
public class Settings implements PersistentStateComponent<Settings> {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.liuzhihang.doc.view.constant.Constant;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -18,7 +17,7 @@
* @date 2020/11/22 13:51
*/
@Data
@State(name = "DocViewShowDocSettingsComment", storages = {@Storage(Constant.DOC_VIEW + "/ShowDocSettings.xml")})
@State(name = "DocViewShowDocSettingsComment", storages = {@Storage("DocViewShowDocSettings.xml")})
public class ShowDocSettings implements PersistentStateComponent<ShowDocSettings> {

private String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.liuzhihang.doc.view.DocViewBundle;
import com.liuzhihang.doc.view.constant.Constant;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -17,7 +16,7 @@
* @author liuzhihang
* @date 2020/11/22 13:51
*/
@State(name = "TemplateSettingsComponent", storages = {@Storage(Constant.DOC_VIEW + "/TemplateSettings.xml")})
@State(name = "TemplateSettingsComponent", storages = {@Storage("DocViewTemplateSettings.xml")})
public class TemplateSettings implements PersistentStateComponent<TemplateSettings> {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.liuzhihang.doc.view.constant.Constant;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -18,7 +17,7 @@
* @date 2020/11/22 13:51
*/
@Data
@State(name = "DocViewYApiSettingsComment", storages = {@Storage(Constant.DOC_VIEW + "/YApiSettings.xml")})
@State(name = "DocViewYApiSettingsComment", storages = {@Storage("DocViewYApiSettings.xml")})
public class YApiSettings implements PersistentStateComponent<YApiSettings> {

private String url;
Expand Down
42 changes: 7 additions & 35 deletions src/main/java/com/liuzhihang/doc/view/ui/DocEditorForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -384,8 +386,10 @@ public void actionPerformed(@NotNull AnActionEvent e) {
responseTreeTable.getCellEditor().stopCellEditing();
}
generateMethodComment();
generateComment((ParamTreeTableModel) requestTreeTable.getTreeTableModel());
generateComment((ParamTreeTableModel) requestTreeTable.getTreeTableModel());

DocViewUtils.writeComment(project, ((ParamTreeTableModel) requestTreeTable.getTreeTableModel()).getModifiedMap());
DocViewUtils.writeComment(project, ((ParamTreeTableModel) responseTreeTable.getTreeTableModel()).getModifiedMap());

popup.cancel();
}
});
Expand Down Expand Up @@ -448,36 +452,4 @@ private void generateMethodComment() {

}

/**
* 变动的字段生成注释
*
* @param paramTreeTableModel
*/
private void generateComment(ParamTreeTableModel paramTreeTableModel) {

Map<PsiElement, DocViewParamData> modifyBodyMap = paramTreeTableModel.getModifiedMap();

for (PsiElement element : modifyBodyMap.keySet()) {
DocViewParamData data = modifyBodyMap.get(element);
String comment;

// 原来有注解, 则不添加注释
if (!DocViewUtils.isRequired((PsiField) element) && data.getRequired()) {
comment = "/** "
+ data.getDesc() + "\n"
+ "* @" + Settings.getInstance(project).getRequired()
+ " */";
} else {
comment = "/** "
+ data.getDesc()
+ " */";
}


PsiElementFactory factory = PsiElementFactory.getInstance(project);
PsiDocComment psiDocComment = factory.createDocCommentFromText(comment);
writerService.write(project, element, psiDocComment);
}
}

}
42 changes: 3 additions & 39 deletions src/main/java/com/liuzhihang/doc/view/ui/ParamDocEditorForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.ui.WindowMoveListener;
import com.intellij.util.ui.JBUI;
import com.liuzhihang.doc.view.DocViewBundle;
import com.liuzhihang.doc.view.config.Settings;
import com.liuzhihang.doc.view.config.SettingsConfigurable;
import com.liuzhihang.doc.view.dto.Body;
import com.liuzhihang.doc.view.dto.DocViewData;
import com.liuzhihang.doc.view.dto.DocViewParamData;
import com.liuzhihang.doc.view.notification.DocViewNotification;
import com.liuzhihang.doc.view.service.impl.WriterService;
import com.liuzhihang.doc.view.ui.treetable.ParamTreeTableModel;
import com.liuzhihang.doc.view.ui.treetable.ParamTreeTableUtils;
import com.liuzhihang.doc.view.utils.DocViewUtils;
Expand Down Expand Up @@ -67,8 +64,6 @@ public class ParamDocEditorForm {
public static final String DOC_VIEW_POPUP = "com.intellij.docview.param.editor.popup";
private static final AtomicBoolean myIsPinned = new AtomicBoolean(true);

private final WriterService writerService = ServiceManager.getService(WriterService.class);

public ParamDocEditorForm(@NotNull Project project, @NotNull PsiFile psiFile,
@NotNull Editor editor, @NotNull PsiClass psiClass) {

Expand Down Expand Up @@ -211,7 +206,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {

generateComment();
DocViewUtils.writeComment(project, treeTableModel.getModifiedMap());
popup.cancel();
}
});
Expand All @@ -230,37 +225,6 @@ public void actionPerformed(@NotNull AnActionEvent e) {

}

/**
* 变动的字段生成注释
*/
private void generateComment() {


Map<PsiElement, DocViewParamData> modifyBodyMap = treeTableModel.getModifiedMap();

for (PsiElement element : modifyBodyMap.keySet()) {
DocViewParamData data = modifyBodyMap.get(element);
String comment;

// 不修改原有注解
if (!DocViewUtils.isRequired((PsiField) element) && data.getRequired()) {
comment = "/** "
+ data.getDesc() + "\n"
+ "* @" + Settings.getInstance(project).getRequired()
+ " */";
} else {
comment = "/** "
+ data.getDesc()
+ " */";
}


PsiElementFactory factory = PsiElementFactory.getInstance(project);
PsiDocComment psiDocComment = factory.createDocCommentFromText(comment);
writerService.write(project, element, psiDocComment);
}
}

private void initTailLeftToolbar() {

}
Expand Down
107 changes: 107 additions & 0 deletions src/main/java/com/liuzhihang/doc/view/utils/DocViewUtils.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.liuzhihang.doc.view.utils;

import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.util.InheritanceUtil;
import com.liuzhihang.doc.view.config.Settings;
import com.liuzhihang.doc.view.constant.SwaggerConstant;
import com.liuzhihang.doc.view.dto.DocViewParamData;
import com.liuzhihang.doc.view.service.impl.WriterService;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -294,13 +300,32 @@ public static boolean isRequired(@NotNull PsiParameter psiParameter) {

/**
* 获取字段的描述
* <p>
* 优先从 swagger 中获取注释
*
* @param psiField
* @return
*/
@NotNull
public static String fieldDesc(@NotNull PsiField psiField) {

// swagger v3 @Schema
PsiAnnotation schemaAnnotation = psiField.getAnnotation(SwaggerConstant.SCHEMA);
if (schemaAnnotation != null) {
PsiAnnotationMemberValue value = schemaAnnotation.findAttributeValue("description");
if (value != null && StringUtils.isNotBlank(value.getText())) {
return value.getText().replace("\"", "");
}
}
// swagger @ApiModelProperty
PsiAnnotation apiModelPropertyAnnotation = psiField.getAnnotation(SwaggerConstant.API_MODEL_PROPERTY);
if (apiModelPropertyAnnotation != null) {
PsiAnnotationMemberValue value = apiModelPropertyAnnotation.findAttributeValue("value");
if (value != null && StringUtils.isNotBlank(value.getText())) {
return value.getText().replace("\"", "");
}
}

PsiDocComment docComment = psiField.getDocComment();

if (docComment != null) {
Expand All @@ -310,4 +335,86 @@ public static String fieldDesc(@NotNull PsiField psiField) {
}
return "";
}

/**
* 变动的字段生成注释
*/
public static void writeComment(Project project, @NotNull Map<PsiElement, DocViewParamData> modifyBodyMap) {

for (PsiElement element : modifyBodyMap.keySet()) {
DocViewParamData data = modifyBodyMap.get(element);
String comment;

PsiField psiField = (PsiField) element;

// swagger v3 @Schema 直接修改属性
PsiAnnotation schemaAnnotation = psiField.getAnnotation(SwaggerConstant.SCHEMA);
if (schemaAnnotation != null) {
String annotationText = "";

if (StringUtils.isNotBlank(data.getDesc()) && data.getRequired()) {
annotationText = "@Schema(description = \"" + data.getDesc() + "\", required = true)";
} else if (StringUtils.isNotBlank(data.getDesc()) && !data.getRequired()) {
annotationText = "@Schema(description = \"" + data.getDesc() + "\")";
} else if (StringUtils.isBlank(data.getDesc()) && data.getRequired()) {
annotationText = "@Schema(required = true)";
}

if (StringUtils.isNotBlank(annotationText)) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
PsiAnnotation newAnnotation = elementFactory.createAnnotationFromText(annotationText, psiField);

WriteCommandAction.runWriteCommandAction(project, () -> {
schemaAnnotation.replace(newAnnotation);
});

}
return;
}
// swagger @ApiModelProperty 直接修改属性
PsiAnnotation apiModelPropertyAnnotation = psiField.getAnnotation(SwaggerConstant.API_MODEL_PROPERTY);
if (apiModelPropertyAnnotation != null) {

String annotationText = "";

if (StringUtils.isNotBlank(data.getDesc()) && data.getRequired()) {
annotationText = "@ApiModelProperty(value = \"" + data.getDesc() + "\", required = true)";
} else if (StringUtils.isNotBlank(data.getDesc()) && !data.getRequired()) {
annotationText = "@ApiModelProperty(\"" + data.getDesc() + "\")";
} else if (StringUtils.isBlank(data.getDesc()) && data.getRequired()) {
annotationText = "@ApiModelProperty(required = true)";
}

if (StringUtils.isNotBlank(annotationText)) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
PsiAnnotation newAnnotation = elementFactory.createAnnotationFromText(annotationText, psiField);
// 调用生成逻辑
WriteCommandAction.runWriteCommandAction(project, () -> {
apiModelPropertyAnnotation.replace(newAnnotation);
});

}
return;
}

// 不修改原有注解
if (!DocViewUtils.isRequired(psiField) && data.getRequired()) {
comment = "/** "
+ data.getDesc() + "\n"
+ "* @" + Settings.getInstance(project).getRequired()
+ " */";
} else {
comment = "/** "
+ data.getDesc()
+ " */";
}


PsiElementFactory factory = PsiElementFactory.getInstance(project);
PsiDocComment psiDocComment = factory.createDocCommentFromText(comment);
ServiceManager.getService(WriterService.class).write(project, element, psiDocComment);
}
}


}

0 comments on commit c090567

Please sign in to comment.