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) { private static void cacheUpdate(Project project, Map<VirtualFile, Map<Integer, LineEndCache>> fileMap) {
try { try {
fileMap.forEach((file, lineMap) -> lineMap.forEach((lineNumber, lineCache) -> { if (project.isDisposed() || DumbService.isDumb(project)) {
@NotNull LineInfo info = lineCache.info; return;
@Nullable List<LineExtensionInfo> list = lineCache.map.get(info.text); }
if (!(lineCache.needUpdate() || list == null)) { fileMap.forEach((file, lineMap) -> {
if (!file.isValid()) {
return; return;
} }
try { lineMap.forEach((lineNumber, lineCache) -> {
if (project.isDisposed() || DumbService.isDumb(project) || !file.isValid()) { @NotNull LineInfo info = lineCache.info;
@Nullable List<LineExtensionInfo> list = lineCache.map.get(info.text);
if (!(lineCache.needUpdate() || list == null)) {
return; return;
} }
@Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info); try {
@Nullable LineInfo info2 = LineInfo.of(info, lineNumber); @Nullable LineExtensionInfo lineExt = LineEnd.lineExt(info);
if (info2 == null || !info2.text.equals(info.text)) { @Nullable LineInfo info2 = LineInfo.of(info, lineNumber);
return; 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) {
if (list != null) { if (list != null) {
list.add(lineExt); list.clear();
} else { }
@NotNull ArrayList<LineExtensionInfo> lineExtList = new ArrayList<>(1); // fix delete line get doc from before line because PsiFile be not updated
lineExtList.add(lineExt); if ("}".equals(info.text.trim())) {
lineCache.map.put(info.text, lineExtList); 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 (ProcessCanceledException ignored) {
} catch (IllegalStateException ignore) { } catch (IllegalStateException ignore) {
// ignore inSmartMode(project) throw: // 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, public @Nullable <T extends SettingsInfo> String treeDoc(@NotNull T info, @NotNull ProjectViewNode<?> node,
@NotNull Project project) { @NotNull Project project) {
@Nullable VirtualFile virtualFile = node.getVirtualFile(); @Nullable VirtualFile virtualFile = node.getVirtualFile();
if (virtualFile == null) { if (virtualFile == null || virtualFile.isDirectory()) {
return null; return null;
} }
@Nullable FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(virtualFile); @Nullable FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(virtualFile);

View File

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