简体   繁体   English

将IFile转换为XtextResource

[英]Convert IFile to XtextResource

I have an eclipse project with Xtext files.I need to find get all the files in the eclipse project to be XtextResource in order to find metrics about them. 我有一个带有Xtext文件的eclipse项目。我需要找到eclipse项目中的所有文件作为XtextResource ,以便找到有关它们的指标。 so far,I tried the following things : 到目前为止,我尝试了以下操作:

1.Iterate all over the list of files in the project and got them as IFile .but I cant convert IFile to XtextResource . 1.遍历项目中所有文件列表,并将它们作为IFile XtextResource 。但是我无法将IFile转换为XtextResource

2.I success get XtextResource from only active page in IWorkBenchPage ,so if I can find all the Pages in the project and not only the active (in IworkBenchPage )or maybe set all pages in the project as active I think it can work. 2.I成功获得XtextResource 活动页面IWorkBenchPage ,所以如果我能找到的所有项目中的页面,而不仅仅是(在活动IworkBenchPage )或可能设置在项目中活跃,我认为它可以工作的所有页面。

this is an example code to what I have done if I have private static void setResource() { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IEditorPart activeEditor = page.getActiveEditor(); if (activeEditor instanceof XtextEditor) { XtextEditor xtextEditor = (XtextEditor) activeEditor; xtextEditor.getDocument().readOnly((XtextResource resource) -> {
ResourceHandler.resource = resource; return null; }); } }
这是我拥有private static void setResource() { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IEditorPart activeEditor = page.getActiveEditor(); if (activeEditor instanceof XtextEditor) { XtextEditor xtextEditor = (XtextEditor) activeEditor; xtextEditor.getDocument().readOnly((XtextResource resource) -> {
ResourceHandler.resource = resource; return null; }); } }
我所做的示例代码private static void setResource() { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IEditorPart activeEditor = page.getActiveEditor(); if (activeEditor instanceof XtextEditor) { XtextEditor xtextEditor = (XtextEditor) activeEditor; xtextEditor.getDocument().readOnly((XtextResource resource) -> {
ResourceHandler.resource = resource; return null; }); } }
private static void setResource() { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IEditorPart activeEditor = page.getActiveEditor(); if (activeEditor instanceof XtextEditor) { XtextEditor xtextEditor = (XtextEditor) activeEditor; xtextEditor.getDocument().readOnly((XtextResource resource) -> {
ResourceHandler.resource = resource; return null; }); } }

I am not an expert with all those relations in eclipse and hope somecan can save me here. 我不是所有与日食有关的专家,希望有人能在这里救我。 thanks! 谢谢!

The following should work (assuming you create the place where this is used via guice aware extension factory... 以下应该可以工作(假设您通过guice感知扩展工厂创建了使用它的地方...

@Inject
IResourceSetProvider resourceSetProvider;

...
IProject project = file.getProject();
URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
ResourceSet rs = resourceSetProvider.get(project);
Resource r = rs.getResource(uri, true);
r.load(null)

if you dont have guice at your place use 如果您在家里没有果汁,请使用

ResourceSet rs = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(uri).get(IResourceSetProvider.class).get(project);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM