Search in sources :

Example 61 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class PyAbstractTestProcessRunner method assertAllTestsAreResolved.

/**
   * Ensures all test locations are resolved (i.e. user may click on test and navigate to it)
   * All tests are checked but [root] (it never resolves).
   */
public final void assertAllTestsAreResolved(@NotNull final Project project) {
    final List<SMTestProxy> allTests = getTestProxy().getAllTests();
    assert !allTests.isEmpty() : "No tests at all.";
    final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
    EdtTestUtil.runInEdtAndWait(() -> allTests.subList(1, allTests.size()).forEach(t -> Assert.assertNotNull("No location " + t, t.getLocation(project, scope))));
}
Also used : ExecutionException(com.intellij.execution.ExecutionException) EdtTestUtil(com.intellij.testFramework.EdtTestUtil) ModalityState(com.intellij.openapi.application.ModalityState) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) AbstractPythonRunConfigurationParams(com.jetbrains.python.run.AbstractPythonRunConfigurationParams) Filter(com.intellij.execution.testframework.Filter) RerunFailedActionsTestTools(com.intellij.execution.testframework.actions.RerunFailedActionsTestTools) Project(com.intellij.openapi.project.Project) SMTestProxy(com.intellij.execution.testframework.sm.runner.SMTestProxy) Logger(com.intellij.openapi.diagnostic.Logger) SMRootTestProxy(com.intellij.execution.testframework.sm.runner.SMTestProxy.SMRootTestProxy) ProgramRunner(com.intellij.execution.runners.ProgramRunner) ConsoleViewImpl(com.intellij.execution.impl.ConsoleViewImpl) StringUtil(com.intellij.openapi.util.text.StringUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) AbstractTestProxy(com.intellij.execution.testframework.AbstractTestProxy) ConfigurationFactory(com.intellij.execution.configurations.ConfigurationFactory) ProcessHandler(com.intellij.execution.process.ProcessHandler) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) SMTRunnerConsoleView(com.intellij.execution.testframework.sm.runner.ui.SMTRunnerConsoleView) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) Ref(com.intellij.openapi.util.Ref) javax.swing(javax.swing) SMTestProxy(com.intellij.execution.testframework.sm.runner.SMTestProxy) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 62 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class CompleteReferenceExpression method getVariantsFromQualifier.

private void getVariantsFromQualifier(@NotNull GrExpression qualifier) {
    Project project = qualifier.getProject();
    final PsiType qualifierType = TypesUtil.boxPrimitiveType(qualifier.getType(), qualifier.getManager(), qualifier.getResolveScope());
    final ResolveState state = ResolveState.initial();
    if (qualifierType == null || PsiType.VOID.equals(qualifierType)) {
        if (qualifier instanceof GrReferenceExpression) {
            PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
            if (resolved instanceof PsiPackage || resolved instanceof PsiVariable) {
                resolved.processDeclarations(myProcessor, state, null, myRefExpr);
                return;
            }
        }
        getVariantsFromQualifierType(TypesUtil.getJavaLangObject(qualifier), project);
    } else if (qualifierType instanceof PsiIntersectionType) {
        for (PsiType conjunct : ((PsiIntersectionType) qualifierType).getConjuncts()) {
            getVariantsFromQualifierType(conjunct, project);
        }
    } else if (qualifierType instanceof GrTraitType) {
        // Process trait type conjuncts in reversed order because last applied trait matters.
        PsiType[] conjuncts = ((GrTraitType) qualifierType).getConjuncts();
        for (int i = conjuncts.length - 1; i >= 0; i--) {
            getVariantsFromQualifierType(conjuncts[i], project);
        }
    } else {
        getVariantsFromQualifierType(qualifierType, project);
        if (qualifier instanceof GrReferenceExpression && !PsiUtil.isSuperReference(qualifier) && !PsiUtil.isInstanceThisRef(qualifier)) {
            PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
            if (resolved instanceof PsiClass) {
                ////omitted .class
                GlobalSearchScope scope = myRefExpr.getResolveScope();
                PsiClass javaLangClass = PsiUtil.getJavaLangClass(resolved, scope);
                if (javaLangClass != null) {
                    PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
                    PsiTypeParameter[] typeParameters = javaLangClass.getTypeParameters();
                    if (typeParameters.length == 1) {
                        substitutor = substitutor.put(typeParameters[0], qualifierType);
                    }
                    PsiType javaLangClassType = JavaPsiFacade.getElementFactory(myRefExpr.getProject()).createType(javaLangClass, substitutor);
                    ResolveUtil.processAllDeclarations(javaLangClassType, myProcessor, state, myRefExpr);
                }
            }
        }
    }
}
Also used : GrTraitType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTraitType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 63 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class MavenClasspathsAndSearchScopesTest method testLibraryScopeForTwoDependentModules.

