Skip to content

Commit

Permalink
支持导出功能
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhihang committed Nov 15, 2020
1 parent e52719d commit 9371c9d
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 164 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Doc View
- 支持在 Controller 内右键唤出菜单, 显示 Controller 内的接口文档。
- 支持在 Controller 的方法内右键唤出菜单, 显示该方法内的接口文档。
- 支持复制为 Markdown 到剪贴板。
- 支持导出 Markdown 文件。
- 其他一些小功能点。

**注:因 API 问题,新插件仅支持 2020.1 和 2020.2 版本 。**
Expand All @@ -36,9 +37,10 @@ Doc View
更新
----

## [v1.0.1](https://github.com/liuzhihang/doc-view/releases/tag/v1.0.1) (2020-11-03)
## [v1.0.2](https://github.com/liuzhihang/doc-view/releases/tag/v1.0.2) (2020-11-15)

- 修复请求路径拼装错误
- 新增配置页面(依旧不支持配置)
- 新增导出单个为 Markdown 格式文件


[查看更多历史更新记录](./doc/ChangeNotes.md)
Expand Down
12 changes: 12 additions & 0 deletions doc/ChangeNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# ChangeNotes

## [v1.0.2](https://github.com/liuzhihang/doc-view/releases/tag/v1.0.2) (2020-11-15)

- 新增配置页面(依旧不支持配置)
- 新增导出单个为 Markdown 格式文件


## [v1.0.1](https://github.com/liuzhihang/doc-view/releases/tag/v1.0.1) (2020-11-03)

- 去除支持 2020.3
- 发布到插件仓库
- 修复请求路径错误问题


## [v1.0.0](https://github.com/liuzhihang/doc-view/releases/tag/v1.0.0) (2020-11-01)

Expand Down
9 changes: 4 additions & 5 deletions parts/changeNotes.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@

<h4>English introduction</h4>
<ul>
<li>1.0.1
<li>1.0.2
<ol>
<li>Repair request path assembly error</li>

<li>Support exporting as Markdown file.</li>
</ol>
</li>
<li><a href="https://github.com/liuzhihang/doc-view/blob/master/doc/ChangeNotes.md">More records</a></li>
</ul>
<h4>中文介绍</h4>
<ul>
<li>1.0.1
<li>1.0.2
<ol>
<li>修复请求路径拼装错误</li>
<li>支持导出为 Markdown 文件。</li>
</ol>
</li>
<li><a href="https://github.com/liuzhihang/doc-view/blob/master/doc/ChangeNotes.md">更多记录</a></li>
Expand Down
5 changes: 2 additions & 3 deletions parts/pluginDescription.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ <h1>An IntelliJ IDEA plug-in that can directly generate Markdown documents throu
the method.
</li>
<li>Support copying as Markdown to clipboard.</li>
<li>Some other small features.</li>

<li>Support exporting as Markdown file.</li>
</ul>
<br/>

Expand All @@ -46,7 +45,7 @@ <h1>An IntelliJ IDEA plug-in that can directly generate Markdown documents throu
<li>支持在 Controller 内右键唤出菜单, 显示 Controller 内的接口文档。</li>
<li>支持在 Controller 的方法内右键唤出菜单, 显示该方法内的接口文档。</li>
<li>支持复制为 Markdown 到剪贴板。</li>
<li>其他一些小功能点</li>
<li>支持导出为 Markdown 文件</li>
</ul>
<br/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.liuzhihang.doc.view.component.Settings;
import com.liuzhihang.doc.view.dto.DocView;
import com.liuzhihang.doc.view.service.DocViewService;
import com.liuzhihang.doc.view.ui.PreviewForm;
import com.liuzhihang.doc.view.service.SpringDocViewService;
import com.liuzhihang.doc.view.utils.CustomPsiUtils;
import com.liuzhihang.doc.view.utils.NotificationUtils;

import java.util.Map;

