gpt4 book ai didi

eclipse-cdt - 使用 CDT 从现有 eclipse 项目中获取源文件列表

转载 作者:行者123 更新时间:2023-12-01 01:15:15 25 4
gpt4 key购买 nike

我正在开发 CDT eclipse 插件,我正在尝试使用以下代码使用 CDT 代码获取 eclipse 项目资源管理器中存在的源文件列表,结果为空。

案例1:

IFile[] files2 = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(new URI("file:/"+workingDirectory));
for (IFile file : files2) {
System.out.println("fullpath " +file.getFullPath());
}

案例2:
IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(getProject().getRawLocationURI());
for (IFile file : files) {
System.out.println("fullpath " +file.getFullPath());
}

案例3:
IFile[] files3 = ResourceLookup.findFilesByName(getProject().getFullPath(),ResourcesPlugin.getWorkspace().getRoot().getProjects(),false);
for (IFile file : files3) {
System.out.println("fullpath " +file.getFullPath());
}

案例4:
IFolder srcFolder = project.getFolder("src");

案例 1 ,2,3 给我输出 null,我期待文件列表;
在案例 4 中:我正在获取“helloworld/src”文件列表,但我希望从现有项目中获取文件意味着主根目录,例如:“helloworld”
请给我建议。

最佳答案

您可以使用 IResourceVisitor 浏览 worspace 资源树 - 或者您可以浏览 CDT 模型:

private void findSourceFiles(final IProject project) {
final ICProject cproject = CoreModel.getDefault().create(project);
if (cproject != null) {
try {
cproject.accept(new ICElementVisitor() {

@Override
public boolean visit(final ICElement element) throws CoreException {
if (element.getElementType() == ICElement.C_UNIT) {
ITranslationUnit unit = (ITranslationUnit) element;
if (unit.isSourceUnit()) {
System.out.printf("%s, %s, %s\n", element.getElementName(), element.getClass(), element
.getUnderlyingResource().getFullPath());
}
return false;
} else {
return true;
}
}
});
} catch (final CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

请注意,源文件可能比您实际想要的要多(例如,您可能不关心系统头文件)——您可以通过检查底层资源是什么来过滤它们。

关于eclipse-cdt - 使用 CDT 从现有 eclipse 项目中获取源文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12421787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com