Skip to content

Commit

Permalink
Merge pull request #53 from liuzhihang/develop
Browse files Browse the repository at this point in the history
缓存路径调整
  • Loading branch information
liuzhihang authored Jun 16, 2022
2 parents 393c928 + 71faae4 commit 70f2530
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 101 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.liuzhihang'
version '1.2.8'
version '1.2.9'

JavaVersion.VERSION_11

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

<h4>English introduction</h4>
<ul>
<li>1.2.8
<li>1.2.9
<ol>
<li>fix: Error reporting problem</li>
<li>Tuning file cache in x "Scratches and Consoles"</li>
<li>Support double-click to open the document</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.2.8
<li>1.2.9
<ol>
<li>fix: 报错问题</li>
<li>调整文件缓存在 "Scratches and Consoles" 中</li>
<li>支持双击打开文档</li>
</ol>
</li>
<li><a href="https://github.com/liuzhihang/doc-view/blob/master/doc/ChangeNotes.md">更多记录</a></li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.liuzhihang.doc.view.utils.CustomFileUtils;
import com.liuzhihang.doc.view.utils.StorageUtils;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;

/**
* @author liuzhihang
* @date 2021/10/23 19:55
Expand All @@ -21,9 +18,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
if (project == null) {
return;
}

Path configDir = StorageUtils.getConfigDir(project);
CustomFileUtils.delete(configDir.toFile(), project);
CustomFileUtils.delete(project, "Doc View");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import com.liuzhihang.doc.view.utils.CustomFileUtils;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.nio.file.Path;

/**
* @author liuzhihang
* @date 2021/10/23 19:55
Expand All @@ -35,13 +32,10 @@ public void actionPerformed(@NotNull AnActionEvent e) {

if (selectedNode instanceof DocViewNode) {
DocViewNode docViewNode = (DocViewNode) selectedNode;
Path cachePath = docViewNode.cachePath(project);
File file = new File(cachePath.toUri());
CustomFileUtils.delete(file, project);
}
CustomFileUtils.delete(project, docViewNode.cachePath(project));

}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import com.liuzhihang.doc.view.utils.DocViewUtils;

import java.awt.event.InputEvent;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -26,7 +24,7 @@
public class ClassNode extends DocViewNode {

private final List<MethodNode> methodNodes = new ArrayList<>();
private final PsiClass psiClass;
private final PsiClass psiClass;

protected ClassNode(SimpleNode aParent, PsiClass psiClass) {
super(aParent);
Expand All @@ -51,11 +49,11 @@ public void updateNode(Project project) {
}

@Override
public Path cachePath(Project project) {
public String cachePath(Project project) {

ModuleNode moduleNode = (ModuleNode) getParent();

return Paths.get(moduleNode.cachePath(project).toString(), DocViewUtils.getTitle(psiClass));
return moduleNode.cachePath(project) + "/" + DocViewUtils.getTitle(psiClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.intellij.ui.treeStructure.SimpleNode;
import com.liuzhihang.doc.view.dto.DocView;

import java.nio.file.Path;
import java.util.List;

/**
Expand All @@ -31,6 +30,6 @@ public String getName() {

public abstract void updateNode(Project project);

public abstract Path cachePath(Project project);
public abstract String cachePath(Project project);

}
17 changes: 10 additions & 7 deletions src/main/java/com/liuzhihang/doc/view/ui/window/MethodNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
import com.intellij.ui.treeStructure.SimpleTree;
import com.liuzhihang.doc.view.dto.DocView;
import com.liuzhihang.doc.view.service.DocViewService;
import com.liuzhihang.doc.view.utils.CustomFileUtils;
import com.liuzhihang.doc.view.utils.DocViewUtils;
import com.liuzhihang.doc.view.utils.DubboPsiUtils;
import com.liuzhihang.doc.view.utils.SpringPsiUtils;
import icons.DocViewIcons;

import javax.swing.*;
import java.awt.event.InputEvent;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* 目录树上的一个节点
Expand Down Expand Up @@ -87,11 +90,11 @@ public void updateNode(Project project) {
}

@Override
public Path cachePath(Project project) {
public String cachePath(Project project) {

ClassNode classNode = (ClassNode) getParent();

return Paths.get(classNode.cachePath(project).toString(), DocViewUtils.getName(psiMethod) + ".md");
return classNode.cachePath(project) + "/" + DocViewUtils.getName(psiMethod) + ".md";

}

Expand All @@ -101,14 +104,14 @@ public void handleSelection(SimpleTree tree) {
}

/**
* 被双击或者 Enter 时
* 被双击或者 Enter 时, 双击打开文档
*
* @param tree
* @param inputEvent
*/
@Override
public void handleDoubleClickOrEnter(SimpleTree tree, InputEvent inputEvent) {
super.handleDoubleClickOrEnter(tree, inputEvent);
CustomFileUtils.open(this, psiClass.getProject());
}

public PsiMethod getPsiMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import com.liuzhihang.doc.view.utils.DubboPsiUtils;
import com.liuzhihang.doc.view.utils.FeignPsiUtil;
import com.liuzhihang.doc.view.utils.SpringPsiUtils;
import com.liuzhihang.doc.view.utils.StorageUtils;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
Expand Down Expand Up @@ -70,9 +67,11 @@ public void updateNode(Project project) {
}

