我正在编写一个 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();
注意:“值”是我在解析时遇到的字符串,而不是当前文件。我想知道 {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));
关于java - (更好的方法)使用 Eclipse 和 XText 在项目中获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7242403/