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

一曲冷凌霜 提交于 2019-12-08 19:21:36

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/7242403/a-better-way-to-get-files-within-a-project-using-eclipse-and-xtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!