我正在编写一个 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 );
你可以使用org.eclipse.emf.common.util.URI.toPlatformString(boolean)
然后ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
就像是
String platformString = resource.getURI().toPlatformString(true);
IFile myFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)