use of com.jetbrains.cidr.lang.psi.OCStruct 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;
}
use of com.jetbrains.cidr.lang.psi.OCStruct 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;
}
Aggregations