Search in sources :

Example 1 with OCMacroCall

use of com.jetbrains.cidr.lang.psi.OCMacroCall in project intellij by bazelbuild.

the class BlazeCppTestLocator method findPsiElement.

@Nullable
private static PsiElement findPsiElement(Project project, @Nullable String instantiation, @Nullable String suite, @Nullable String method) {
    if (suite == null) {
        return null;
    }
    OCSymbol<?> symbol;
    if (method != null) {
        symbol = CidrGoogleTestUtil.findGoogleTestSymbol(project, suite, method);
    } else if (instantiation != null) {
        symbol = CidrGoogleTestUtil.findGoogleTestInstantiationSymbol(project, suite, instantiation);
    } else {
        symbol = findSuiteSymbol(project, suite);
    }
    if (symbol == null) {
        return null;
    }
    PsiElement psi = symbol.locateDefinition();
    while (!(psi instanceof OCStruct || psi instanceof OCMacroCall) && psi != null) {
        PsiElement prev = psi.getPrevSibling();
        psi = prev == null ? psi.getParent() : prev;
    }
    return psi;
}
Also used : OCStruct(com.jetbrains.cidr.lang.psi.OCStruct) OCMacroCall(com.jetbrains.cidr.lang.psi.OCMacroCall) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

Example 2 with OCMacroCall

use of com.jetbrains.cidr.lang.psi.OCMacroCall in project intellij by bazelbuild.

the class GoogleTestLocation method findGoogleTest.

@Nullable
public static GoogleTestLocation findGoogleTest(PsiElement element) {
    // Copied from on CidrGoogleTestRunConfigurationProducer::findTestObject.
    // Precedence order (decreasing): class/function, macro, file
    PsiElement parent = PsiTreeUtil.getNonStrictParentOfType(element, OCFunctionDefinition.class, OCStruct.class);
    OCStructSymbol parentSymbol;
    if (parent instanceof OCStruct && ((parentSymbol = ((OCStruct) parent).getSymbol()) != null) && CidrGoogleTestUtil.isGoogleTestClass(parentSymbol)) {
        Couple<String> name = CidrGoogleTestUtil.extractGoogleTestName(parentSymbol);
        if (name != null) {
            return createFromClassAndMethod(parent, name.first, name.second);
        }
        String className = parentSymbol.getQualifiedName().getName();
        return createFromClass(parent, className);
    } else if (parent instanceof OCFunctionDefinition) {
        OCFunctionSymbol symbol = ((OCFunctionDefinition) parent).getSymbol();
        if (symbol != null) {
            OCSymbolWithQualifiedName<?> resolvedOwner = symbol.getResolvedOwner();
            if (resolvedOwner != null) {
                OCSymbol<?> owner = resolvedOwner.getDefinitionSymbol();
                if (owner instanceof OCStructSymbol && CidrGoogleTestUtil.isGoogleTestClass((OCStructSymbol) owner)) {
                    OCStruct struct = (OCStruct) owner.locateDefinition();
                    Couple<String> name = CidrGoogleTestUtil.extractGoogleTestName((OCStructSymbol) owner);
                    if (name != null) {
                        return createFromClassAndMethod(struct, name.first, name.second);
                    }
                    return createFromClass(struct, ((OCStructSymbol) owner).getQualifiedName().getName());
                }
            }
        }
    }
    // if we're still here, let's test for a macro and, as a last resort, a file.
    parent = PsiTreeUtil.getNonStrictParentOfType(element, OCMacroCall.class, OCFile.class);
    if (parent instanceof OCMacroCall) {
        OCMacroCall gtestMacro = CidrGoogleTestUtil.findGoogleTestMacros(parent);
        if (gtestMacro != null) {
            List<OCMacroCallArgument> arguments = gtestMacro.getArguments();
            if (arguments.size() >= 2) {
                OCMacroCallArgument suiteArg = arguments.get(0);
                OCMacroCallArgument testArg = arguments.get(1);
                // if the element is the first argument of macro call,
                // then running entire suite, otherwise only a current test
                boolean isSuite = isFirstArgument(PsiTreeUtil.getParentOfType(element, OCMacroCallArgument.class)) || isFirstArgument(element.getPrevSibling());
                String suiteName = CidrGoogleTestUtil.extractArgumentValue(suiteArg);
                String testName = CidrGoogleTestUtil.extractArgumentValue(testArg);
                OCStructSymbol symbol = CidrGoogleTestUtil.findGoogleTestSymbol(element.getProject(), suiteName, testName);
                if (symbol != null) {
                    OCStruct targetElement = (OCStruct) symbol.locateDefinition();
                    return createFromClassAndMethod(targetElement, suiteName, isSuite ? null : testName);
                }
            }
        }
        Couple<String> suite = CidrGoogleTestUtil.extractFullSuiteNameFromMacro(parent);
        if (suite != null) {
            Collection<OCStructSymbol> res = CidrGoogleTestUtil.findGoogleTestSymbolsForSuiteRandomly(element.getProject(), suite.first, true);
            if (res.size() != 0) {
                OCStruct struct = (OCStruct) res.iterator().next().locateDefinition();
                GoogleTestSpecification gtest = new GoogleTestSpecification.FromPsiElement(suite.first, null, suite.second, null);
                return new GoogleTestLocation(struct, gtest);
            }
        }
    } else if (parent instanceof OCFile) {
        return createFromFile(parent);
    }
    return null;
}
Also used : OCFunctionSymbol(com.jetbrains.cidr.lang.symbols.cpp.OCFunctionSymbol) OCFunctionDefinition(com.jetbrains.cidr.lang.psi.OCFunctionDefinition) Couple(com.intellij.openapi.util.Couple) OCSymbol(com.jetbrains.cidr.lang.symbols.OCSymbol) OCFile(com.jetbrains.cidr.lang.psi.OCFile) OCStruct(com.jetbrains.cidr.lang.psi.OCStruct) OCMacroCallArgument(com.jetbrains.cidr.lang.psi.OCMacroCallArgument) OCMacroCall(com.jetbrains.cidr.lang.psi.OCMacroCall) OCStructSymbol(com.jetbrains.cidr.lang.symbols.cpp.OCStructSymbol) OCSymbolWithQualifiedName(com.jetbrains.cidr.lang.symbols.cpp.OCSymbolWithQualifiedName) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 OCMacroCall (com.jetbrains.cidr.lang.psi.OCMacroCall)2 OCStruct (com.jetbrains.cidr.lang.psi.OCStruct)2 Nullable (javax.annotation.Nullable)2 Couple (com.intellij.openapi.util.Couple)1 OCFile (com.jetbrains.cidr.lang.psi.OCFile)1 OCFunctionDefinition (com.jetbrains.cidr.lang.psi.OCFunctionDefinition)1 OCMacroCallArgument (com.jetbrains.cidr.lang.psi.OCMacroCallArgument)1 OCSymbol (com.jetbrains.cidr.lang.symbols.OCSymbol)1 OCFunctionSymbol (com.jetbrains.cidr.lang.symbols.cpp.OCFunctionSymbol)1 OCStructSymbol (com.jetbrains.cidr.lang.symbols.cpp.OCStructSymbol)1 OCSymbolWithQualifiedName (com.jetbrains.cidr.lang.symbols.cpp.OCSymbolWithQualifiedName)1