|
|
Re: Is it possbile to modify "ImportURI Mechanism"? [message #715259 is a reply to message #714842] |
Sat, 13 August 2011 00:22 |
alex.ren2006 Messages: 46 Registered: June 2011 |
Member |
|
|
Thanks a lot for your help. I also got some useful info from the post (http://www.eclipse.org/forums/index.php/mv/msg/207406/664109/#msg_664109). The following is what I did, it works. If I did anything inappropriate, please point it out. For clairty, I use a sample project instead of the real ATS-IDE project. The grammar goes as follows:
=============================
grammar org.xtext.example.testimport.TestImport with org.eclipse.xtext.common.Terminals
generate testImport "http://www.xtext.org/example/testimport/TestImport"
Model:
imp+=myimport*
def+=valdef*
;
myimport:
'import' importURI=STRING
;
valdef:
'val' name=ID '=' (valint=INT | valref=[valdef])
; =============================
The source code would look like this
=============================
import "folder01/folder02/ti02.testimport"
val x = 3
val y = x
val z = ti02x
val q = ti03x
val xx = ti04x ===================================
I choose to use the "ImportUriGlobalScopeProvider" so my mwe2 file contains the following fragment
===================================
// scoping and exporting API
fragment = scoping.ImportURIScopingFragment {}
fragment = exporting.SimpleNamesFragment {}
// scoping and exporting API
// fragment = scoping.ImportNamespacesScopingFragment {}
// fragment = exporting.QualifiedNamesFragment {}
// fragment = builder.BuilderIntegrationFragment {}
// provides the necessary bindings for java types integration
// fragment = types.TypesGeneratorFragment {} ===================================
To use my own "ImportUriGlobalScopeProvider" and "ImportUriResolver", I add the following to my runtime module
==================================
public Class<? extends ImportUriResolver> bindImportUriResolver() {
return MyImportUriResolver.class;
}
public Class<? extends org.eclipse.xtext.scoping.IGlobalScopeProvider> bindIGlobalScopeProvider() {
return MyImportUriGlobalScopeProvider.class;
} ==================================
"MyImportUriResolver" goes as follows
====================================
public class MyImportUriResolver extends ImportUriResolver {
@Override
public String resolve(EObject from) {
String resolvename = super.resolve(from);
if (null == resolvename) {
return resolvename;
}
System.out.println("resolve: " + resolvename);
Resource resource = from.eResource();
URI importUri = URI.createURI(resolvename);
if (EcoreUtil2.isValidUri(resource, importUri)) {
return resolvename;
}
String projname = null;
String path = from.eResource().getURI().toString();
final String platform = "platform:/resource/";
final String filesys = "file://";
if (path.startsWith(platform)) {
projname = path.substring(platform.length()).split("/")[0];
System.out.println("projname is " + projname);
resolvename = platform + projname + "/" + resolvename;
}
return resolvename;
} ==================================
Basically, I try to find the imported file based on the path of the current file. If it failed, then I try to find the imported file based on the project folder.
To add the support of "default library", "MyImportUriGlobalScopeProvider" goes as follows
==================================
public class MyImportUriGlobalScopeProvider extends
ImportUriGlobalScopeProvider {
@Override
protected LinkedHashSet<URI> getImportedUris(final Resource resource) {
LinkedHashSet<URI> temp = super.getImportedUris(resource);
temp.add(URI.createURI("platform:/resource/atstest2/ti03.testimport"));
temp.add(URI.createURI("file://G:/WORKSPACE/ti04.testimport"));
return temp;
}
} ====================================
The "temp.add(URI.createURI("platform:/resource/atstest2/ti03.testimport"));" works fine. No need to import "ti03.testimport" explicitly in the source code. But "temp.add(URI.createURI("file://G:/WORKSPACE/ti04.testimport"));" leads to strange result. When I press CTRL and move the cursor over the name referencing to certain element defined in "ti04.testimport", the cursor turns into hyper-link. And there is no error report for "cross reference cannot be found". So it seems the cross-reference has been resolved successfully. But when I actually click the mouse, the file "ti04.testimport" is not opened at all. Is there certain kind of constraint that only files in the project can be opened? Thank you once again for your help.
|
|
|
Re: Is it possbile to modify "ImportURI Mechanism"? [message #715291 is a reply to message #715259] |
Sat, 13 August 2011 06:05 |
Alexander Nittka Messages: 1193 Registered: July 2009 |
Senior Member |
|
|
Hi,
an Xtext editor can only open a file located in the worksapce (also the model file has to have a corresponding URI). This is not the case for a file URI.
There are several threads in thins forum (and bugzillas) dealing with the issue of opening a model located outside the workspace. One solution is creating within the project a link to the file. You may want to adapt the URI opener for dealing with your file uris. That said, a file uri as a default import does not seem to be a brilliant idea anyway, better stick to platform plugin/resource or classpath uri.
Alex
[Updated on: Sat, 13 August 2011 06:20] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03128 seconds