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

发布于 2024-12-01 20:28:42 字数 1036 浏览 5 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-12-08 20:28:42

您可以使用 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));

You could use org.eclipse.emf.common.util.URI.toPlatformString(boolean) and afterwards ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));

something like

String platformString = resource.getURI().toPlatformString(true);
IFile myFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文