Eclipse 插件开发:如何获取当前所选项目的路径?
我正在编写一个插件,它将解析项目中的一堆文件。 但目前我一直在通过 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”目录下)?
I'm writing a plugin that will parse a bunch of files in a project. But for the moment I'm stuck searching through the Eclipse API for answers.
The plugin works like this: Whenever I open a source file I let the plugin parse the source's corresponding build file (this could be further developed with caching the parse result). Getting the file is simple enough:
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. */
}
}
The problem I have is I have to use hard coded strings for the paths where the source files and build files go. Anyone know how to retrieve the build path from Eclipse? (I'm working in CDT by the way). Also is there a simple way to determine what the source path is (e.g. one file is under the "src" directory) of a source file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该看看 ICProject,特别是 getOutputEntries 和 getAllSourceRoots 操作。 本教程也有一些简短的示例。 我与 JDT 合作,所以这就是我能做的。 希望能帮助到你 :)
You should take a look at ICProject, especially the getOutputEntries and getAllSourceRoots operations. This tutorial has some brief examples too. I work with JDT so thats pretty much what I can do. Hope it helps :)