简体   繁体   中英

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. 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 .

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.

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; }); } }

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...

@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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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