correctly update index on file copy

This commit is contained in:
peter
2016-11-09 10:32:38 +01:00
parent 3b2e08696c
commit 944b6834a1
2 changed files with 28 additions and 3 deletions
@@ -16,6 +16,7 @@
package com.intellij.index
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.command.impl.CurrentEditorProvider
import com.intellij.openapi.command.impl.UndoManagerImpl
@@ -25,6 +26,7 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileTypes.PlainTextFileType
import com.intellij.openapi.module.StdModuleTypes
import com.intellij.openapi.roots.ContentIterator
import com.intellij.openapi.util.Ref
import com.intellij.openapi.util.io.FileUtil
@@ -39,6 +41,7 @@ import com.intellij.psi.impl.cache.impl.id.IdIndex
import com.intellij.psi.impl.cache.impl.id.IdIndexEntry
import com.intellij.psi.impl.file.impl.FileManagerImpl
import com.intellij.psi.impl.java.stubs.index.JavaStubIndexKeys
import com.intellij.psi.impl.source.JavaFileElementType
import com.intellij.psi.impl.source.PostprocessReformattingAspect
import com.intellij.psi.impl.source.PsiFileWithStubSupport
import com.intellij.psi.search.EverythingGlobalScope
@@ -50,6 +53,7 @@ import com.intellij.psi.stubs.StubIndexImpl
import com.intellij.psi.stubs.StubUpdatingIndex
import com.intellij.testFramework.IdeaTestUtil
import com.intellij.testFramework.PlatformTestUtil
import com.intellij.testFramework.PsiTestUtil
import com.intellij.testFramework.SkipSlowTestLocally
import com.intellij.testFramework.exceptionCases.IllegalArgumentExceptionCase
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase
@@ -615,4 +619,21 @@ class IndexTest extends JavaCodeInsightFixtureTestCase {
StringUtil.replace(operation.get(), file.getPath(), file.getName());
}
void "test files inside copied directory are indexed"() {
def facade = JavaPsiFacade.getInstance(project)
def srcFile = myFixture.addFileToProject('foo/bar/A.java', 'class A {}')
assert facade.findClass('A', GlobalSearchScope.moduleScope(myModule)) != null
def anotherDir = myFixture.tempDirFixture.findOrCreateDir('another')
def anotherModule = PsiTestUtil.addModule(project, StdModuleTypes.JAVA, 'another', anotherDir)
assert !facade.findClass('A', GlobalSearchScope.moduleScope(anotherModule))
WriteAction.run { srcFile.virtualFile.parent.copy(this, anotherDir, 'doo') }
assert facade.findClass('A', GlobalSearchScope.moduleScope(anotherModule)) != null
assert JavaFileElementType.isInSourceContent(myFixture.tempDirFixture.getFile('another/doo/A.java'))
}
}
@@ -39,6 +39,7 @@ import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.events.VFileCopyEvent;
import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent;
import com.intellij.openapi.vfs.newvfs.events.VFileEvent;
import com.intellij.openapi.vfs.newvfs.events.VFileMoveEvent;
@@ -88,7 +89,10 @@ public class PushedFilePropertiesUpdaterImpl extends PushedFilePropertiesUpdater
boolean pushedSomething = false;
List<Runnable> delayedTasks = ContainerUtil.newArrayList();
for (VFileEvent event : events) {
final VirtualFile file = event.getFile();
VirtualFile file = event.getFile();
if (event instanceof VFileCopyEvent) {
file = ((VFileCopyEvent)event).getNewParent().findChild(((VFileCopyEvent)event).getNewChildName());
}
if (file == null) continue;
final FilePropertyPusher[] pushers = file.isDirectory() ? myPushers : myFilePushers;
@@ -104,8 +108,8 @@ public class PushedFilePropertiesUpdaterImpl extends PushedFilePropertiesUpdater
else if (!ProjectUtil.isProjectOrWorkspaceFile(file)) {
ContainerUtil.addIfNotNull(delayedTasks, createRecursivePushTask(file, pushers));
}
} else if (event instanceof VFileMoveEvent) {
for (FilePropertyPusher pusher : pushers) {
} else if (event instanceof VFileMoveEvent || event instanceof VFileCopyEvent) {
for (FilePropertyPusher<?> pusher : pushers) {
file.putUserData(pusher.getFileDataKey(), null);
}
// push synchronously to avoid entering dumb mode in the middle of a meaningful write action