fix: valid file and dir

This commit is contained in:
林万程
2025-09-13 09:44:58 +08:00
parent de46e38d45
commit 89f619b4b7
3 changed files with 49 additions and 35 deletions

View File

@@ -67,46 +67,51 @@ public class LineEndCacheUtils {
private static void cacheUpdate(Project project, Map<VirtualFile, Map<Integer, LineEndCache>> fileMap) {
try {
fileMap.forEach((file, lineMap) -> lineMap.forEach((lineNumber, lineCache) -> {
@NotNull LineInfo info = lineCache.info;
@Nullable List<LineExtensionInfo> list = lineCache.map.get(info.text);
if (!(lineCache.needUpdate() || list == null)) {
if (project.isDisposed() || DumbService.isDumb(project)) {
return;
}
fileMap.forEach((file, lineMap) -> {
if (!file.isValid()) {
return;
}
try {
if (project.isDisposed() || DumbService.isDumb(project) || !file.isValid()) {
lineMap.forEach((lineNumber, lineCache) -> {
@NotNull LineInfo info = lineCache.info;
@Nullable List<LineExtensionInfo> list = lineCache.map.get(info.text);
if (!(lineCache.needUpdate() || list == null)) {
return;
}
@Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info);
@Nullable LineInfo info2 = LineInfo.of(info, lineNumber);
if (info2 == null || !info2.text.equals(info.text)) {
return;
}
if (list != null) {
list.clear();
}
// fix delete line get doc from before line because PsiFile be not updated
if ("}".equals(info.text.trim())) {
return;
}
if (lineExt != null) {
try {
@Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info);
@Nullable LineInfo info2 = LineInfo.of(info, lineNumber);
if (info2 == null || !info2.text.equals(info.text)) {
return;
}
if (list != null) {
list.add(lineExt);
} else {
@NotNull ArrayList<LineExtensionInfo> lineExtList = new ArrayList<>(1);
lineExtList.add(lineExt);
lineCache.map.put(info.text, lineExtList);
list.clear();
}
// fix delete line get doc from before line because PsiFile be not updated
if ("}".equals(info.text.trim())) {
return;
}
if (lineExt != null) {
if (list != null) {
list.add(lineExt);
} else {
@NotNull ArrayList<LineExtensionInfo> lineExtList = new ArrayList<>(1);
lineExtList.add(lineExt);
lineCache.map.put(info.text, lineExtList);
}
}
lineCache.updated();
} catch (ProcessCanceledException ignored) {
} catch (Throwable e) {
@Nullable String msg = e.getMessage();
if (msg == null || !msg.contains("File is not valid")) {
LOG.info("LineEndCacheUtils lineMap.forEach catch Throwable but log to record.", e);
}
}
lineCache.updated();
} catch (ProcessCanceledException ignored) {
} catch (Throwable e) {
@Nullable String msg = e.getMessage();
if (msg == null || !msg.contains("File is not valid")) {
LOG.info("LineEndCacheUtils lineMap.forEach catch Throwable but log to record.", e);
}
}
}));
});
});
} catch (ProcessCanceledException ignored) {
} catch (IllegalStateException ignore) {
// ignore inSmartMode(project) throw:

View File

@@ -41,7 +41,7 @@ public class XmlLangDoc extends BaseLangDoc {
public @Nullable <T extends SettingsInfo> String treeDoc(@NotNull T info, @NotNull ProjectViewNode<?> node,
@NotNull Project project) {
@Nullable VirtualFile virtualFile = node.getVirtualFile();
if (virtualFile == null) {
if (virtualFile == null || virtualFile.isDirectory()) {
return null;
}
@Nullable FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(virtualFile);

View File

@@ -59,7 +59,13 @@ public abstract class BaseLangDoc extends EditorLinePainter {
if (viewProvider == null) {
return null;
}
@Nullable PsiElement element = viewProvider.findElementAt(info.endOffset);
@Nullable PsiElement element;
try {
element = viewProvider.findElementAt(info.endOffset);
} catch (Throwable ignored) {
// InvalidVirtualFileAccessException
return null;
}
if (element == null) {
// file end
element = viewProvider.findElementAt(info.endOffset - 1);
@@ -177,6 +183,9 @@ public abstract class BaseLangDoc extends EditorLinePainter {
public static @Nullable <T extends SettingsInfo> String resolveDoc(@NotNull T info,
@NotNull PsiElement psiElement) {
try {
if (!psiElement.isValid()) {
return null;
}
// byte to src
PsiElement navElement = psiElement.getNavigationElement();
if (navElement != null) {