Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(A better way to) Get files within a project using Eclipse and XText

I'm writing an XText editor, and doing some semantic highlighting. Part of the language I'm parsing refers to files, which should exist in the project. I'd like to highlight based on whether these files are in the correct place. At the moment, I've got a very ugly solution, and I'm sure there's a better way:

public void provideHighlightingFor( XtextResource resource, IHighlightedPositionAcceptor acceptor ) {
...
String resStr = resource.getURI().toString();
String projName = resStr.replaceAll( "^.*resource/", "" ).replaceAll( "/.*", "" );
IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
IFile file = workspace.getFile( new Path( projName + "/" + value ) );
ok = file != null && file.exists();

NOTE: "value" is a string which I've encountered when parsing, not the current file. I want to find out if {workspace}/{project}/{value} exists.

There must be a better way to get the project name/location, based on the current file; I've put this as an XText question, as I'd like, if possible, to avoid using the currently selected editor, and base selection on the current resource, which is presented as an XText resource.

NOTE: the code I've ended up using, based on the answer below is:

String platformString = resource.getURI().toPlatformString(true);
IFile myFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
IProject proj = myFile.getProject();
IFile linkedFile = proj.getFile( value );
like image 890
mo-seph Avatar asked Aug 30 '11 11:08

mo-seph


1 Answers

You could use org.eclipse.emf.common.util.URI.toPlatformString(boolean) and afterwards ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));

something like

String platformString = resource.getURI().toPlatformString(true);
IFile myFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
like image 167
Tom Seidel Avatar answered Oct 08 '22 12:10

Tom Seidel