(Best way) Get files in a project using Eclipse and XText

I am writing an XText editor and making semantic highlighting. Part of the language I am processing relates to files that must exist in the project. I would like to dwell on whether these files are in the right place. At the moment, I have a very ugly solution, and I'm sure there is 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 the line that I encountered while parsing, not the current file. I want to find out if {workspace} / {project} / {value} exists.

There should be a better way to get the project name / location based on the current file; I posed this as an XText question, as I would like, if possible, to avoid using the currently selected editor and selecting a database on the current resource, which is presented as an XText resource.

NOTE. The code I used is based on the following below:

 String platformString = resource.getURI().toPlatformString(true); IFile myFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString)); IProject proj = myFile.getProject(); IFile linkedFile = proj.getFile( value ); 
+8
java eclipse plugins xtext
source share
1 answer

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

something like

 String platformString = resource.getURI().toPlatformString(true); IFile myFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString)); 
+8
source share

All Articles