@Null Annotations, etc

This commit is contained in:
林万程
2022-10-23 20:24:25 +08:00
parent 032b50b23f
commit 44f024ac21
63 changed files with 761 additions and 678 deletions

View File

@@ -13,7 +13,7 @@ class GetFromDocMap {
@Nullable
static String get(@NotNull Map<String, Map<String, List<String>>> docMap, @NotNull String... words) {
List<String> keywordDoc = list(docMap, words);
@NotNull List<String> keywordDoc = list(docMap, words);
if (keywordDoc.size() >= 2) {
return keywordDoc.get(1);
}
@@ -22,7 +22,7 @@ class GetFromDocMap {
@NotNull
private static List<String> list(@NotNull Map<String, Map<String, List<String>>> docMap, @NotNull String... words) {
for (Map.Entry<String, Map<String, List<String>>> entry : docMap.entrySet()) {
for (@NotNull Map.Entry<String, Map<String, List<String>>> entry : docMap.entrySet()) {
Map<String, List<String>> map = entry.getValue();
for (String word : words) {
List<String> wordDoc = map.get(word);

View File

@@ -16,8 +16,8 @@ public class LineExt {
public static @Nullable String doc(@NotNull LineInfo lineInfo) {
int i = lineInfo.text.indexOf(lineInfo.appSettings.lineEndPrefix);
String code = i <= 0 ? lineInfo.text : lineInfo.text.substring(0, i);
String extDoc = LineExt.extDoc(lineInfo, code);
@NotNull String code = i <= 0 ? lineInfo.text : lineInfo.text.substring(0, i);
@Nullable String extDoc = LineExt.extDoc(lineInfo, code);
if (extDoc == null) {
return null;
}
@@ -28,20 +28,21 @@ public class LineExt {
return extDoc;
}
@Nullable
public static String extDoc(@NotNull LineInfo lineInfo, @NotNull String code) {
String path = lineInfo.file.getPath();
String name = lineInfo.file.getName();
String ext = lineInfo.file.getExtension();
Map<String, Map<String, List<String>>> keyMap = ConfCache.keyMap(path, name, ext);
@NotNull String path = lineInfo.file.getPath();
@NotNull String name = lineInfo.file.getName();
@Nullable String ext = lineInfo.file.getExtension();
@NotNull Map<String, Map<String, List<String>>> keyMap = ConfCache.keyMap(path, name, ext);
if (keyMap.isEmpty()) {
return null;
}
Pattern pattern = ConfCache.pattern(lineInfo.project, keyMap, path);
@Nullable Pattern pattern = ConfCache.pattern(lineInfo.project, keyMap, path);
if (pattern == null || pattern.pattern().length() == 0) {
return null;
}
Map<String, Map<String, List<String>>> docMap = ConfCache.docMap(path, name, ext);
Map<String, Map<String, List<String>>> treeMap = ConfCache.treeMap(path, name, ext);
@NotNull Map<String, Map<String, List<String>>> docMap = ConfCache.docMap(path, name, ext);
@NotNull Map<String, Map<String, List<String>>> treeMap = ConfCache.treeMap(path, name, ext);
if (docMap.isEmpty() && treeMap.isEmpty()) {
return null;
}
@@ -49,7 +50,7 @@ public class LineExt {
code = cblNotAndOr(code);
}
String[] words = pattern.split(code);
Matcher matcher = pattern.matcher(code);
@NotNull Matcher matcher = pattern.matcher(code);
return extDoc(keyMap, matcher, docMap, words, treeMap);
}
@@ -57,12 +58,12 @@ public class LineExt {
private static final Pattern AND_OR_PATTERN = Pattern.compile("(AND|OR) ?'");
@NotNull
private static String cblNotAndOr(String text) {
private static String cblNotAndOr(@NotNull String text) {
// maybe faster than regexp
if (!text.contains("=")) {
return text;
}
Matcher matcher = DICT_PATTERN.matcher(text);
@NotNull Matcher matcher = DICT_PATTERN.matcher(text);
if (!matcher.find()) {
return text;
}
@@ -81,13 +82,13 @@ public class LineExt {
@NotNull Map<String, Map<String, List<String>>> treeMap) {
boolean haveDoc = false;
boolean haveKey = false;
StringBuilder sb = new StringBuilder();
for (String s : words) {
@NotNull StringBuilder sb = new StringBuilder();
for (@NotNull String s : words) {
haveDoc |= appendDoc(sb, s, docMap, treeMap);
haveKey = appendKeyDoc(sb, matcher, keyMap);
}
while (haveKey) {
haveKey = appendKeyDoc(sb, matcher, keyMap);
haveKey = appendKeyDoc(sb, matcher, keyMap);
}
if (!haveDoc) {
return null;
@@ -102,12 +103,12 @@ public class LineExt {
if (word.length() == 0) {
return false;
}
String wordDoc = GetFromDocMap.get(docMap, word);
@Nullable String wordDoc = GetFromDocMap.get(docMap, word);
if (wordDoc != null) {
sb.append(wordDoc);
return true;
}
String treeDoc = GetFromDocMap.get(treeMap, word);
@Nullable String treeDoc = GetFromDocMap.get(treeMap, word);
if (treeDoc != null) {
sb.append(treeDoc);
return true;
@@ -118,14 +119,14 @@ public class LineExt {
}
private static boolean appendKeyDoc(@NotNull StringBuilder sb,
@NotNull Matcher matcher,
@NotNull Map<String, Map<String, List<String>>> keyMap) {
@NotNull Matcher matcher,
@NotNull Map<String, Map<String, List<String>>> keyMap) {
if (!matcher.find()) {
return false;
}
String keyword = matcher.group();
// "" if no doc
String keyDoc = GetFromDocMap.get(keyMap, keyword);
@Nullable String keyDoc = GetFromDocMap.get(keyMap, keyword);
if (keyDoc != null) {
sb.append(" ").append(keyDoc);
}

View File

@@ -13,21 +13,23 @@ public class TreeExt {
private TreeExt() {}
public static @Nullable String doc(ProjectViewNode<?> node) {
VirtualFile file = node.getVirtualFile();
public static @Nullable String doc(@NotNull ProjectViewNode<?> node) {
@Nullable VirtualFile file = node.getVirtualFile();
if (file == null) {
return null;
}
return TreeExt.extDoc(file);
}
@Nullable
public static String extDoc(@NotNull VirtualFile file) {
Map<String, Map<String, List<String>>> docMap = ConfCache.treeMap(file.getPath(), file.getName(), file.getPath());
String[] words = {
@NotNull Map<String, Map<String, List<String>>> docMap = ConfCache.treeMap(
file.getPath(), file.getName(), file.getPath());
@NotNull String[] words = {
file.getName(),
file.getNameWithoutExtension(),
};
String extDoc = GetFromDocMap.get(docMap, words);
@Nullable String extDoc = GetFromDocMap.get(docMap, words);
if (extDoc == null) {
return null;
}

View File

@@ -76,7 +76,7 @@ public class ConfCache {
TREE_CACHE.clear();
}
static void remove(@NotNull VirtualFile file, String name) {
static void remove(@NotNull VirtualFile file, @Nullable String name) {
if (name != null) {
int i = name.lastIndexOf('.');
name = name.substring(0, i);
@@ -93,7 +93,7 @@ public class ConfCache {
}
static void copy(@NotNull VirtualFile file, @NotNull VirtualFile newFile) {
String name = file.getNameWithoutExtension();
@NotNull String name = file.getNameWithoutExtension();
if (name.endsWith(KEY_MID_EXT)) {
copyCache(file, newFile, KEY_CACHE);
} else if (name.endsWith(DOC_MID_EXT)) {
@@ -117,9 +117,9 @@ public class ConfCache {
static void loadAll(@NotNull Project project) {
DumbService.getInstance(project).runReadActionInSmartMode(() ->
ApplicationManager.getApplication().runReadAction(() -> {
Collection<VirtualFile> files = FilenameIndex.getAllFilesByExt(project, EXT);
StringBuilder sb = new StringBuilder();
for (VirtualFile file : files) {
@NotNull Collection<VirtualFile> files = FilenameIndex.getAllFilesByExt(project, EXT);
@NotNull StringBuilder sb = new StringBuilder();
for (@NotNull VirtualFile file : files) {
load(project, file);
sb.append(file.getName()).append("\n");
}
@@ -139,16 +139,16 @@ public class ConfCache {
if (!ConfCache.EXT.equals(file.getExtension())) {
return;
}
Document document = FileDocumentManager.getInstance().getDocument(file);
@Nullable Document document = FileDocumentManager.getInstance().getDocument(file);
if (document == null) {
return;
}
String text = document.getText();
String name = file.getNameWithoutExtension();
@NotNull String text = document.getText();
@NotNull String name = file.getNameWithoutExtension();
// this pattern would skip empty line
String[] lines = LINE_PATTERN.split(text);
if (name.endsWith(KEY_MID_EXT)) {
String matchName = name.substring(0, name.length() - KEY_MID_EXT.length());
@NotNull String matchName = name.substring(0, name.length() - KEY_MID_EXT.length());
int i = matchName.lastIndexOf(".");
if (i > 0) {
EXT_IN_KEY_CACHE.add(matchName.substring(i + 1));

View File

@@ -32,13 +32,13 @@ class ConfCacheGetUtils {
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<>();
@NotNull TreeMap<String, T> map = new TreeMap<>();
int max = path.length();
int length = String.valueOf(max).length();
for (Map.Entry<VirtualFile, T> entry : cache.entrySet()) {
for (@NotNull Map.Entry<VirtualFile, T> entry : cache.entrySet()) {
VirtualFile confFile = entry.getKey();
String confName = confFile.getNameWithoutExtension();
String confPath = confFile.getPath();
@NotNull String confName = confFile.getNameWithoutExtension();
@NotNull String confPath = confFile.getPath();
int level = level(path, confPath);
if (level == 0) {
continue;
@@ -76,12 +76,12 @@ class ConfCacheGetUtils {
static <T> TreeMap<String, T> filterPath(@SuppressWarnings("SameParameterValue")
@NotNull Map<VirtualFile, T> cache,
@NotNull String path, String name, String ext) {
TreeMap<String, T> map = new TreeMap<>();
@NotNull TreeMap<String, T> map = new TreeMap<>();
int max = path.length();
int length = String.valueOf(max).length();
for (Map.Entry<VirtualFile, T> entry : cache.entrySet()) {
for (@NotNull Map.Entry<VirtualFile, T> entry : cache.entrySet()) {
VirtualFile confFile = entry.getKey();
String confPath = confFile.getPath();
@NotNull String confPath = confFile.getPath();
int level = level(path, confPath);
if (level == 0) {
continue;
@@ -119,7 +119,7 @@ class ConfCacheGetUtils {
}
@NotNull
private static String srcPath(String path) {
private static String srcPath(@NotNull String path) {
int i = path.indexOf('!');
if (i != -1) {
return path.substring(i + 1);
@@ -131,7 +131,7 @@ class ConfCacheGetUtils {
return path;
}
private static boolean match(String path, String confPath) {
private static boolean match(@NotNull String path, @NotNull String confPath) {
if (confPath.equals(path) || SKIP_PATH.equals(confPath)) {
return true;
}

View File

@@ -29,11 +29,11 @@ class ConfFactory {
@Nullable
static Pattern buildPattern(@Nullable Project project, @NotNull String path,
@NotNull Map<String, Map<String, List<String>>> map) {
Set<String> exclude = new LinkedSet<>();
StringBuilder sb = new StringBuilder();
for (Map<String, List<String>> keyMap : map.values()) {
@NotNull Set<String> exclude = new LinkedSet<>();
@NotNull StringBuilder sb = new StringBuilder();
for (@NotNull Map<String, List<String>> keyMap : map.values()) {
// key() is escape
for (List<String> list : keyMap.values()) {
for (@NotNull List<String> list : keyMap.values()) {
String key = list.get(0);
if (key.startsWith("?")) {
exclude.add(key.substring(1));
@@ -45,7 +45,7 @@ class ConfFactory {
if (sb.length() > 0) {
sb.delete(sb.length() - 1, sb.length());
}
String regex = sb.toString();
@NotNull String regex = sb.toString();
Pattern pattern = PATTERN_CACHE.get(regex);
if (pattern != null) {
return pattern;
@@ -54,7 +54,7 @@ class ConfFactory {
sb.insert(0, path);
sb.insert(0, "\n");
try {
Pattern compile = Pattern.compile(regex);
@NotNull Pattern compile = Pattern.compile(regex);
PATTERN_CACHE.put(regex, compile);
REGEXP_LOG.createNotification("Ext doc keyword regexp compile success", regex.length() + " chars",
sb.toString(), NotificationType.INFORMATION).notify(project);
@@ -74,9 +74,9 @@ class ConfFactory {
@NotNull
static Map<String, List<String>> buildMap(@Nullable Project project, @NotNull String path,
@NotNull String[] lines, boolean isKey) {
Map<String, List<String>> map = new LinkedHashMap<>();
for (String line : lines) {
List<String> words = Splitter.on('\t').splitToList(line);
@NotNull Map<String, List<String>> map = new LinkedHashMap<>();
for (@NotNull String line : lines) {
@NotNull List<String> words = Splitter.on('\t').splitToList(line);
if (!words.isEmpty()) {
String key = words.get(0);
if (key.length() == 0) {

View File

@@ -4,6 +4,7 @@ import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* call ConfCache.loadFile
@@ -12,7 +13,7 @@ public class ConfFileChangeListener implements FileEditorManagerListener {
@Override
public void selectionChanged(@NotNull FileEditorManagerEvent event) {
VirtualFile file = event.getOldFile();
@Nullable VirtualFile file = event.getOldFile();
if (file == null) {
return;
}

View File

@@ -4,6 +4,7 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.BulkFileListener;
import com.intellij.openapi.vfs.newvfs.events.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@@ -14,18 +15,18 @@ public class ConfFileListener implements BulkFileListener {
@Override
public void after(@NotNull List<? extends VFileEvent> events) {
for (VFileEvent event : events) {
for (@NotNull VFileEvent event : events) {
forEvent(event);
}
}
private static void forEvent(VFileEvent event) {
VirtualFile file = event.getFile();
private static void forEvent(@NotNull VFileEvent event) {
@Nullable VirtualFile file = event.getFile();
if (file == null) {
return;
}
if (event instanceof VFilePropertyChangeEvent) {
VFilePropertyChangeEvent changeEvent = (VFilePropertyChangeEvent) event;
@NotNull VFilePropertyChangeEvent changeEvent = (VFilePropertyChangeEvent) event;
if ("name".equals(changeEvent.getPropertyName())) {
String oldName = changeEvent.getOldValue().toString();
if (oldName.endsWith(ConfCache.EXT)) {
@@ -45,8 +46,8 @@ public class ConfFileListener implements BulkFileListener {
return;
}
if (event instanceof VFileCopyEvent) {
VFileCopyEvent copyEvent = (VFileCopyEvent) event;
VirtualFile newFile = copyEvent.findCreatedFile();
@NotNull VFileCopyEvent copyEvent = (VFileCopyEvent) event;
@Nullable VirtualFile newFile = copyEvent.findCreatedFile();
if (newFile == null) {
return;
}

View File

@@ -4,6 +4,7 @@ import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* call ConfCache.loadAll
@@ -11,7 +12,7 @@ import org.jetbrains.annotations.NotNull;
public class ReloadExtDocAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Project project = e.getProject();
@Nullable Project project = e.getProject();
if (project == null) {
return;
}