chore: IDEA add @NotNull @Nullable

This commit is contained in:
林万程
2025-09-28 23:03:07 +08:00
parent bd243ce877
commit f3762a1c91
17 changed files with 77 additions and 69 deletions

View File

@@ -16,7 +16,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MavenLangDoc extends XmlLangDoc {
Pattern PARAM_PATTERN = Pattern.compile("\\$\\{[^}]++}");
@NotNull Pattern PARAM_PATTERN = Pattern.compile("\\$\\{[^}]++}");
@Nullable
@Override
@@ -25,12 +25,12 @@ public class MavenLangDoc extends XmlLangDoc {
if (!"pom.xml".equals(info.file.getName()) || !info.text.contains("$")) {
return null;
}
MavenDomProjectModel mdm = MavenDomUtil.getMavenDomModel(element.getContainingFile(), MavenDomProjectModel.class);
@Nullable MavenDomProjectModel mdm = MavenDomUtil.getMavenDomModel(element.getContainingFile(), MavenDomProjectModel.class);
if (mdm == null) {
return null;
}
List<String> list = new ArrayList<>();
Matcher m = PARAM_PATTERN.matcher(info.text);
@NotNull List<String> list = new ArrayList<>();
@NotNull Matcher m = PARAM_PATTERN.matcher(info.text);
while (m.find()) {
String s = m.group();
String v;

View File

@@ -73,7 +73,7 @@ public class ScalaLangDoc extends JavaLangDoc {
for (@NotNull PsiDocTag tag : tags) {
if (tag instanceof ScDocTag) {
@NotNull ScDocTag scDocTag = (ScDocTag) tag;
String doc = PsiUnSaveUtils.getText(scDocTag);
@NotNull String doc = PsiUnSaveUtils.getText(scDocTag);
doc = doc.replace(key, "");
DocFilter.addHtml(tagStrBuilder, doc);
}

View File

@@ -17,6 +17,7 @@ import org.jetbrains.annotations.Nullable;
public class AnnoDocJava extends BaseAnnoDoc<PsiJvmModifiersOwner> {
@NotNull
public static AnnoDocJava INSTANCE = new AnnoDocJava();
private AnnoDocJava() {}
@@ -76,8 +77,8 @@ public class AnnoDocJava extends BaseAnnoDoc<PsiJvmModifiersOwner> {
@NotNull Project project = value.getProject();
@NotNull PsiConstantEvaluationHelper helper = JavaPsiFacade.getInstance(project).getConstantEvaluationHelper();
if (value instanceof PsiArrayInitializerMemberValue) {
PsiAnnotationMemberValue[] initializers = ((PsiArrayInitializerMemberValue) value).getInitializers();
StringBuilder sb = new StringBuilder();
@NotNull PsiAnnotationMemberValue[] initializers = ((PsiArrayInitializerMemberValue) value).getInitializers();
@NotNull StringBuilder sb = new StringBuilder();
for (PsiAnnotationMemberValue initializer : initializers) {
try {
sb.append(helper.computeConstantExpression(initializer)).append(" ");

View File

@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.psi.ValueArgumentName;
public class AnnoDocKt extends BaseAnnoDoc<KtAnnotated> {
@NotNull
public static AnnoDocKt INSTANCE = new AnnoDocKt();
private AnnoDocKt() {}
@@ -24,8 +25,8 @@ public class AnnoDocKt extends BaseAnnoDoc<KtAnnotated> {
@Nullable
protected String annoDocMatch(@NotNull KtAnnotated owner, @NotNull String[] arr) {
if (typeMatch(owner, arr[0])) {
for (KtAnnotationEntry entry : owner.getAnnotationEntries()) {
String s = annoDocName(entry, arr);
for (@NotNull KtAnnotationEntry entry : owner.getAnnotationEntries()) {
@Nullable String s = annoDocName(entry, arr);
if (s != null) {
return s;
}
@@ -52,11 +53,11 @@ public class AnnoDocKt extends BaseAnnoDoc<KtAnnotated> {
@Nullable
private static String annoDocName(@NotNull KtAnnotationEntry entry, @NotNull String[] arr) {
Name shortName = entry.getShortName();
@Nullable Name shortName = entry.getShortName();
if (shortName == null) {
return null;
}
String name = shortName.asString();
@NotNull String name = shortName.asString();
String annoName = arr[1];
int i = annoName.lastIndexOf('.');
if (i > 0) {
@@ -65,15 +66,15 @@ public class AnnoDocKt extends BaseAnnoDoc<KtAnnotated> {
if (!annoName.equals(name)) {
return null;
}
for (ValueArgument argument : entry.getValueArguments()) {
ValueArgumentName argumentName = argument.getArgumentName();
String method = argumentName == null ? "value" : argumentName.getAsName().asString();
for (@NotNull ValueArgument argument : entry.getValueArguments()) {
@Nullable ValueArgumentName argumentName = argument.getArgumentName();
@NotNull String method = argumentName == null ? "value" : argumentName.getAsName().asString();
if (arr[2].equals(method)) {
KtExpression expression = argument.getArgumentExpression();
@Nullable KtExpression expression = argument.getArgumentExpression();
if (expression instanceof KtStringTemplateExpression) {
String text = PsiUnSaveUtils.getText(expression);
@NotNull String text = PsiUnSaveUtils.getText(expression);
if (text.length() >= 2) {
String s = text.substring(1, text.length() - 1);
@NotNull String s = text.substring(1, text.length() - 1);
if (!s.isEmpty()) {
return s;
}

View File

@@ -61,7 +61,7 @@ public class SkipUtils {
if (info.appSettings.skipBlank && isBlank(doc)) {
return null;
}
String text = PsiUnSaveUtils.getText(doc);
@NotNull String text = PsiUnSaveUtils.getText(doc);
boolean skip = DocSkip.skipDoc(info, text);
return skip ? null : doc;
}
@@ -69,7 +69,7 @@ public class SkipUtils {
private static boolean isBlank(@NotNull PsiDocComment doc) {
@NotNull PsiElement[] elements = doc.getDescriptionElements();
for (@NotNull PsiElement element : elements) {
String text = PsiUnSaveUtils.getText(element);
@NotNull String text = PsiUnSaveUtils.getText(element);
if (StringUtils.isNotBlank(text)) {
return false;
}

View File

@@ -56,7 +56,7 @@ public class FileInfo extends SettingsInfo {
return new FileInfo(file, document, project, FuncEnum.LINE);
}
public String getText(PsiElement element) {
public String getText(@NotNull PsiElement element) {
try {
TextRange range = element.getTextRange();
int startOffset = range.getStartOffset();

View File

@@ -65,7 +65,7 @@ public class LineEndCacheUtils {
}
}
private static void cacheUpdate(Project project, Map<VirtualFile, Map<Integer, LineEndCache>> fileMap) {
private static void cacheUpdate(@NotNull Project project, @NotNull Map<VirtualFile, Map<Integer, LineEndCache>> fileMap) {
try {
if (project.isDisposed() || DumbService.isDumb(project)) {
return;

View File

@@ -40,7 +40,7 @@ public class TreeCacheUtils {
}
}
private static void cacheUpdate(Project project, Map<ProjectViewNode<?>, TreeCache> nodeCache) {
private static void cacheUpdate(@NotNull Project project, @NotNull Map<ProjectViewNode<?>, TreeCache> nodeCache) {
try {
nodeCache.forEach((node, treeCache) -> {
if (treeCache.needUpdate) {

View File

@@ -34,7 +34,7 @@ public class SqlCache extends FileLoader {
@Nullable
@Override
public String treeDoc(@NotNull VirtualFile virtualFile) {
String name = virtualFile.getNameWithoutExtension();
@NotNull String name = virtualFile.getNameWithoutExtension();
return TABLE_DOC_CACHE.get(name);
}
@@ -52,8 +52,8 @@ public class SqlCache extends FileLoader {
@Override
public void loadAllImpl(@NotNull Project project) {
GlobalSettingsState g = GlobalSettingsState.getInstance();
ProjectSettingsState p = ProjectSettingsState.getInstance(project);
@NotNull GlobalSettingsState g = GlobalSettingsState.getInstance();
@NotNull ProjectSettingsState p = ProjectSettingsState.getInstance(project);
if (!g.sqlSplitEffect && !p.sqlSplitEffect) {
return;
}
@@ -74,18 +74,18 @@ public class SqlCache extends FileLoader {
@Override
public void loadFileImpl(@NotNull VirtualFile file, @Nullable Project project) {
String code = PsiUnSaveUtils.fileText(project, file);
@Nullable String code = PsiUnSaveUtils.fileText(project, file);
if (code == null) {
return;
}
GlobalSettingsState g = GlobalSettingsState.getInstance();
@NotNull GlobalSettingsState g = GlobalSettingsState.getInstance();
loadSqlDoc(code, TABLE_DOC_CACHE, g.tableDocEffect, g.tableDoc);
loadSqlDoc(code, COLUMN_DOC_CACHE, g.columnDocEffect, g.columnDoc);
loadSqlDoc(code, INDEX_DOC_CACHE, g.indexDocEffect, g.indexDoc);
if (project == null) {
return;
}
ProjectSettingsState p = ProjectSettingsState.getInstance(project);
@NotNull ProjectSettingsState p = ProjectSettingsState.getInstance(project);
loadSqlDoc(code, TABLE_DOC_CACHE, p.tableDocEffect, p.tableDoc);
loadSqlDoc(code, COLUMN_DOC_CACHE, p.columnDocEffect, p.columnDoc);
loadSqlDoc(code, INDEX_DOC_CACHE, p.indexDocEffect, p.indexDoc);
@@ -94,13 +94,13 @@ public class SqlCache extends FileLoader {
public static final Pattern KEY_PATTERN = Pattern.compile("`?, *`?");
private static void loadSqlDoc(@NotNull String code, @NotNull Map<String, String> map,
boolean effect, Map<String, Pattern[]> patternMap) {
boolean effect, @NotNull Map<String, Pattern[]> patternMap) {
if (!effect) {
return;
}
for (Pattern[] patterns : patternMap.values()) {
for (Pattern pattern : patterns) {
Matcher m = pattern.matcher(code);
for (@NotNull Pattern[] patterns : patternMap.values()) {
for (@NotNull Pattern pattern : patterns) {
@NotNull Matcher m = pattern.matcher(code);
while (m.find()) {
String key = m.group(1);
String comment = m.group(2);
@@ -121,7 +121,7 @@ public class SqlCache extends FileLoader {
private static final Pattern IGNORE_KEY_PATTERN = Pattern.compile("(?i)^(id|name)$");
private static void putComment(@NotNull Map<String, String> map, @NotNull String k, String comment) {
private static void putComment(@NotNull Map<String, String> map, @NotNull String k, @NotNull String comment) {
if (IGNORE_KEY_PATTERN.matcher(k).find()) {
return;
}

View File

@@ -12,17 +12,18 @@ public class SqlDoc {
@Nullable
public static String sqlDoc(@NotNull LineInfo info, @NotNull String code) {
String extension = info.file.getExtension();
@Nullable String extension = info.file.getExtension();
if (extension == null) {
return null;
}
String s = sqlDocState(extension, info.projectSettings, code);
@Nullable String s = sqlDocState(extension, info.projectSettings, code);
if (s != null) {
return s;
}
return sqlDocState(extension, info.globalSettings, code);
}
@Nullable
private static String sqlDocState(@NotNull String extension, @NotNull AbstractSettingsState state,
@NotNull String code) {
if (!state.sqlSplitEffect) {
@@ -32,7 +33,7 @@ public class SqlDoc {
if (patterns == null) {
return null;
}
for (Pattern pattern : patterns) {
for (@NotNull Pattern pattern : patterns) {
return sqlDocPattern(pattern, code);
}
return null;
@@ -83,7 +84,8 @@ public class SqlDoc {
return false;
}
private static String indexSymbol(String indexDoc) {
@NotNull
private static String indexSymbol(@NotNull String indexDoc) {
switch (indexDoc) {
case "primary":
case "PRIMARY":
@@ -110,7 +112,7 @@ public class SqlDoc {
}
@NotNull
private static String unescape(String keyword) {
private static String unescape(@NotNull String keyword) {
// &gt;|&lt;|&amp;|&quot;|&apos;
switch (keyword) {
case "&gt;":

View File

@@ -48,7 +48,7 @@ public class PhpLangDoc extends BaseTagLangDoc<PhpDocComment> {
for (PsiElement child : children) {
@Nullable PsiComment comment = PsiTreeUtil.getChildOfType(child, PsiComment.class);
if (comment != null) {
String text = PsiUnSaveUtils.getText(comment);
@NotNull String text = PsiUnSaveUtils.getText(comment);
return DocFilter.cutDoc(text, info, true);
}
}
@@ -75,7 +75,7 @@ public class PhpLangDoc extends BaseTagLangDoc<PhpDocComment> {
@NotNull
@Override
protected <T extends SettingsInfo> String descDoc(@NotNull T info, @NotNull PhpDocComment phpDocComment) {
String text = PsiUnSaveUtils.getText(phpDocComment);
@NotNull String text = PsiUnSaveUtils.getText(phpDocComment);
return DocFilter.cutDoc(text, info, true);
}

View File

@@ -48,9 +48,9 @@ public class XmlLangDoc extends BaseLangDoc {
if (viewProvider == null) {
return null;
}
PsiElement psiElement = viewProvider.findElementAt(0);
@Nullable PsiElement psiElement = viewProvider.findElementAt(0);
if (psiElement == null || !"<!--".equals(PsiUnSaveUtils.getText(psiElement))) {
Document document = viewProvider.getDocument();
@Nullable Document document = viewProvider.getDocument();
if (document == null) {
return null;
}
@@ -70,11 +70,11 @@ public class XmlLangDoc extends BaseLangDoc {
if (!(parent instanceof PsiComment)) {
return null;
}
PsiElement[] children = parent.getChildren();
@NotNull PsiElement[] children = parent.getChildren();
if (children.length < 2) {
return null;
}
String doc = PsiUnSaveUtils.getText(children[1]);
@Nullable String doc = PsiUnSaveUtils.getText(children[1]);
// Copyright or copyright
//noinspection SpellCheckingInspection
if (doc == null || doc.contains("opyright")) {
@@ -87,10 +87,10 @@ public class XmlLangDoc extends BaseLangDoc {
@Nullable
@Override
public String findRefDoc(@NotNull LineInfo info, @NotNull FileViewProvider viewProvider, @NotNull PsiElement element) {
ExtensionPointName<XmlLangDoc> epn = ExtensionPointName.create("io.github.linwancen.show-comment.xmlLangDoc");
List<XmlLangDoc> extensionList = epn.getExtensionList();
for (XmlLangDoc xmlLangDoc : extensionList) {
String doc = xmlLangDoc.findRefDoc(info, viewProvider, element);
@NotNull ExtensionPointName<XmlLangDoc> epn = ExtensionPointName.create("io.github.linwancen.show-comment.xmlLangDoc");
@NotNull List<XmlLangDoc> extensionList = epn.getExtensionList();
for (@NotNull XmlLangDoc xmlLangDoc : extensionList) {
@Nullable String doc = xmlLangDoc.findRefDoc(info, viewProvider, element);
if (doc != null && !doc.trim().isEmpty()) {
return doc;
}

View File

@@ -62,7 +62,7 @@ public class Prev {
@Nullable
private static PsiElement refClassParent(@NotNull PsiElement element,
@NotNull List<Class<? extends PsiElement>> refClass) {
String text = PsiUnSaveUtils.getText(element);
@NotNull String text = PsiUnSaveUtils.getText(element);
if (!SYMBOL_PATTERN.matcher(text).find()) {
return null;
}

View File

@@ -8,12 +8,15 @@ import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PsiUnSaveUtils {
public static String getText(PsiElement element) {
@Contract("!null -> !null")
@Nullable
public static String getText(@Nullable PsiElement element) {
try {
if (element == null) {
return null;
@@ -23,7 +26,7 @@ public class PsiUnSaveUtils {
return element.getText();
}
if (element instanceof PsiFile) {
Document document = documentManager.getDocument(((PsiFile) element));
@Nullable Document document = documentManager.getDocument(((PsiFile) element));
if (document == null) {
return element.getText();
}
@@ -33,7 +36,7 @@ public class PsiUnSaveUtils {
if (containingFile == null) {
return element.getText();
}
Document document = documentManager.getDocument(containingFile);
@Nullable Document document = documentManager.getDocument(containingFile);
if (document == null) {
return element.getText();
}
@@ -45,14 +48,14 @@ public class PsiUnSaveUtils {
@Nullable
public static String fileText(@Nullable Project project, @NotNull VirtualFile file) {
Document document = FileDocumentManager.getInstance().getDocument(file);
@Nullable Document document = FileDocumentManager.getInstance().getDocument(file);
if (document != null) {
return document.getText();
}
if (project == null) {
return null;
}
PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
@Nullable PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
if (psiFile != null) {
return psiFile.getText();
}

View File

@@ -74,7 +74,7 @@ public class ResolveDoc {
}
if (!keywords.isEmpty()) {
while (psiElement != null) {
String text = PsiUnSaveUtils.getText(psiElement);
@NotNull String text = PsiUnSaveUtils.getText(psiElement);
if (keywords.contains(text)) {
psiElement = Prev.prevCompactElement(info, psiElement, document);
} else {
@@ -85,7 +85,7 @@ public class ResolveDoc {
@NotNull StringBuilder sb = new StringBuilder();
boolean isComment = psiElement instanceof PsiComment;
while (isComment) {
String text = PsiUnSaveUtils.getText(psiElement);
@NotNull String text = PsiUnSaveUtils.getText(psiElement);
int thisStartOffset = psiElement.getTextRange().getStartOffset();
psiElement = Prev.prevCompactElement(info, psiElement, document);
isComment = psiElement instanceof PsiComment;

View File

@@ -77,7 +77,7 @@ public class VueRouterCache extends FileLoader {
if (psiFile == null) {
return;
}
Collection<JSArrayLiteralExpression> arrays =
@NotNull Collection<JSArrayLiteralExpression> arrays =
PsiTreeUtil.findChildrenOfType(psiFile, JSArrayLiteralExpression.class);
for (JSArrayLiteralExpression arr : arrays) {
parseArr(arr);

View File

@@ -2,6 +2,7 @@ package io.github.linwancen.plugin.show.lang.base;
import groovy.json.JsonOutput;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -33,7 +34,7 @@ class DocSkipTest {
@Test
void skipName() {
// o include, x skip
boolean[][][] results = {{
@NotNull boolean[][][] results = {{
// "java" -- name
// {}, {"java"}, {"io.b"}, {"java", "io.b"} -- exclude
{o, x, o, x}, // {},
@@ -56,18 +57,18 @@ class DocSkipTest {
{o, o, x, x}, // {"java", "io"},
}};
for (int i = 0, namesLength = names.length; i < namesLength; i++) {
String name = names[i];
@NotNull String name = names[i];
loopTest(name, results[i]);
}
}
private void loopTest(String name, boolean[][] results) {
private void loopTest(@NotNull String name, boolean[][] results) {
for (int includeIndex = 0, includesLength = includes.length; includeIndex < includesLength; includeIndex++) {
Pattern include = includes[includeIndex];
@NotNull Pattern include = includes[includeIndex];
for (int excludeIndex = 0, excludesLength = excludes.length; excludeIndex < excludesLength; excludeIndex++) {
Pattern exclude = excludes[excludeIndex];
@NotNull Pattern exclude = excludes[excludeIndex];
boolean isSkip = DocSkip.skipText(name, include, exclude);
String tip =
@NotNull String tip =
name + "==" + JsonOutput.toJson(include) + "!=" + JsonOutput.toJson(exclude) + "=>" + isSkip;
System.out.println(tip);
// o true include, x false skip, so use '!'
@@ -79,7 +80,7 @@ class DocSkipTest {
@Test
void include() {
boolean[][] results = {
@NotNull boolean[][] results = {
// {"java", "io.a", "io.b"} -- name
{o, o, o}, // {},
{o, x, x}, // {"java"},
@@ -91,7 +92,7 @@ class DocSkipTest {
@Test
void exclude() {
boolean[][] results = {
@NotNull boolean[][] results = {
// {"java", "io.a", "io.b"} ... names
{x, x, x}, // {},
{o, x, x}, // {"java"},
@@ -101,13 +102,13 @@ class DocSkipTest {
loopTest(DocSkip::exclude, results);
}
private void loopTest(BiPredicate<String, Pattern> biPredicate, boolean[][] results) {
private void loopTest(@NotNull BiPredicate<String, Pattern> biPredicate, boolean[][] results) {
for (int includeIndex = 0, includesLength = includes.length; includeIndex < includesLength; includeIndex++) {
Pattern include = includes[includeIndex];
@NotNull Pattern include = includes[includeIndex];
for (int nameIndex = 0, namesLength = names.length; nameIndex < namesLength; nameIndex++) {
String name = names[nameIndex];
@NotNull String name = names[nameIndex];
boolean result = biPredicate.test(name, include);
String tip = name + "==" + JsonOutput.toJson(include) + "=>" + result;
@NotNull String tip = name + "==" + JsonOutput.toJson(include) + "=>" + result;
System.out.println(tip);
Assertions.assertEquals(results[includeIndex][nameIndex], result, tip);
}