(更好的方法)使用Eclipse和XText获取项目中的文件 [英] (A better way to) Get files within a project using Eclipse and XText

查看:204
本文介绍了(更好的方法)使用Eclipse和XText获取项目中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个XText编辑器,并进行一些语义突出显示。我正在解析的语言的一部分是指项目中应该存在的文件。我想根据这些文件是否在正确的位置突出显示。目前,我有一个非常难看的解决方案,我确定有一个更好的方法:

  public void provideHighlightingFor (XtextResource资源,IHighlightedPositionAcceptor接受者){
...
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&&文件已存在();

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



  String platformString = resource 。.getURI()toPlatformString(真); 
IFile myFile = ResourcesPlugin.getWorkspace()。getRoot()。getFile(new Path(platformString));


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 );

解决方案

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));

这篇关于(更好的方法)使用Eclipse和XText获取项目中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