From 6adfa3221fa131f4128ca4de7ab84cc3ce5121c0 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 31 Dec 2012 15:22:39 +0100 Subject: [PATCH] Do not index and navigate to broken links --- .../com/intellij/openapi/vfs/VfsUtilCore.java | 11 ++++++++++ .../com/intellij/ide/util/EditSourceUtil.java | 12 +++++----- .../openapi/project/FileContentQueue.java | 22 ++++++++++++------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/platform/core-api/src/com/intellij/openapi/vfs/VfsUtilCore.java b/platform/core-api/src/com/intellij/openapi/vfs/VfsUtilCore.java index 04880607af00..a82d4b73e241 100644 --- a/platform/core-api/src/com/intellij/openapi/vfs/VfsUtilCore.java +++ b/platform/core-api/src/com/intellij/openapi/vfs/VfsUtilCore.java @@ -261,6 +261,17 @@ public class VfsUtilCore { } } + /** + * Returns {@code true} if given virtual file represents broken symbolic link (which points to non-existent file). + */ + public static boolean isBrokenLink(@NotNull VirtualFile link) { + final VirtualFile target = link.getCanonicalFile(); + return target == null || target.equals(link); + } + + /** + * Returns {@code true} if given virtual file represents broken or recursive symbolic link. + */ public static boolean isInvalidLink(@NotNull VirtualFile link) { final VirtualFile target = link.getCanonicalFile(); return target == null || target.equals(link) || isAncestor(target, link, true); diff --git a/platform/lang-api/src/com/intellij/ide/util/EditSourceUtil.java b/platform/lang-api/src/com/intellij/ide/util/EditSourceUtil.java index 89ed5895b907..2f4f98a5a945 100644 --- a/platform/lang-api/src/com/intellij/ide/util/EditSourceUtil.java +++ b/platform/lang-api/src/com/intellij/ide/util/EditSourceUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import com.intellij.navigation.NavigationItem; import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.fileEditor.OpenFileDescriptor; import com.intellij.openapi.util.UserDataHolder; +import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.pom.Navigatable; import com.intellij.pom.PomTargetPsiElement; @@ -29,8 +30,7 @@ import com.intellij.psi.util.PsiUtilCore; import org.jetbrains.annotations.Nullable; public class EditSourceUtil { - private EditSourceUtil() { - } + private EditSourceUtil() { } @Nullable public static Navigatable getDescriptor(final PsiElement element) { @@ -58,9 +58,9 @@ public class EditSourceUtil { if (element == null || !element.isValid()) { return false; } - final PsiElement navigationElement = element.getNavigationElement(); - final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(navigationElement); - return virtualFile != null && virtualFile.isValid() && !virtualFile.isSpecialFile(); + + VirtualFile file = PsiUtilCore.getVirtualFile(element.getNavigationElement()); + return file != null && file.isValid() && !file.isSpecialFile() && !VfsUtilCore.isBrokenLink(file); } public static void navigate(NavigationItem item, boolean requestFocus, boolean useCurrentWindow) { diff --git a/platform/platform-impl/src/com/intellij/openapi/project/FileContentQueue.java b/platform/platform-impl/src/com/intellij/openapi/project/FileContentQueue.java index bc626fb6f19f..185842a576e5 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/FileContentQueue.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/FileContentQueue.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.vfs.InvalidVirtualFileAccessException; +import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -55,9 +56,7 @@ public class FileContentQueue { public void run() { try { for (VirtualFile file : files) { - if (indicator != null) { - indicator.checkCanceled(); - } + indicator.checkCanceled(); addLast(file, indicator); } @@ -87,7 +86,7 @@ public class FileContentQueue { private void addLast(VirtualFile file, @NotNull final ProgressIndicator indicator) throws InterruptedException { FileContent content = new FileContent(file); - if (file.isValid() && !file.isDirectory()) { + if (file.isValid() && !file.isDirectory() && !file.isSpecialFile() && !VfsUtilCore.isBrokenLink(file)) { if (!doLoadContent(content, indicator)) { content.setEmptyContent(); } @@ -99,6 +98,7 @@ public class FileContentQueue { myQueue.put(content); } + @SuppressWarnings("InstanceofCatchParameter") private boolean doLoadContent(final FileContent content, @NotNull final ProgressIndicator indicator) throws InterruptedException { final long contentLength = content.getLength(); @@ -124,10 +124,16 @@ public class FileContentQueue { notifyAll(); } } - if (e instanceof ProcessCanceledException) throw (ProcessCanceledException)e; - if (e instanceof InterruptedException) throw (InterruptedException)e; - if (e instanceof IOException || e instanceof InvalidVirtualFileAccessException) LOG.info(e); + if (e instanceof ProcessCanceledException) { + throw (ProcessCanceledException)e; + } + else if (e instanceof InterruptedException) { + throw (InterruptedException)e; + } + else if (e instanceof IOException || e instanceof InvalidVirtualFileAccessException) { + LOG.info(e); + } else if (ApplicationManager.getApplication().isUnitTestMode()) { e.printStackTrace(); }