Eclipse插件开发人员:如何获取当前所选项目的路径?
时间:2020-03-05 18:45:54 来源:igfitidea点击:
我正在编写一个插件,它将解析项目中的一堆文件。但是目前,我仍在搜索Eclipse API中寻找答案。
插件的工作方式如下:每当我打开源文件时,我都会让插件解析源的相应构建文件(可以通过缓存解析结果来进一步开发)。获取文件非常简单:
public void showSelection(IWorkbenchPart sourcePart) { // Gets the currently selected file from the editor IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor() .getEditorInput().getAdapter(IFile.class); if (file != null) { String path = file.getProjectRelativePath(); /** Snipped out: Rip out the source path part * and replace with build path * Then parse it. */ } }
我的问题是我必须对源文件和构建文件所在的路径使用硬编码的字符串。有人知道如何从Eclipse检索构建路径吗? (顺便说一下,我正在CDT工作)。还有一种简单的方法可以确定源文件的源路径(例如,一个文件位于" src"目录下)吗?
解决方案
回答
我们应该看一下ICProject,尤其是getOutputEntries和getAllSourceRoots操作。本教程也有一些简短的示例。我与JDT合作,因此我几乎可以做。希望能帮助到你 :)