(更好的方法)使用 Eclipse 和 XText 获取项目中的文件
我正在编写一个 XText 编辑器,并进行一些语义突出显示。我正在解析的部分语言引用了文件,这些文件应该存在于项目中。我想根据这些文件是否位于正确的位置来突出显示。目前,我有一个非常丑陋的解决方案,我确信有更好的方法:
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();
注意:“value”是我在解析时遇到的字符串,而不是当前文件。我想知道 {workspace}/{project}/{value} 是否存在。
必须有更好的方法来根据当前文件获取项目名称/位置;我将其作为 XText 问题,因为我希望,如果可能的话,避免使用当前选择的编辑器,并基于当前资源(作为 XText 资源呈现)进行基础选择。
注意:根据下面的答案,我最终使用的代码是:
String platformString = resource.getURI().toPlatformString(true);
IFile myFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
IProject proj = myFile.getProject();
IFile linkedFile = proj.getFile( value );
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 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 org.eclipse.emf.common.util.URI.toPlatformString(boolean) ,然后使用 ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString)); 东西
类似的
You could use
org.eclipse.emf.common.util.URI.toPlatformString(boolean)
and afterwardsResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
something like