/**
* @author liuzhihang
* @date 2020/2/26 21:57
Expand Down Expand Up @@ -58,26 +52,21 @@ public void actionPerformed(AnActionEvent e) {

Settings settings = project.getService(Settings.class);

// 不是接口 且不包含指定的注解
if (!targetClass.isInterface() && !AnnotationUtil.isAnnotated(targetClass, settings.getContainClassAnnotationName(), 0)) {
NotificationUtils.errorNotify("The current class is not an interface and has no Spring annotations", project);

if (targetClass.isInterface()) {
NotificationUtils.errorNotify("The current version does not support the interface", project);
return;
}

// 当前方法
PsiMethod targetMethod = CustomPsiUtils.getTargetMethod(editor, psiFile);
// Spring
if (AnnotationUtil.isAnnotated(targetClass, settings.getContainClassAnnotationName(), 0)) {

// 生成文档列表
Map<String, DocView> docMap = DocViewService.getInstance().buildDocView(settings, targetClass, targetMethod);
SpringDocViewService.getInstance().doPreview(project, psiFile, editor, targetClass);

if (docMap == null || docMap.size() == 0) {
NotificationUtils.errorNotify("There is no public modification method in the class", project);
return;
}


DialogWrapper dialog = new PreviewForm(project, psiFile, editor, targetClass, docMap);
dialog.show();
NotificationUtils.errorNotify("Document generation is not supported here", project);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.psi.*;
import com.liuzhihang.doc.view.component.Settings;
import com.liuzhihang.doc.view.dto.Body;
import com.liuzhihang.doc.view.dto.DocView;
import com.liuzhihang.doc.view.dto.Header;
import com.liuzhihang.doc.view.dto.Param;
import com.liuzhihang.doc.view.ui.PreviewForm;
import com.liuzhihang.doc.view.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
Expand All @@ -21,35 +25,45 @@
* @author liuzhihang
* @date 2020/3/3 13:32
*/
public class DocViewService {
public class SpringDocViewService {


public static DocViewService getInstance() {
return ServiceManager.getService(DocViewService.class);
public static SpringDocViewService getInstance() {
return ServiceManager.getService(SpringDocViewService.class);
}

public Map<String, DocView> buildDocView(@NotNull Settings settings, @NotNull PsiClass psiClass, PsiMethod psiMethod) {

public void doPreview(@NotNull Project project, PsiFile psiFile, Editor editor, PsiClass targetClass) {

if (psiClass.isInterface()) {
// 接口按照dubbo接口生成
Settings settings = project.getService(Settings.class);

} else {
// 当前方法
PsiMethod targetMethod = CustomPsiUtils.getTargetMethod(editor, psiFile);

if (psiMethod != null) {
// 单个方法
DocView docView = buildClassMethodDoc(settings, psiClass, psiMethod);
Map<String, DocView> docMap = new HashMap<>(2);
if (docView != null) {
docMap.put(docView.getName(), docView);
}
return docMap;
} else {
// 单个类 的所有方法
return buildClassDoc(settings, psiClass);
Map<String, DocView> docMap = new HashMap<>();

if (targetMethod != null) {

if (checkMethod(settings, targetMethod)) {
NotificationUtils.errorNotify("The method does not meet the conditions", project);
return;
}

DocView docView = buildClassMethodDoc(settings, targetClass, targetMethod);
docMap.put(docView.getName(), docView);

} else {
// 生成文档列表
docMap = buildClassDoc(settings, targetClass);
if (docMap.size() == 0) {
NotificationUtils.errorNotify("There is no public modification method in the class", project);
return;
}
}
return null;

DialogWrapper dialog = new PreviewForm(project, psiFile, editor, targetClass, docMap);
dialog.show();

}

@NotNull
Expand All @@ -58,29 +72,21 @@ private Map<String, DocView> buildClassDoc(Settings settings, @NotNull PsiClass
Map<String, DocView> docMap = new HashMap<>(32);

for (PsiMethod method : psiClass.getMethods()) {
DocView docView = buildClassMethodDoc(settings, psiClass, method);
if (docView != null) {
docMap.put(docView.getName(), docView);

if (checkMethod(settings, method)) {
continue;
}

DocView docView = buildClassMethodDoc(settings, psiClass, method);
docMap.put(docView.getName(), docView);
}

return docMap;
}

private DocView buildClassMethodDoc(Settings settings, PsiClass psiClass, @NotNull PsiMethod psiMethod) {
@NotNull
public DocView buildClassMethodDoc(Settings settings, PsiClass psiClass, @NotNull PsiMethod psiMethod) {

if (!CustomPsiModifierUtils.hasModifierProperty(psiMethod, PsiModifier.PUBLIC)) {
return null;
}
if (CustomPsiModifierUtils.hasModifierProperty(psiMethod, PsiModifier.STATIC)) {
return null;
}
if (psiMethod.isConstructor()) {
return null;
}
if (!AnnotationUtil.isAnnotated(psiMethod, settings.getContainMethodAnnotationName(), 0)) {
return null;
}
// 请求路径
String path = CustomPsiAnnotationUtils.getPath(psiClass, psiMethod);

Expand Down Expand Up @@ -157,4 +163,27 @@ private DocView buildClassMethodDoc(Settings settings, PsiClass psiClass, @NotNu
return docView;
}

/**
* 检查方法是否满足条件
*
* @param settings
* @param psiMethod
* @return true 不满足条件
*/
private boolean checkMethod(Settings settings, @NotNull PsiMethod psiMethod) {
if (!CustomPsiModifierUtils.hasModifierProperty(psiMethod, PsiModifier.PUBLIC)) {
return true;
}
if (CustomPsiModifierUtils.hasModifierProperty(psiMethod, PsiModifier.STATIC)) {
return true;
}
if (psiMethod.isConstructor()) {
return true;
}
if (!AnnotationUtil.isAnnotated(psiMethod, settings.getContainMethodAnnotationName(), 0)) {
return true;
}
return false;
}

}
Loading

0 comments on commit 9371c9d

Please sign in to comment.