8

我正在编写一个 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 );
4

1 回答 1

8

你可以使用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));
于 2011-08-30T11:49:39.147 回答