java - (更好的方法)使用 Eclipse 和 XText 在项目中获取文件

标签 java eclipse plugins 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();

注意:“值”是我在解析时遇到的字符串,而不是当前文件。我想知道 {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/

相关文章:

java - 大于显示时的屏幕截图而不滚动

opengl - 带有 Oculus Rift 的原生渲染插件

java - 映射 Http 请求

java - 当线程在后台运行时,MediaPlayer 不显示视频

java - Eclipse 给出 “Java was started but returned exit code 13”

java - 如何在 Eclipse 中禁用非类型建议?

java - 编码 Sonar 插件2.13

java - 如何在 Eclipse 中导入库并使用该库中的类

java-me - 适用于 MIPS 的 CDC for Oracle Java ME 嵌入式客户端资源

Eclipse 无法识别 JavaFX 类