From 3b29b329036236a059085081df72821433775f71 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 17 Apr 2012 14:58:06 +0400 Subject: [PATCH] Remove analyzeProjectWithCache with caching on project. It's used nowhere, gives cycles in resolve and produce second copy of binding context. KT-1792 UI freeze and AssertionError in log #KT-1792 fixed --- .../jet/plugin/caches/JetShortNamesCache.java | 4 +- .../project/AnalyzerFacadeWithCache.java | 44 ------------------- .../plugin/quickfix/ImportClassAndFunFix.java | 15 ++++--- .../{FirstFile.kt => RenameKotlinClass.kt} | 0 .../{FirstFile.kt => RenameKotlinClass.kt} | 0 .../rename/RenameInKotlinTest.java | 27 +++++++++--- 6 files changed, 31 insertions(+), 59 deletions(-) rename idea/testData/refactoring/rename/renameKotlinClass/after/{FirstFile.kt => RenameKotlinClass.kt} (100%) rename idea/testData/refactoring/rename/renameKotlinClass/before/{FirstFile.kt => RenameKotlinClass.kt} (100%) diff --git a/idea/src/org/jetbrains/jet/plugin/caches/JetShortNamesCache.java b/idea/src/org/jetbrains/jet/plugin/caches/JetShortNamesCache.java index c96b6146d27..4b572148207 100644 --- a/idea/src/org/jetbrains/jet/plugin/caches/JetShortNamesCache.java +++ b/idea/src/org/jetbrains/jet/plugin/caches/JetShortNamesCache.java @@ -129,8 +129,8 @@ public class JetShortNamesCache extends PsiShortNamesCache { } @NotNull - public Collection getFQNamesByName(@NotNull final String name, @NotNull GlobalSearchScope scope) { - BindingContext context = WholeProjectAnalyzerFacade.analyzeProjectWithCache(project, scope).getBindingContext(); + public Collection getFQNamesByName(@NotNull final String name, JetFile file, @NotNull GlobalSearchScope scope) { + BindingContext context = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file).getBindingContext(); return Collections2.filter(context.getKeys(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR), new Predicate() { @Override public boolean apply(@Nullable FqName fqName) { diff --git a/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java b/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java index 3a8458faf4b..c5ce3144984 100644 --- a/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java +++ b/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java @@ -19,7 +19,6 @@ package org.jetbrains.jet.plugin.project; import com.google.common.base.Predicates; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; import com.intellij.psi.PsiFile; import com.intellij.psi.util.CachedValue; @@ -111,47 +110,4 @@ public final class AnalyzerFacadeWithCache { DiagnosticUtils.throwIfRunningOnServer(e); LOG.error(e); } - - /** - * Analyze project with string cache for the whole project. All given files will be analyzed only for descriptors. - */ - @NotNull - public static AnalyzeExhaust analyzeProjectWithCache(@NotNull final Project project, @NotNull final Collection files) { - // Need lock for getValue(), because parallel threads can start evaluation of compute() simultaneously - synchronized (lock) { - CachedValue bindingContextCachedValue = project.getUserData(ANALYZE_EXHAUST); - if (bindingContextCachedValue == null) { - bindingContextCachedValue = - CachedValuesManager.getManager(project).createCachedValue(new CachedValueProvider() { - @Override - public Result compute() { - try { - AnalyzeExhaust analyzeExhaust = AnalyzerFacadeProvider.getAnalyzerFacadeForProject(project) - .analyzeFiles(project, - files, - Predicates.alwaysFalse(), - JetControlFlowDataTraceFactory.EMPTY); - return new Result(analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT); - } - catch (ProcessCanceledException e) { - throw e; - } - catch (Throwable e) { - handleError(e); - return emptyExhaust(); - } - } - - @NotNull - private Result emptyExhaust() { - BindingTraceContext bindingTraceContext = new BindingTraceContext(); - AnalyzeExhaust analyzeExhaust = new AnalyzeExhaust(bindingTraceContext.getBindingContext(), null); - return new Result(analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT); - } - }, false); - project.putUserData(ANALYZE_EXHAUST, bindingContextCachedValue); - } - return bindingContextCachedValue.getValue(); - } - } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassAndFunFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassAndFunFix.java index 754c905da97..8217ee8c1c5 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassAndFunFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportClassAndFunFix.java @@ -53,7 +53,10 @@ import org.jetbrains.jet.plugin.actions.JetAddImportAction; import org.jetbrains.jet.plugin.caches.JetCacheManager; import org.jetbrains.jet.plugin.caches.JetShortNamesCache; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Set; /** * Check possibility and perform fix for unresolved references. @@ -85,7 +88,7 @@ public class ImportClassAndFunFix extends JetHintAction assert referenceName != null; List result = Lists.newArrayList(); - result.addAll(getClassNames(referenceName, file.getProject())); + result.addAll(getClassNames(referenceName, (JetFile) file)); result.addAll(getJetTopLevelFunctions(referenceName, element, file.getProject())); result.addAll(getJetExtensionFunctions(referenceName, element, file.getProject())); @@ -142,11 +145,11 @@ public class ImportClassAndFunFix extends JetHintAction /* * Searches for possible class names in kotlin context and java facade. */ - public static Collection getClassNames(@NotNull String referenceName, @NotNull Project project) { - final GlobalSearchScope scope = GlobalSearchScope.allScope(project); + public static Collection getClassNames(@NotNull String referenceName, @NotNull JetFile file) { + final GlobalSearchScope scope = GlobalSearchScope.allScope(file.getProject()); Set possibleResolveNames = Sets.newHashSet(); - possibleResolveNames.addAll(JetCacheManager.getInstance(project).getNamesCache().getFQNamesByName(referenceName, scope)); - possibleResolveNames.addAll(getJavaClasses(referenceName, project, scope)); + possibleResolveNames.addAll(JetCacheManager.getInstance(file.getProject()).getNamesCache().getFQNamesByName(referenceName, file, scope)); + possibleResolveNames.addAll(getJavaClasses(referenceName, file.getProject(), scope)); // TODO: Do appropriate sorting return Lists.newArrayList(possibleResolveNames); diff --git a/idea/testData/refactoring/rename/renameKotlinClass/after/FirstFile.kt b/idea/testData/refactoring/rename/renameKotlinClass/after/RenameKotlinClass.kt similarity index 100% rename from idea/testData/refactoring/rename/renameKotlinClass/after/FirstFile.kt rename to idea/testData/refactoring/rename/renameKotlinClass/after/RenameKotlinClass.kt diff --git a/idea/testData/refactoring/rename/renameKotlinClass/before/FirstFile.kt b/idea/testData/refactoring/rename/renameKotlinClass/before/RenameKotlinClass.kt similarity index 100% rename from idea/testData/refactoring/rename/renameKotlinClass/before/FirstFile.kt rename to idea/testData/refactoring/rename/renameKotlinClass/before/RenameKotlinClass.kt diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInKotlinTest.java b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInKotlinTest.java index f3fcdc138f7..0c103c5da72 100644 --- a/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInKotlinTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/refactoring/rename/RenameInKotlinTest.java @@ -16,18 +16,19 @@ package org.jetbrains.jet.plugin.refactoring.rename; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileManager; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; -import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.PsiFile; import com.intellij.refactoring.MultiFileTestCase; import com.intellij.refactoring.rename.RenameProcessor; import org.jetbrains.annotations.NonNls; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; -import org.jetbrains.jet.lang.psi.JetClassOrObject; +import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.FqName; @@ -64,9 +65,23 @@ public class RenameInKotlinTest extends MultiFileTestCase { doTest(new PerformAction() { @Override public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception { - BindingContext bindingContext = WholeProjectAnalyzerFacade.analyzeProjectWithCache( - getProject(), GlobalSearchScope.allScope(getProject())) - .getBindingContext(); + VirtualFile child = rootDir.findChild(getTestName(false) + ".kt"); + if (child == null) { + return; + } + + Document document = FileDocumentManager.getInstance().getDocument(child); + if (document == null) { + return; + } + + PsiFile file = PsiDocumentManager.getInstance(getProject()).getPsiFile(document); + if (!(file instanceof JetFile)) { + return; + } + + BindingContext bindingContext = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile((JetFile)file) + .getBindingContext(); ClassDescriptor classDescriptor = bindingContext.get(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, qClassName); assertNotNull(classDescriptor); @@ -79,8 +94,6 @@ public class RenameInKotlinTest extends MultiFileTestCase { PsiDocumentManager.getInstance(myProject).commitAllDocuments(); FileDocumentManager.getInstance().saveAllDocuments(); VirtualFileManager.getInstance().refresh(false); - - } }); }