1.13 Add Copy With Line Comment & Add Line Comment | 增加 带行末注释复制 和 添加行末注释
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user