public void testLibraryScopeForTwoDependentModules() throws IOException {
    VirtualFile m1 = createModulePom("m1", "<groupId>test</groupId>" + "<artifactId>m1</artifactId>" + "<version>1</version>" + "<dependencies>" + "  <dependency>" + "    <groupId>test</groupId>" + "    <artifactId>m2</artifactId>" + "    <version>1</version>" + "  </dependency>" + "</dependencies>");
    VirtualFile m2 = createModulePom("m2", "<groupId>test</groupId>" + "<artifactId>m2</artifactId>" + "<version>1</version>" + "    <dependencies>" + "        <dependency>" + "            <groupId>junit</groupId>" + "            <artifactId>junit</artifactId>" + "            <version>4.0</version>" + "            <scope>provided</scope>" + "        </dependency>" + "    </dependencies>");
    importProjects(m1, m2);
    assertModules("m1", "m2");
    Module m1m = ModuleManager.getInstance(myProject).findModuleByName("m1");
    List<OrderEntry> modules1 = new ArrayList<>();
    ModuleRootManager.getInstance(m1m).orderEntries().withoutSdk().withoutModuleSourceEntries().forEach(new CommonProcessors.CollectProcessor<>(modules1));
    GlobalSearchScope scope1 = LibraryScopeCache.getInstance(myProject).getLibraryScope(modules1);
    assertSearchScope(scope1, getProjectPath() + "/m1/src/main/java", getProjectPath() + "/m1/src/test/java", getProjectPath() + "/m2/src/main/java", getProjectPath() + "/m2/src/test/java");
    String libraryPath = getRepositoryPath() + "/junit/junit/4.0/junit-4.0.jar";
    String librarySrcPath = getRepositoryPath() + "/junit/junit/4.0/junit-4.0-sources.jar";
    Module m2m = ModuleManager.getInstance(myProject).findModuleByName("m2");
    List<OrderEntry> modules2 = new ArrayList<>();
    ModuleRootManager.getInstance(m2m).orderEntries().withoutSdk().withoutModuleSourceEntries().forEach(new CommonProcessors.CollectProcessor<>(modules2));
    GlobalSearchScope scope2 = LibraryScopeCache.getInstance(myProject).getLibraryScope(modules2);
    List<String> expectedPaths = ContainerUtil.newArrayList(getProjectPath() + "/m2/src/main/java", getProjectPath() + "/m2/src/test/java", libraryPath);
    if (new File(librarySrcPath).exists()) {
        expectedPaths.add(librarySrcPath);
    }
    assertSearchScope(scope2, ArrayUtil.toStringArray(expectedPaths));
}
Also used : DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) Module(com.intellij.openapi.module.Module) File(java.io.File)

Example 64 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project kotlin by JetBrains.

the class AbstractLoadJavaTest method doTestJavaAgainstKotlin.

protected void doTestJavaAgainstKotlin(String expectedFileName) throws Exception {
    File expectedFile = new File(expectedFileName);
    File sourcesDir = new File(expectedFileName.replaceFirst("\\.txt$", ""));
    FileUtil.copyDir(sourcesDir, new File(tmpdir, "test"), new FileFilter() {

        @Override
        public boolean accept(@NotNull File pathname) {
            return pathname.getName().endsWith(".java");
        }
    });
    CompilerConfiguration configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, getJdkKind());
    ContentRootsKt.addKotlinSourceRoot(configuration, sourcesDir.getAbsolutePath());
    JvmContentRootsKt.addJavaSourceRoot(configuration, new File("compiler/testData/loadJava/include"));
    JvmContentRootsKt.addJavaSourceRoot(configuration, tmpdir);
    final KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForTests(getTestRootDisposable(), configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
    AnalysisResult result = TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(environment.getProject(), environment.getSourceFiles(), new CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace(), configuration, new Function1<GlobalSearchScope, PackagePartProvider>() {

        @Override
        public PackagePartProvider invoke(GlobalSearchScope scope) {
            return new JvmPackagePartProvider(environment, scope);
        }
    });
    PackageViewDescriptor packageView = result.getModuleDescriptor().getPackage(TEST_PACKAGE_FQNAME);
    checkJavaPackage(expectedFile, packageView, result.getBindingContext(), COMPARATOR_CONFIGURATION);
}
Also used : CliLightClassGenerationSupport(org.jetbrains.kotlin.cli.jvm.compiler.CliLightClassGenerationSupport) AnalysisResult(org.jetbrains.kotlin.analyzer.AnalysisResult) KotlinCoreEnvironment(org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) JvmPackagePartProvider(org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider) CompilerConfiguration(org.jetbrains.kotlin.config.CompilerConfiguration) JvmPackagePartProvider(org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider) FileFilter(java.io.FileFilter) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Example 65 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class DefaultSourcePositionProvider method getSourcePositionForField.

