我终于找到了一种直接从我的插件添加我的库的方法。尽管尤金的回答没有错,但它缺乏一些解释。我将尝试展示如何做到这一点。
如果你想添加一个包含多个文件的库,你可以这样做:
- 创建一个扩展JsGlobalScopeContainerInitializer的类
- 为扩展点org.eclipse.wst.jsdt.core.JsGlobalScopeContainerInitializer贡献一个扩展
- 添加一个IIncludePathEntry,使用它的 id 指向 JsGlobalScopeContainer ,到你想在其中使用库的项目
1.创建一个扩展JsGlobalScopeContainerInitializer的类
那里有一些非常令人困惑的教程(包括 eclipse wiki 上的教程),起初使人们更难理解这一点。我想出了以下内容:
[... package and imports ommited ...]
public class LibInitializer extends JsGlobalScopeContainerInitializer {
private static final String LIBRARY_ID = "com.testvendor.testplugin.library";
public IPath getPath() {
return new Path(LIBRARY_ID);
}
@Override
public LibraryLocation getLibraryLocation() {
return null;
}
@Override
public String getDescription() {
return "Test Library";
}
@Override
public String getDescription(IPath containerPath, IJavaScriptProject project) {
return getDescription();
}
@Override
public IIncludePathEntry[] getIncludepathEntries() {
try {
//get the Bundle object of the plugin
Bundle bundle = Platform.getBundle("com.testvendor.testplugin");
//get the java.io.File object corresponding to the root of the bundles installation directory
File bundleFile = FileLocator.getBundleFile(bundle);
//add the location pointing to the library relative to that bundle root
File libraryLocation = new File(bundleFile, "bin/com/testvendor/testplugin/library/");
//create a Path object from it
IPath pa = new Path(libraryLocation.getAbsolutePath());
/* create an IIncludePathEntry of the type "library" from this path
my library only contains one folder (for now) so this is it */
IIncludePathEntry entry = JavaScriptCore.newLibraryEntry(pa, pa, pa);
//put the entry (or entries if you had more) into an array and return
IIncludePathEntry[] entries = {entry};
return entries;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
最有趣的部分是方法getIncludepathEntries(),其中将从容器中检索实际条目。由于 IIncludePathEntry 不适用于“file://”伪协议的 URL,因此 Eugene 建议的“toFileURL”方法在这里不起作用。
2.为 JSGlobalScope... 扩展点贡献一个扩展
要告诉项目包含 id 为com.testvendor.testplugin.library的容器条目,最简单的方法是向扩展点 *org.eclipse.wst.jsdt.core.JsGlobalScopeContainerInitializer** 贡献代码,如下所示:
<extension point="org.eclipse.wst.jsdt.core.JsGlobalScopeContainerInitializer">
<JsGlobalScopeContainerInitializer
id="com.testvendor.testplugin.library"
class="com.testvendor.testplugin.library.LibInitializer"/>
</extension>
其中课程当然是指第 1 步中的 JsGlobalScopeContainerInitializer。
3.将 IIncludePathEntry 添加到项目中
IJavaScriptProject jsProj = ... get your project object from somewhere ...
//create an instance of the container from step 1.
JsGlobalScopeContainerInitializer container = new LibInitializer();
//create an includepath entry refering to the container
IIncludePathEntry entry = JavaScriptCore.newContainerEntry(container.getPath());
IIncludePathEntry[] ipaths = jsProj.getRawIncludepath();
IIncludePathEntry[] newpaths = new IIncludePathEntry[ipaths.length +1];
System.arraycopy(ipaths, 0, newpaths, 0, ipaths.length);
//add the new entry
newPaths[ipaths.length] = enty;
// set the new includepath to the project
jsProj.setRawIncludepath(newpaths, null);
如果您现在幸运的话,您的 JavaScript 资源中将有一个库条目,其中包含您使用 ContainerIntitializer 添加的库文件夹中包含的所有 JavaScript 对象和类。所有这些对象和类都将在代码完成建议中可用。
我希望这可以防止其他人花费数小时的时间在一个可能比实际更简单的主题上。