use of com.jetbrains.cidr.lang.psi.OCFile in project intellij by bazelbuild.
the class IwyuPragmasTest method noPragmas.
@Test
public void noPragmas() {
OCFile file = createFile("bar.cc", "#include \"bar.h\"", "", "#include <memory>", "#include <vector>", "", "#include \"f/foo1.h\"", "#include \"f/foo2.h\"", "#include \"f/foo3.h\" // blah", "", "void bar() {}");
IwyuPragmas pragmas = IwyuPragmas.parse(file);
assertThat(pragmas.privatePragma.isPresent()).isFalse();
assertThat(pragmas.keeps).isEmpty();
assertThat(pragmas.exports).isEmpty();
assertThat(pragmas.associatedHeader.isPresent()).isFalse();
}
use of com.jetbrains.cidr.lang.psi.OCFile in project intellij by bazelbuild.
the class DelegatingSwitchToHeaderOrSourceProvider method getItems.
@Override
public List<? extends GotoRelatedItem> getItems(DataContext context) {
Project project = CommonDataKeys.PROJECT.getData(context);
PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(context);
if (!(psiFile instanceof OCFile) || project == null || !Blaze.isBlazeProject(project)) {
return ImmutableList.of();
}
Optional<List<? extends GotoRelatedItem>> fromDelegate = getItemsWithTimeout(() -> DELEGATE.getItems(context));
if (!fromDelegate.isPresent()) {
logger.info("Timed out: Trying fallback for .h <-> .cc");
return getItemsFallback((OCFile) psiFile);
}
return fromDelegate.get();
}
use of com.jetbrains.cidr.lang.psi.OCFile 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;
}
use of com.jetbrains.cidr.lang.psi.OCFile in project intellij by bazelbuild.
the class BlazeCppAutoImportHelperTest method resolve.
private void resolve(ProjectView projectView, TargetMap targetMap, OCFile... files) {
BlazeProjectData blazeProjectData = projectDataBuilder().setTargetMap(targetMap).build();
registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(blazeProjectData));
BlazeCWorkspace.getInstance(getProject()).update(new BlazeContext(), workspaceRoot, ProjectViewSet.builder().add(projectView).build(), blazeProjectData);
for (OCFile file : files) {
resetFileSymbols(file);
}
FileSymbolTablesCache.getInstance(getProject()).ensurePendingFilesProcessed();
}
use of com.jetbrains.cidr.lang.psi.OCFile in project intellij by bazelbuild.
the class BlazeCppAutoImportHelperTest method stlPathsUnderWorkspaceRoot_importStlHeader.
@Test
public void stlPathsUnderWorkspaceRoot_importStlHeader() {
ProjectView projectView = projectView(directories("foo/bar"), targets("//foo/bar:bar"));
TargetMap targetMap = TargetMapBuilder.builder().addTarget(createCcToolchain()).addTarget(createCcTarget("//foo/bar:bar", Kind.CC_LIBRARY, sources("foo/bar/bar.cc"), sources())).addTarget(createCcTarget("//third_party/stl:stl", Kind.CC_LIBRARY, sources(), sources("third_party/stl/vector.h"))).build();
// Normally this is <vector> without .h, but we need to trick the file type detector into
// realizing that this is an OCFile.
OCFile header = createFile("third_party/stl/vector.h", "namespace std {", "template<typename T> class vector {};", "}");
OCFile file = createFile("foo/bar/bar.cc", "std::vector<int> my_vector;");
resolve(projectView, targetMap, file, header);
testFixture.openFileInEditor(file.getVirtualFile());
OCReferenceElement referenceElement = testFixture.findElementByText("std::vector<int>", OCReferenceElement.class);
OCImportSymbolFix fix = new OCImportSymbolFix(referenceElement);
assertThat(fix.isAvailable(getProject(), testFixture.getEditor(), file)).isTrue();
assertThat(fix.getAutoImportItems()).hasSize(1);
assertThat(fix.getAutoImportItems().get(0).getTitleAndLocation().getFirst()).isEqualTo("class 'std::vector'");
assertThat(fix.getAutoImportItems().get(0).getTitleAndLocation().getSecond()).isEqualTo("<vector.h>");
}
Aggregations