@Override
public Path cachePath(Project project) {
public String cachePath(Project project) {

return Paths.get(StorageUtils.getConfigDir(project).toString(), module.getName());
RootNode rootNode = (RootNode) getParent();

return rootNode.cachePath(project) + "/" + module.getName();
}

@Override
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/liuzhihang/doc/view/ui/window/RootNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.ui.treeStructure.SimpleNode;
import com.liuzhihang.doc.view.dto.DocView;
import com.liuzhihang.doc.view.utils.StorageUtils;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -46,8 +44,8 @@ public void updateNode(Project project) {
}

@Override
public Path cachePath(Project project) {
return StorageUtils.getConfigDir(project);
public String cachePath(Project project) {
return "Doc View";
}


Expand Down
93 changes: 34 additions & 59 deletions src/main/java/com/liuzhihang/doc/view/utils/CustomFileUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.liuzhihang.doc.view.utils;

import com.intellij.ide.extensionResources.ExtensionsRootType;
import com.intellij.ide.scratch.ScratchFileService.Option;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.UndoConfirmationPolicy;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.liuzhihang.doc.view.DocViewBundle;
import com.liuzhihang.doc.view.dto.DocView;
Expand All @@ -15,52 +18,31 @@
import com.liuzhihang.doc.view.service.DocViewService;
import com.liuzhihang.doc.view.ui.window.MethodNode;

import java.io.File;
import java.io.IOException;

/**
* @author liuzhihang
* @date 2021/12/16
*/
public class CustomFileUtils {

public static void delete(File file, Project project) {
public static void delete(Project project, String path) {

if (file.isFile()) {
closeFileIfOpen(file, project);
}

if (file.isDirectory()) {

File[] files = file.listFiles();
try {

if (files == null) {
return;
}
VirtualFile file = ExtensionsRootType.getInstance().findFile(project, path, Option.create_if_missing);

for (File listFile : files) {
delete(listFile, project);
if (FileEditorManager.getInstance(project).isFileOpen(file)) {
FileEditorManager.getInstance(project).closeFile(file);
}
}

FileUtil.delete(file);
}


private static void closeFileIfOpen(File file, Project project) {

VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);

if (vf == null) {
return;
}
WriteAction.runAndWait(() -> file.delete(project));

if (FileEditorManager.getInstance(project).isFileOpen(vf)) {
FileEditorManager.getInstance(project).closeFile(vf);
} catch (IOException e) {
DocViewNotification.notifyError(project, DocViewBundle.message("notify.extensions.file.delete.file", path));
}

}


public static void open(MethodNode node, Project project) {

if (node == null) {
Expand All @@ -73,36 +55,29 @@ public static void open(MethodNode node, Project project) {
return;
}

File file = new File(node.cachePath(project).toUri());

if (!file.exists()) {
String markdownText = ApplicationManager.getApplication().runReadAction((Computable<String>) () -> {
DocView docView = service.buildClassMethodDoc(project, node.getPsiClass(), node.getPsiMethod());
return DocViewData.markdownText(project, docView);
});
String markdownText = ApplicationManager.getApplication().runReadAction((Computable<String>) () -> {
DocView docView = service.buildClassMethodDoc(project, node.getPsiClass(), node.getPsiMethod());
return DocViewData.markdownText(project, docView);
});

try {
FileUtil.writeToFile(file, markdownText);
} catch (Exception ex) {
DocViewNotification.notifyError(project, DocViewBundle.message("notify.spring.error.method"));
try {
VirtualFile virtualFile = WriteCommandAction.writeCommandAction(project)
.withName(DocViewBundle.message("notify.extensions.file.creating"))
.withGlobalUndo().shouldRecordActionForActiveDocument(false)
.withUndoConfirmationPolicy(UndoConfirmationPolicy.REQUEST_CONFIRMATION).compute(() -> {
ExtensionsRootType fileService = ExtensionsRootType.getInstance();
VirtualFile scratchFile = fileService.findFile(project, node.cachePath(project), Option.create_if_missing);
VfsUtil.saveText(scratchFile, markdownText);
return scratchFile;
});

if (virtualFile != null) {
FileEditorManager.getInstance(project).openFile(virtualFile, true);
}
} catch (IOException e) {
DocViewNotification.notifyError(project, DocViewBundle.message("notify.extensions.file.create.file", node.getName()));
}

openMarkdownDoc(file, project);
}


private static void openMarkdownDoc(File file, Project project) {
ApplicationManager.getApplication().invokeAndWait(() -> {
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);

if (vf == null) {
return;
}

OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vf);
FileEditorManager.getInstance(project).openTextEditor(descriptor, false);
});
}

}
3 changes: 3 additions & 0 deletions src/main/resources/messages/DocViewBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ notify.not.support.jcef=\u5F53\u524D IDEA \u7248\u672C\u4E0D\u652F\u6301 JCEF, \
notify.window.export.empty=\u6CA1\u6709\u8981\u5BFC\u51FA\u7684\u6587\u6863
notify.window.upload.empty=\u6CA1\u6709\u8981\u4E0A\u4F20\u7684\u6587\u6863
notify.window.select.filter=\u8BF7\u5148\u9009\u53D6\u8BFB\u53D6\u8303\u56F4
notify.extensions.file.creating=Create New Extension File
notify.extensions.file.create.file=Could not create file ''{0}''
notify.extensions.file.delete.file=Could not delete file ''{0}''
#
# support \u754C\u9762
support.title=\u60A8\u53EF\u4EE5\u901A\u8FC7\u4EE5\u4E0B\u65B9\u5F0F\u5173\u6CE8\u4E86\u89E3\u672C\u9879\u76EE, \u63D0\u4EA4\u95EE\u9898\u5EFA\u8BAE:
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/liuzhihang/doc/view/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Ant Group
* Copyright (c) 2004-2022 All Rights Reserved.
*/
package com.liuzhihang.doc.view;

import com.intellij.openapi.util.text.StringUtil;

/**
* @author zijun.lzh
* @version MainTest.java, v 0.1 2022年06月08日 2:39 PM zijun.lzh
*/
public class MainTest {

public static void main(String[] args) {

StringUtil.

}
}

0 comments on commit 70f2530

Please sign in to comment.