@Nullable
private static SourcePosition getSourcePositionForField(@NotNull FieldDescriptor descriptor, @NotNull Project project, @NotNull DebuggerContextImpl context, boolean nearest) {
    final ReferenceType type = descriptor.getField().declaringType();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final String fieldName = descriptor.getField().name();
    if (fieldName.startsWith(FieldDescriptorImpl.OUTER_LOCAL_VAR_FIELD_PREFIX)) {
        // this field actually mirrors a local variable in the outer class
        String varName = fieldName.substring(fieldName.lastIndexOf('$') + 1);
        PsiElement element = PositionUtil.getContextElement(context);
        if (element == null) {
            return null;
        }
        PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class, false);
        if (aClass == null) {
            return null;
        }
        PsiElement navigationElement = aClass.getNavigationElement();
        if (!(navigationElement instanceof PsiClass)) {
            return null;
        }
        aClass = (PsiClass) navigationElement;
        PsiVariable psiVariable = facade.getResolveHelper().resolveReferencedVariable(varName, aClass);
        if (psiVariable == null) {
            return null;
        }
        if (nearest) {
            return DebuggerContextUtil.findNearest(context, psiVariable, aClass.getContainingFile());
        }
        return SourcePosition.createFromElement(psiVariable);
    } else {
        final DebuggerSession session = context.getDebuggerSession();
        final GlobalSearchScope scope = session != null ? session.getSearchScope() : GlobalSearchScope.allScope(project);
        PsiClass aClass = facade.findClass(type.name().replace('$', '.'), scope);
        if (aClass == null) {
            // trying to search, assuming declaring class is an anonymous class
            final DebugProcessImpl debugProcess = context.getDebugProcess();
            if (debugProcess != null) {
                try {
                    final List<Location> locations = type.allLineLocations();
                    if (!locations.isEmpty()) {
                        // important: use the last location to be sure the position will be within the anonymous class
                        final Location lastLocation = locations.get(locations.size() - 1);
                        final SourcePosition position = debugProcess.getPositionManager().getSourcePosition(lastLocation);
                        aClass = JVMNameUtil.getClassAt(position);
                    }
                } catch (AbsentInformationException | ClassNotPreparedException ignored) {
                }
            }
        }
        if (aClass != null) {
            PsiField field = aClass.findFieldByName(fieldName, false);
            if (field == null)
                return null;
            if (nearest) {
                return DebuggerContextUtil.findNearest(context, field.getNavigationElement(), aClass.getContainingFile());
            }
            return SourcePosition.createFromElement(field);
        }
        return null;
    }
}
Also used : AbsentInformationException(com.sun.jdi.AbsentInformationException) ClassNotPreparedException(com.sun.jdi.ClassNotPreparedException) ReferenceType(com.sun.jdi.ReferenceType) DebuggerSession(com.intellij.debugger.impl.DebuggerSession) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SourcePosition(com.intellij.debugger.SourcePosition) Location(com.sun.jdi.Location) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)485 Project (com.intellij.openapi.project.Project)145 NotNull (org.jetbrains.annotations.NotNull)134 VirtualFile (com.intellij.openapi.vfs.VirtualFile)106 Module (com.intellij.openapi.module.Module)101 Nullable (org.jetbrains.annotations.Nullable)78 PsiClass (com.intellij.psi.PsiClass)53 ArrayList (java.util.ArrayList)37 PsiFile (com.intellij.psi.PsiFile)32 PsiElement (com.intellij.psi.PsiElement)31 THashSet (gnu.trove.THashSet)22 SearchScope (com.intellij.psi.search.SearchScope)19 PsiDirectory (com.intellij.psi.PsiDirectory)18 ContainerUtil (com.intellij.util.containers.ContainerUtil)18 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)17 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)17 com.intellij.psi (com.intellij.psi)17 List (java.util.List)17 StringUtil (com.intellij.openapi.util.text.StringUtil)15 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)15