1.13 Add Copy With Line Comment & Add Line Comment | 增加 带行末注释复制 和 添加行末注释

This commit is contained in:
林万程
2022-04-10 19:42:26 +08:00
parent 595d3730d4
commit baa50ba361
29 changed files with 385 additions and 185 deletions

View File

@@ -54,6 +54,7 @@ https://plugins.jetbrains.com/plugin/18553-show-comment
<h2>English Change Notes:</h2>
<ul>
<li>1.13 Add Copy With Line Comment & Add Line Comment
<li>1.12 ★ External Comment for COBOL etc
<li>1.11 Add json key jump to field
<li>1.10 Add project-view-tree-comment for package from parent or other project
@@ -70,6 +71,7 @@ https://plugins.jetbrains.com/plugin/18553-show-comment
<h2>中文更新说明:</h2>
<ul>
<li>1.12 增加 带行末注释复制 和 添加行末注释
<li>1.12 ★ 外部注释用于 COBOL 等
<li>1.11 增加 json 跳转到字段
<li>1.10 增加 在父包和其他项目的包中获取 项目导航栏注释

View File

@@ -4,7 +4,7 @@ plugins {
}
group 'io.github.linwancen'
version '1.12.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
version '1.13.0.' + (new Date().format('yyyy.MM.dd_HH.mm'))
apply plugin: 'java'
@@ -39,6 +39,7 @@ patchPluginXml {
changeNotes = """
<h2>English Change Notes:</h2>
<ul>
<li>1.13 Add Copy With Line Comment & Add Line Comment
<li>1.12 ★ External Comment for COBOL etc
<li>1.11 Add json key jump to field
<li>1.10 Add project-view-tree-comment for package from parent or other project
@@ -55,6 +56,7 @@ patchPluginXml {
<h2>中文更新说明:</h2>
<ul>
<li>1.12 增加 带行末注释复制 和 添加行末注释
<li>1.12 ★ 外部注释用于 COBOL 等
<li>1.11 增加 json 跳转到字段
<li>1.10 增加 在父包和其他项目的包中获取 项目导航栏注释

View File

@@ -9,8 +9,7 @@ import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ProcessingContext;
import io.github.linwancen.plugin.show.doc.PsiClassUtils;
import io.github.linwancen.plugin.show.json.JsonRef;
import io.github.linwancen.plugin.show.json.JsonUtils;
import io.github.linwancen.plugin.show.jump.JsonRef;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
@@ -39,7 +38,7 @@ public class JsonJump extends PsiReferenceContributor {
List<PsiField> psiFields = new ArrayList<>();
List<PsiField> tips = new ArrayList<>();
PsiClass[] psiClasses = PsiClassUtils.encClass(virtualFile, project);
List<String> jsonPath = JsonUtils.jsonPath(jsonProp);
List<String> jsonPath = jsonPath(jsonProp);
put(project, psiFields, tips, psiClasses, jsonPath, jsonPath.size() - 1);
List<PsiReference> list = new ArrayList<>();
@@ -51,6 +50,15 @@ public class JsonJump extends PsiReferenceContributor {
});
}
@NotNull
private static List<String> jsonPath(JsonProperty jsonProp) {
ArrayList<String> jsonPath = new ArrayList<>();
do {
jsonPath.add(jsonProp.getName());
} while ((jsonProp = PsiTreeUtil.getParentOfType(jsonProp, JsonProperty.class)) != null);
return jsonPath;
}
private static void put(Project project, List<PsiField> psiFields, List<PsiField> tips,
PsiClass[] psiClasses, List<String> jsonPath, int level) {
String name = jsonPath.get(level);

View File

@@ -5,16 +5,14 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorLinePainter;
import com.intellij.openapi.editor.LineExtensionInfo;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiManager;
import com.intellij.psi.javadoc.PsiDocComment;
import io.github.linwancen.plugin.show.doc.DocTextUtils;
import io.github.linwancen.plugin.show.line.LineDocLeftToRightUtils;
import io.github.linwancen.plugin.show.line.LineDocRightToLeftUtils;
import io.github.linwancen.plugin.show.ext.LineExtUtils;
import io.github.linwancen.plugin.show.line.FileViewToDocStrUtils;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -51,13 +49,18 @@ public class LineEnd extends EditorLinePainter {
private static @Nullable String doc(@NotNull Project project,
@NotNull VirtualFile file, int lineNumber) {
FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(file);
if (viewProvider == null) {
return null;
}
Document document = viewProvider.getDocument();
Document document = FileDocumentManager.getInstance().getDocument(file);
if (document == null) {
return null;
}
return doc(document, lineNumber, project, file, viewProvider);
}
@Nullable
private static String doc(@NotNull Document document, int lineNumber,
@Nullable Project project,
@Nullable VirtualFile file,
@Nullable FileViewProvider viewProvider) {
// lineNumber start 0, as 1 <= 1 should return
if (document.getLineCount() <= lineNumber) {
return null;
@@ -67,13 +70,7 @@ public class LineEnd extends EditorLinePainter {
if (startOffset == endOffset) {
return null;
}
String ext = LineExtUtils.extDoc(project, file, document, startOffset, endOffset);
if (ext != null) {
return ext;
}
PsiDocComment docComment = AppSettingsState.getInstance().findElementRightToLeft
? LineDocRightToLeftUtils.rightDoc(viewProvider, startOffset, endOffset)
: LineDocLeftToRightUtils.leftDoc(viewProvider, document, startOffset, endOffset);
return DocTextUtils.text(docComment);
String text = document.getText(new TextRange(startOffset, endOffset));
return FileViewToDocStrUtils.doc(document, project, file, viewProvider, startOffset, endOffset, text);
}
}

View File

@@ -0,0 +1,73 @@
package io.github.linwancen.plugin.show;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileVisitor;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiManager;
import io.github.linwancen.plugin.show.line.FileViewToDocStrUtils;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
/**
* on ProjectViewPopupMenu
*/
public class LineEndAdd extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
Project project = event.getProject();
if (project == null) {
return;
}
VirtualFile[] files = event.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
if (files == null) {
return;
}
ListPopup confirmation = JBPopupFactory.getInstance().createConfirmation(
"Add Line Comment?", "Add and replace files!", "Don't add.",
() -> addDocAll(project, files), 2);
confirmation.showInFocusCenter();
}
private void addDocAll(@NotNull Project project, @NotNull VirtualFile[] files) {
AppSettingsState settings = AppSettingsState.getInstance();
for (VirtualFile file : files) {
VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor<Void>() {
@Override
public boolean visitFile(@NotNull VirtualFile file) {
if (!file.isDirectory()) {
addDoc(project, file, settings);
}
return true;
}
});
}
}
private void addDoc(@NotNull Project project, @NotNull VirtualFile file, @NotNull AppSettingsState settings) {
FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(file);
if (viewProvider == null) {
return;
}
Document document = viewProvider.getDocument();
if (document == null) {
return;
}
int startLine = 0;
int endLine = document.getLineCount() - 1;
String textWithDoc = FileViewToDocStrUtils.textWithDoc(settings, document,
startLine, endLine, project, file, viewProvider);
WriteCommandAction.runWriteCommandAction(project, () ->
document.replaceString(0, document.getTextLength() - 1, textWithDoc)
);
}
}

View File

@@ -0,0 +1,63 @@
package io.github.linwancen.plugin.show;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiFile;
import io.github.linwancen.plugin.show.line.FileViewToDocStrUtils;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
import java.awt.datatransfer.StringSelection;
/**
* on EditorPopupMenu
*/
public class LineEndCopy extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent event) {
Project project = event.getProject();
VirtualFile file = event.getData(CommonDataKeys.VIRTUAL_FILE);
if (file == null) {
return;
}
PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE);
if (psiFile == null) {
return;
}
FileViewProvider viewProvider = psiFile.getViewProvider();
Document document = viewProvider.getDocument();
if (document == null) {
return;
}
int startLine = 0;
int endLine = document.getLineCount() - 1;
// if select
Editor editor = event.getData(CommonDataKeys.EDITOR);
if (editor != null) {
Caret primaryCaret = editor.getCaretModel().getPrimaryCaret();
int start = primaryCaret.getSelectionStart();
int end = primaryCaret.getSelectionEnd();
if (start != end) {
startLine = document.getLineNumber(start);
endLine = document.getLineNumber(end);
}
}
AppSettingsState settings = AppSettingsState.getInstance();
String textWithDoc = FileViewToDocStrUtils.textWithDoc(settings, document,
startLine, endLine, project, file, viewProvider);
StringSelection content = new StringSelection(textWithDoc);
CopyPasteManager.getInstance().setContents(content);
}
}

View File

@@ -14,9 +14,9 @@ import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
import io.github.linwancen.plugin.show.doc.DocTextUtils;
import io.github.linwancen.plugin.show.doc.DocUtils;
import io.github.linwancen.plugin.show.ext.TreeExtUtils;
import io.github.linwancen.plugin.show.doc.PsiDocToStrDoc;
import io.github.linwancen.plugin.show.doc.OwnerToPsiDocUtils;
import io.github.linwancen.plugin.show.ext.TreeExt;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.Nullable;
@@ -51,7 +51,7 @@ public class Tree implements ProjectViewNodeDecorator {
@Nullable
private String doc(ProjectViewNode<?> node, Project project) {
String extDoc = extDoc(node, project);
String extDoc = extDoc(node);
if (extDoc != null) {
return extDoc;
}
@@ -59,23 +59,23 @@ public class Tree implements ProjectViewNodeDecorator {
if (docComment == null) {
return null;
}
return DocTextUtils.text(docComment);
return PsiDocToStrDoc.text(docComment);
}
@Nullable
public static String extDoc(ProjectViewNode<?> node, Project project) {
public static String extDoc(ProjectViewNode<?> node) {
VirtualFile file = node.getVirtualFile();
if (file == null) {
return null;
}
return TreeExtUtils.extDoc(project, file);
return TreeExt.extDoc(file);
}
@Nullable
private static PsiDocComment nodeDoc(ProjectViewNode<?> node, Project project) {
if (node instanceof PsiFileNode) {
PsiFile psiFile = ((PsiFileNode) node).getValue();
return DocUtils.fileDoc(psiFile);
return OwnerToPsiDocUtils.fileDoc(psiFile);
}
if (node instanceof PsiDirectoryNode) {
PsiDirectory psiDirectory = ((PsiDirectoryNode) node).getValue();
@@ -85,18 +85,18 @@ public class Tree implements ProjectViewNodeDecorator {
if (node instanceof PsiMethodNode) {
// On Show Members
PsiMethod psiMethod = ((PsiMethodNode) node).getValue();
return DocUtils.methodDoc(psiMethod);
return OwnerToPsiDocUtils.methodDoc(psiMethod);
}
if (node instanceof PsiFieldNode) {
// On Show Members
PsiField psiField = ((PsiFieldNode) node).getValue();
return DocUtils.srcOrByteCodeDoc(psiField);
return OwnerToPsiDocUtils.srcOrByteCodeDoc(psiField);
}
if (node instanceof ClassTreeNode) {
// On Packages View, Project Files View, Show Members
PsiClass psiClass = ((ClassTreeNode) node).getValue();
return DocUtils.srcOrByteCodeDoc(psiClass);
return OwnerToPsiDocUtils.srcOrByteCodeDoc(psiClass);
}
if (node instanceof PackageElementNode) {
// On Packages View
@@ -119,7 +119,7 @@ public class Tree implements ProjectViewNodeDecorator {
@Nullable
private static PsiDocComment dirDoc(PsiDirectory child) {
while (true) {
PsiDocComment docComment = DocUtils.dirDoc(child);
PsiDocComment docComment = OwnerToPsiDocUtils.dirDoc(child);
if (docComment != null) {
return docComment;
}
@@ -137,7 +137,7 @@ public class Tree implements ProjectViewNodeDecorator {
@Nullable
private static PsiDocComment packageDoc(PsiPackage child) {
while (true) {
PsiDocComment docComment = DocUtils.packageDoc(child);
PsiDocComment docComment = OwnerToPsiDocUtils.packageDoc(child);
if (docComment != null) {
return docComment;
}

View File

@@ -5,9 +5,12 @@ import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import org.jetbrains.annotations.Nullable;
public class DocUtils {
/**
* call PackageFileToPsiDoc, PsiMethodToPsiDoc, PsiClassUtils
*/
public class OwnerToPsiDocUtils {
private DocUtils() {}
private OwnerToPsiDocUtils() {}
public static PsiDocComment srcOrByteCodeDoc(PsiDocCommentOwner psiDocCommentOwner) {
PsiElement navElement = psiDocCommentOwner.getNavigationElement();
@@ -19,7 +22,7 @@ public class DocUtils {
@Nullable
public static PsiDocComment methodDoc(PsiMethod psiMethod) {
return MethodDocUtils.methodSupperNewPropDoc(psiMethod);
return PsiMethodToPsiDoc.methodSupperNewPropDoc(psiMethod);
}
@Nullable
@@ -34,7 +37,7 @@ public class DocUtils {
PsiDirectory[] psiDirectories = psiPackage.getDirectories();
for (PsiDirectory psiDirectory : psiDirectories) {
PsiFile file = psiDirectory.findFile(PsiPackage.PACKAGE_INFO_FILE);
PsiDocComment psiDocComment = PackageDocUtils.fromPackageInfoFile(file);
PsiDocComment psiDocComment = PackageFileToPsiDoc.fromPackageInfoFile(file);
if (psiDocComment != null) {
return psiDocComment;
}
@@ -62,7 +65,7 @@ public class DocUtils {
return null;
}
if (PsiPackage.PACKAGE_INFO_FILE.equals(psiFile.getName())) {
return PackageDocUtils.fromPackageInfoFile(psiFile);
return PackageFileToPsiDoc.fromPackageInfoFile(psiFile);
}
PsiClassOwner psiClassOwner = (PsiClassOwner) psiFile;
PsiClass[] classes = psiClassOwner.getClasses();

View File

@@ -8,9 +8,9 @@ import com.intellij.psi.javadoc.PsiDocComment;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class PackageDocUtils {
class PackageFileToPsiDoc {
private PackageDocUtils() {}
private PackageFileToPsiDoc() {}
@Nullable
static PsiDocComment fromPackageInfoFile(PsiFile packageInfoFile) {

View File

@@ -40,7 +40,7 @@ public class PsiClassUtils {
}
@NotNull
public static PsiClass[] simpleNameToClass(@NotNull String className, @NotNull Project project) {
private static PsiClass[] simpleNameToClass(@NotNull String className, @NotNull Project project) {
PsiShortNamesCache namesCache = PsiShortNamesCache.getInstance(project);
return namesCache.getClassesByName(className, GlobalSearchScope.allScope(project));
}

View File

@@ -7,9 +7,9 @@ import com.intellij.psi.javadoc.PsiInlineDocTag;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.Nullable;
public class DocTextUtils {
public class PsiDocToStrDoc {
private DocTextUtils() {}
private PsiDocToStrDoc() {}
@Nullable
public static String text(@Nullable PsiDocComment psiDocComment) {

View File

@@ -7,9 +7,9 @@ import com.intellij.psi.PsiMethod;
import com.intellij.psi.javadoc.PsiDocComment;
import org.jetbrains.annotations.Nullable;
class MethodDocUtils {
class PsiMethodToPsiDoc {
private MethodDocUtils() {}
private PsiMethodToPsiDoc() {}
@Nullable
static PsiDocComment methodSupperNewPropDoc(PsiMethod psiMethod) {
@@ -49,7 +49,7 @@ class MethodDocUtils {
private static PsiDocComment supperMethodDoc(PsiMethod psiMethod) {
PsiMethod[] superMethods = psiMethod.findSuperMethods();
for (PsiMethod superMethod : superMethods) {
PsiDocComment superDoc = DocUtils.methodDoc(superMethod);
PsiDocComment superDoc = OwnerToPsiDocUtils.methodDoc(superMethod);
if (superDoc != null) {
return superDoc;
}

View File

@@ -1,6 +1,5 @@
package io.github.linwancen.plugin.show.ext;
import io.github.linwancen.plugin.show.settings.ProjectSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -8,16 +7,15 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
class DocMapUtils {
class GetFromDocMap {
private DocMapUtils() {}
private GetFromDocMap() {}
@Nullable
static String get(@NotNull Map<String, Map<String, List<String>>> docMap,
@NotNull ProjectSettingsState set, @NotNull String... words) {
static String get(@NotNull Map<String, Map<String, List<String>>> docMap, @NotNull String... words) {
List<String> keywordDoc = list(docMap, words);
if (keywordDoc.size() >= set.extDocColumn) {
return keywordDoc.get(set.extDocColumn - 1);
if (keywordDoc.size() >= 2) {
return keywordDoc.get(1);
}
return null;
}

View File

@@ -1,11 +1,7 @@
package io.github.linwancen.plugin.show.ext;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import io.github.linwancen.plugin.show.ext.conf.ConfCache;
import io.github.linwancen.plugin.show.settings.ProjectSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -14,32 +10,32 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LineExtUtils {
public class LineExt {
private LineExtUtils() {}
private LineExt() {}
public static String extDoc(@NotNull Project project, @NotNull VirtualFile file,
@NotNull Document document, int startOffset, int endOffset) {
Map<String, Map<String, List<String>>> keyMap = ConfCache.keyMap(project, file);
public static String extDoc(@Nullable Project project,
@NotNull String path, @NotNull String name, @Nullable String ext,
@NotNull String text) {
Map<String, Map<String, List<String>>> keyMap = ConfCache.keyMap(path, name, ext);
if (keyMap.isEmpty()) {
return null;
}
Pattern pattern = ConfCache.pattern(project, file, keyMap);
Pattern pattern = ConfCache.pattern(project, keyMap, path);
if (pattern == null || pattern.pattern().length() == 0) {
return null;
}
Map<String, Map<String, List<String>>> docMap = ConfCache.docMap(project, file);
Map<String, Map<String, List<String>>> docMap = ConfCache.docMap(path, name, ext);
if (docMap.isEmpty()) {
return null;
}
Map<String, Map<String, List<String>>> treeMap = ConfCache.treeMap(project, file);
String text = document.getText(new TextRange(startOffset, endOffset));
if ("cbl".equals(file.getExtension())) {
Map<String, Map<String, List<String>>> treeMap = ConfCache.treeMap(path);
if ("cbl".equals(ext)) {
text = cblNotAndOr(text);
}
String[] words = pattern.split(text);
Matcher matcher = pattern.matcher(text);
return extDoc(keyMap, matcher, docMap, words, treeMap, project);
return extDoc(keyMap, matcher, docMap, words, treeMap);
}
private static final Pattern DICT_PATTERN = Pattern.compile("([\\w-]++) ?(NOT)? ?= ?'");
@@ -59,23 +55,20 @@ public class LineExtUtils {
// put NOT first
text = matcher.replaceAll("$2 ( $1 = '");
// add key after AND/OR
return AND_OR_PATTERN.matcher(text).replaceAll("$1 "+ key + " = '");
return AND_OR_PATTERN.matcher(text).replaceAll("$1 " + key + " = '");
}
@Nullable
private static String extDoc(@NotNull Map<String, Map<String, List<String>>> keyMap, @NotNull Matcher matcher,
@NotNull Map<String, Map<String, List<String>>> docMap, @NotNull String[] words,
@NotNull Map<String, Map<String, List<String>>> treeMap, @NotNull Project project) {
ProjectSettingsState set = ProjectSettingsState.getInstance(project);
Pattern extReplaceToSpace = set.extReplaceToSpace;
boolean isReplaceToSpace = extReplaceToSpace.pattern().length() != 0;
@NotNull Map<String, Map<String, List<String>>> treeMap) {
boolean haveDoc = false;
StringBuilder sb = new StringBuilder();
for (String s : words) {
if (appendDoc(set, sb, docMap, treeMap, extReplaceToSpace, isReplaceToSpace, s)) {
if (appendDoc(sb, s, docMap, treeMap)) {
haveDoc = true;
}
appendKeyDoc(set, sb, keyMap, matcher);
appendKeyDoc(sb, matcher, keyMap);
}
if (!haveDoc) {
return null;
@@ -83,23 +76,19 @@ public class LineExtUtils {
return sb.toString();
}
private static boolean appendDoc(ProjectSettingsState set, StringBuilder sb,
private static boolean appendDoc(@NotNull StringBuilder sb, @NotNull String word,
@NotNull Map<String, Map<String, List<String>>> docMap,
@NotNull Map<String, Map<String, List<String>>> treeMap,
Pattern extReplaceToSpace, boolean isReplaceToSpace, String word) {
@NotNull Map<String, Map<String, List<String>>> treeMap) {
word = word.trim();
if (isReplaceToSpace) {
word = extReplaceToSpace.matcher(word).replaceAll(" ");
}
if (word.length() == 0) {
return false;
}
String wordDoc = DocMapUtils.get(docMap, set, word);
String wordDoc = GetFromDocMap.get(docMap, word);
if (wordDoc != null) {
sb.append(wordDoc);
return true;
}
String treeDoc = DocMapUtils.get(treeMap, set, word);
String treeDoc = GetFromDocMap.get(treeMap, word);
if (treeDoc != null) {
sb.append(treeDoc);
return true;
@@ -108,15 +97,15 @@ public class LineExtUtils {
return false;
}
private static void appendKeyDoc(@NotNull ProjectSettingsState set, @NotNull StringBuilder sb,
@NotNull Map<String, Map<String, List<String>>> keyMap,
@NotNull Matcher matcher) {
private static void appendKeyDoc(@NotNull StringBuilder sb,
@NotNull Matcher matcher,
@NotNull Map<String, Map<String, List<String>>> keyMap) {
if (!matcher.find()) {
return;
}
String keyword = matcher.group();
// "" if no doc
String keyDoc = DocMapUtils.get(keyMap, set, keyword);
String keyDoc = GetFromDocMap.get(keyMap, keyword);
if (keyDoc != null) {
sb.append(" ").append(keyDoc);
}

View File

@@ -1,26 +1,23 @@
package io.github.linwancen.plugin.show.ext;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import io.github.linwancen.plugin.show.ext.conf.ConfCache;
import io.github.linwancen.plugin.show.settings.ProjectSettingsState;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
public class TreeExtUtils {
public class TreeExt {
private TreeExtUtils() {}
private TreeExt() {}
public static String extDoc(@NotNull Project project, @NotNull VirtualFile file) {
Map<String, Map<String, List<String>>> docMap = ConfCache.treeMap(project, file);
ProjectSettingsState set = ProjectSettingsState.getInstance(project);
public static String extDoc(@NotNull VirtualFile file) {
Map<String, Map<String, List<String>>> docMap = ConfCache.treeMap(file.getPath());
String[] words = {
file.getName(),
file.getNameWithoutExtension(),
};
String extDoc = DocMapUtils.get(docMap, set, words);
String extDoc = GetFromDocMap.get(docMap, words);
if (extDoc == null) {
return null;
}

View File

@@ -38,30 +38,33 @@ public class ConfCache {
private ConfCache() {}
@Nullable
public static Pattern pattern(@Nullable Project project, @NotNull VirtualFile file,
@NotNull Map<String, Map<String, List<String>>> keyMap) {
return ConfFactory.buildPattern(project, file.getPath(), keyMap);
public static Pattern pattern(@Nullable Project project,
@NotNull Map<String, Map<String, List<String>>> keyMap, @NotNull String path) {
return ConfFactory.buildPattern(project, path, keyMap);
}
@NotNull
public static Map<String, Map<String, List<String>>> keyMap(@Nullable Project project, @NotNull VirtualFile file) {
String ext = file.getExtension();
public static Map<String, Map<String, List<String>>> keyMap(@NotNull String path,
@NotNull String name,
@Nullable String ext) {
// file witch not ext can have itself ext doc
if (ext != null && !EXT_IN_KEY_CACHE.contains(ext)) {
// faster than find in KEY_CACHE
return Collections.emptyMap();
}
return ConfCacheGetUtils.filterPathNameExt(file, KEY_MID_EXT, KEY_CACHE);
return ConfCacheGetUtils.filterPathNameExt(KEY_MID_EXT, KEY_CACHE, path, name, ext);
}
@NotNull
public static Map<String, Map<String, List<String>>> docMap(@Nullable Project project, @NotNull VirtualFile file) {
return ConfCacheGetUtils.filterPathNameExt(file, DOC_MID_EXT, DOC_CACHE);
public static Map<String, Map<String, List<String>>> docMap(@NotNull String path,
@NotNull String name,
@Nullable String ext) {
return ConfCacheGetUtils.filterPathNameExt(DOC_MID_EXT, DOC_CACHE, path, name, ext);
}
@NotNull
public static Map<String, Map<String, List<String>>> treeMap(@Nullable Project project, @NotNull VirtualFile file) {
return ConfCacheGetUtils.filterPath(file, TREE_CACHE);
public static Map<String, Map<String, List<String>>> treeMap(@NotNull String path) {
return ConfCacheGetUtils.filterPath(TREE_CACHE, path);
}
static void clearAll() {

View File

@@ -3,6 +3,7 @@ package io.github.linwancen.plugin.show.ext.conf;
import com.intellij.openapi.vfs.VirtualFile;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.TreeMap;
@@ -28,12 +29,9 @@ class ConfCacheGetUtils {
* @return {@code <sortKey, T>}
*/
@NotNull
static <T> TreeMap<String, T> filterPathNameExt(@NotNull VirtualFile file,
@NotNull String confMidExt,
@NotNull Map<VirtualFile, T> cache) {
String path = file.getPath();
String name = file.getName();
String ext = file.getExtension();
static <T> TreeMap<String, T> filterPathNameExt(@NotNull String confMidExt,
@NotNull Map<VirtualFile, T> cache,
@NotNull String path, @NotNull String name, @Nullable String ext) {
TreeMap<String, T> map = new TreeMap<>();
int max = path.length();
int length = String.valueOf(max).length();
@@ -72,10 +70,9 @@ class ConfCacheGetUtils {
* @return {@code <sortKey, T>}
*/
@NotNull
static <T> TreeMap<String, T> filterPath(@NotNull VirtualFile file,
@SuppressWarnings("SameParameterValue")
@NotNull Map<VirtualFile, T> cache) {
String path = file.getPath();
static <T> TreeMap<String, T> filterPath(@SuppressWarnings("SameParameterValue")
@NotNull Map<VirtualFile, T> cache,
@NotNull String path) {
TreeMap<String, T> map = new TreeMap<>();
int max = path.length();
int length = String.valueOf(max).length();

View File

@@ -1,22 +0,0 @@
package io.github.linwancen.plugin.show.json;
import com.intellij.json.psi.JsonProperty;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class JsonUtils {
private JsonUtils() {}
@NotNull
public static List<String> jsonPath(JsonProperty jsonProp) {
ArrayList<String> jsonPath = new ArrayList<>();
do {
jsonPath.add(jsonProp.getName());
} while ((jsonProp = PsiTreeUtil.getParentOfType(jsonProp, JsonProperty.class)) != null);
return jsonPath;
}
}

View File

@@ -1,4 +1,4 @@
package io.github.linwancen.plugin.show.json;
package io.github.linwancen.plugin.show.jump;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;

View File

@@ -0,0 +1,69 @@
package io.github.linwancen.plugin.show.line;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.javadoc.PsiDocComment;
import io.github.linwancen.plugin.show.doc.PsiDocToStrDoc;
import io.github.linwancen.plugin.show.ext.LineExt;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* call LineExt, ~LeftToRight, ~RightToLeft
*/
public class FileViewToDocStrUtils {
private FileViewToDocStrUtils() {}
/**
* From Ext Or PsiDoc
*/
@Nullable
public static String doc(@NotNull Document document,
@Nullable Project project,
@Nullable VirtualFile file,
@Nullable FileViewProvider viewProvider,
int startOffset, int endOffset, @NotNull String text) {
if (file != null) {
String extDoc = LineExt.extDoc(project, file.getPath(), file.getName(), file.getExtension(), text);
if (extDoc != null) {
return extDoc;
}
}
if (viewProvider == null) {
return null;
}
PsiDocComment docComment = AppSettingsState.getInstance().findElementRightToLeft
? FileViewToPsiDocRightToLeft.rightDoc(viewProvider, startOffset, endOffset)
: FileViewToPsiDocLeftToRight.leftDoc(viewProvider, document, startOffset, endOffset);
return PsiDocToStrDoc.text(docComment);
}
@NotNull
public static String textWithDoc(@NotNull AppSettingsState settings, @NotNull Document document,
int startLine, int endLine,
@Nullable Project project,
@Nullable VirtualFile file,
@Nullable FileViewProvider viewProvider) {
StringBuilder sb = new StringBuilder();
for (int i = startLine; i <= endLine; i++) {
int startOffset = document.getLineStartOffset(i);
int endOffset = document.getLineEndOffset(i);
if (startOffset != endOffset) {
String text = document.getText(new TextRange(startOffset, endOffset));
sb.append(text);
String doc = doc(document, project, file, viewProvider, startOffset, endOffset, text);
if (doc != null) {
sb.append(settings.lineEndPrefix).append(doc);
}
}
sb.append("\n");
}
sb.delete(sb.length() - 1, sb.length());
return sb.toString();
}
}

View File

@@ -8,20 +8,23 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import io.github.linwancen.plugin.show.doc.JsonDocUtils;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class LineDocLeftToRightUtils {
/**
* call JsonToPsiDoc, NewCallRefToPsiDoc
*/
public class FileViewToPsiDocLeftToRight {
private LineDocLeftToRightUtils() {}
private FileViewToPsiDocLeftToRight() {}
static final String[] KEYS = {
"=",
};
@Nullable
public static PsiDocComment leftDoc(FileViewProvider viewProvider, Document document,
public static PsiDocComment leftDoc(@NotNull FileViewProvider viewProvider, @NotNull Document document,
int startOffset, int endOffset) {
String text = document.getText(new TextRange(startOffset, endOffset));
int offset = 0;
@@ -52,7 +55,7 @@ public class LineDocLeftToRightUtils {
}
AppSettingsState instance = AppSettingsState.getInstance();
if (instance.inJson && element.getLanguage().is(JsonLanguage.INSTANCE)) {
return JsonDocUtils.jsonDoc(element, startOffset, endOffset);
return JsonToPsiDoc.jsonDoc(element, startOffset, endOffset);
}
if (startWithSymbol) {
startOffset = 0;
@@ -61,10 +64,10 @@ public class LineDocLeftToRightUtils {
}
@Nullable
private static PsiDocComment nextDoc(PsiElement element, int startOffset, int endOffset) {
private static PsiDocComment nextDoc(@NotNull PsiElement element, int startOffset, int endOffset) {
while (element.getTextRange().getEndOffset() <= endOffset) {
if (element instanceof PsiIdentifier) {
PsiDocComment psiDocComment = LineDocUtils.elementDoc(element, element, startOffset, endOffset);
PsiDocComment psiDocComment = NewCallRefToPsiDoc.elementDoc(element, element, startOffset, endOffset);
if (psiDocComment != null) {
return psiDocComment;
}

View File

@@ -6,16 +6,19 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import io.github.linwancen.plugin.show.doc.JsonDocUtils;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class LineDocRightToLeftUtils {
/**
* call JsonToPsiDoc, NewCallRefToPsiDoc
*/
public class FileViewToPsiDocRightToLeft {
private LineDocRightToLeftUtils() {}
private FileViewToPsiDocRightToLeft() {}
@Nullable
public static PsiDocComment rightDoc(FileViewProvider viewProvider, int startOffset, int endOffset) {
public static PsiDocComment rightDoc(@NotNull FileViewProvider viewProvider, int startOffset, int endOffset) {
// End is always white, can not -1 because @see class.name need it
PsiElement element = viewProvider.findElementAt(endOffset);
if (element == null) {
@@ -39,15 +42,15 @@ public class LineDocRightToLeftUtils {
}
}
public static PsiDocComment psiDoc(AppSettingsState setting,
private static PsiDocComment psiDoc(AppSettingsState setting,
PsiElement identifier, PsiElement element,
int startOffset, int endOffset) {
if (identifier != null && !(identifier instanceof PsiIdentifier)) {
return null;
}
if (setting.inJson && element.getLanguage().is(JsonLanguage.INSTANCE)) {
return JsonDocUtils.jsonDoc(element, startOffset, endOffset);
return JsonToPsiDoc.jsonDoc(element, startOffset, endOffset);
}
return LineDocUtils.elementDoc(element, identifier, startOffset, endOffset);
return NewCallRefToPsiDoc.elementDoc(element, identifier, startOffset, endOffset);
}
}

View File

@@ -1,4 +1,4 @@
package io.github.linwancen.plugin.show.doc;
package io.github.linwancen.plugin.show.line;
import com.intellij.json.psi.JsonProperty;
import com.intellij.psi.PsiDocCommentOwner;
@@ -6,11 +6,12 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import io.github.linwancen.plugin.show.doc.OwnerToPsiDocUtils;
import org.jetbrains.annotations.Nullable;
public class JsonDocUtils {
public class JsonToPsiDoc {
private JsonDocUtils() {}
private JsonToPsiDoc() {}
/**
* depend on JsonJump
@@ -25,7 +26,7 @@ public class JsonDocUtils {
PsiElement resolve = reference.resolve();
if (resolve instanceof PsiDocCommentOwner) {
PsiDocCommentOwner owner = (PsiDocCommentOwner) resolve;
PsiDocComment docComment = DocUtils.srcOrByteCodeDoc(owner);
PsiDocComment docComment = OwnerToPsiDocUtils.srcOrByteCodeDoc(owner);
if (docComment != null) {
return docComment;
}

View File

@@ -7,9 +7,12 @@ import com.intellij.psi.util.PsiTreeUtil;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import org.jetbrains.annotations.Nullable;
class LineDocUtils {
/**
* call OwnerToPsiDocSkip
*/
class NewCallRefToPsiDoc {
private LineDocUtils() {}
private NewCallRefToPsiDoc() {}
@Nullable
static PsiDocComment elementDoc(PsiElement element, PsiElement psiIdentifier,
@@ -60,7 +63,7 @@ class LineDocUtils {
return null;
}
try {
PsiDocComment methodComment = SkipDocUtils.methodDoc(call.resolveMethod());
PsiDocComment methodComment = OwnerToPsiDocSkip.methodDoc(call.resolveMethod());
if (methodComment != null) {
return methodComment;
}
@@ -77,7 +80,7 @@ class LineDocUtils {
if (newExp == null) {
return null;
}
PsiDocComment methodComment = SkipDocUtils.methodDoc(newExp.resolveMethod());
PsiDocComment methodComment = OwnerToPsiDocSkip.methodDoc(newExp.resolveMethod());
if (methodComment != null) {
return methodComment;
}
@@ -104,7 +107,7 @@ class LineDocUtils {
if (element instanceof PsiReference) {
PsiElement resolve = ((PsiReference) element).resolve();
if (resolve instanceof PsiDocCommentOwner) {
return SkipDocUtils.refDoc(((PsiDocCommentOwner) resolve));
return OwnerToPsiDocSkip.refDoc(((PsiDocCommentOwner) resolve));
}
}
// for right to left for
@@ -128,7 +131,7 @@ class LineDocUtils {
}
PsiElement resolve = ref.resolve();
if (resolve instanceof PsiDocCommentOwner) {
return SkipDocUtils.refDoc(((PsiDocCommentOwner) resolve));
return OwnerToPsiDocSkip.refDoc(((PsiDocCommentOwner) resolve));
}
return null;
}
@@ -146,10 +149,10 @@ class LineDocUtils {
}
PsiElement resolve = reference.resolve();
if (resolve instanceof PsiMethod) {
return SkipDocUtils.methodDoc(((PsiMethod) resolve));
return OwnerToPsiDocSkip.methodDoc(((PsiMethod) resolve));
}
if (resolve instanceof PsiDocCommentOwner) {
return SkipDocUtils.refDoc(((PsiDocCommentOwner) resolve));
return OwnerToPsiDocSkip.refDoc(((PsiDocCommentOwner) resolve));
}
}
return null;

View File

@@ -4,18 +4,21 @@ import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDocCommentOwner;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.javadoc.PsiDocComment;
import io.github.linwancen.plugin.show.doc.DocUtils;
import io.github.linwancen.plugin.show.doc.OwnerToPsiDocUtils;
import org.jetbrains.annotations.Nullable;
class SkipDocUtils {
/**
* call RefToPsiDoc, PsiClassSkip
*/
class OwnerToPsiDocSkip {
private SkipDocUtils() {}
private OwnerToPsiDocSkip() {}
static PsiDocComment methodDoc(@Nullable PsiMethod psiMethod) {
if (skip(psiMethod)) {
return null;
}
return DocUtils.methodDoc(psiMethod);
return OwnerToPsiDocUtils.methodDoc(psiMethod);
}
static PsiDocComment refDoc(@Nullable PsiDocCommentOwner docOwner) {
@@ -23,18 +26,18 @@ class SkipDocUtils {
return null;
}
if (docOwner instanceof PsiMethod) {
return DocUtils.methodDoc(((PsiMethod) docOwner));
return OwnerToPsiDocUtils.methodDoc(((PsiMethod) docOwner));
}
return DocUtils.srcOrByteCodeDoc(docOwner);
return OwnerToPsiDocUtils.srcOrByteCodeDoc(docOwner);
}
static boolean skip(@Nullable PsiDocCommentOwner docOwner) {
private static boolean skip(@Nullable PsiDocCommentOwner docOwner) {
if (docOwner == null) {
return true;
}
if (docOwner instanceof PsiClass) {
return SkipUtils.skip((PsiClass) docOwner, docOwner.getProject());
return PsiClassSkip.skip((PsiClass) docOwner, docOwner.getProject());
}
return SkipUtils.skip(docOwner.getContainingClass(), docOwner.getProject());
return PsiClassSkip.skip(docOwner.getContainingClass(), docOwner.getProject());
}
}

View File

@@ -5,9 +5,9 @@ import com.intellij.psi.PsiClass;
import io.github.linwancen.plugin.show.settings.AppSettingsState;
import io.github.linwancen.plugin.show.settings.ProjectSettingsState;
class SkipUtils {
class PsiClassSkip {
private SkipUtils() {}
private PsiClassSkip() {}
static boolean skip(PsiClass psiClass, Project project) {
if (psiClass == null) {

View File

@@ -8,8 +8,6 @@ import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.regex.Pattern;
@State(
name = "io.github.linwancen.plugin.comment.settings.ProjectSettingsState",
storages = @Storage("ShowCommentProject.xml")
@@ -22,8 +20,6 @@ public class ProjectSettingsState implements PersistentStateComponent<ProjectSet
public String lineEndExclude = "";
public String[] lineEndIncludeArray = {};
public String[] lineEndExcludeArray = {};
public Pattern extReplaceToSpace = Pattern.compile("");
public int extDocColumn = 2;
public static ProjectSettingsState getInstance(Project project) {
return project.getService(ProjectSettingsState.class);

View File

@@ -98,5 +98,17 @@
text="🆑 // Clear External Comment">
<add-to-group group-id="ToolsMenu"/>
</action>
<action
id="io.github.linwancen.plugin.show.LineEndCopy"
class="io.github.linwancen.plugin.show.LineEndCopy"
text="// Copy With Line Comment">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
</action>
<action
id="io.github.linwancen.plugin.show.LineEndAdd"
class="io.github.linwancen.plugin.show.LineEndAdd"
text="// Add Line Comment">
<add-to-group group-id="ProjectViewPopupMenu" anchor="first"/>
</action>
</actions>
</idea-plugin>

View File

@@ -8,9 +8,9 @@ import org.junit.jupiter.api.Test;
import java.util.function.BiPredicate;
/**
* @see SkipUtils
* @see PsiClassSkip
*/
class SkipUtilsTest {
class PsiClassSkipTest {
public static final boolean o = true;
public static final boolean x = false;
@@ -65,7 +65,7 @@ class SkipUtilsTest {
String[] include = includes[includeIndex];
for (int excludeIndex = 0, excludesLength = excludes.length; excludeIndex < excludesLength; excludeIndex++) {
String[] exclude = excludes[excludeIndex];
boolean isSkip = SkipUtils.skipName(name, include, exclude);
boolean isSkip = PsiClassSkip.skipName(name, include, exclude);
String tip =
name + "==" + JsonOutput.toJson(include) + "!=" + JsonOutput.toJson(exclude) + "=>" + isSkip;
System.out.println(tip);
@@ -85,7 +85,7 @@ class SkipUtilsTest {
{x, o, o}, // {"io"},
{o, o, o}, // {"java", "io"},
};
loopTest(SkipUtils::include, results);
loopTest(PsiClassSkip::include, results);
}
@Test
@@ -97,7 +97,7 @@ class SkipUtilsTest {
{x, o, o}, // {"io"},
{o, o, o}, // {"java", "io"},
};
loopTest(SkipUtils::exclude, results);
loopTest(PsiClassSkip::exclude, results);
}
private void loopTest(BiPredicate<String, String[]> biPredicate, boolean[][] results) {