Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Is it possbile to modify "ImportURI Mechanism"?(about Global Scopes Based On Explicit Imports)
Is it possbile to modify "ImportURI Mechanism"? [message #714833] Thu, 11 August 2011 15:14 Go to next message
alex.ren2006 is currently offline alex.ren2006Friend
Messages: 46
Registered: June 2011
Member
I learn the feature of "Global Scopes Based On Explicit Imports (ImportURI Mechanism)" from the tutorial and use it in my project for ATS IDE (www.ats-lang.org). It seems that the generated plugin searches the imported resource starting from the folder where the current file locates. Is it possible to change this behavior so that the user can specify an absolute path for the imported resource in the DSL source code or the search can start from the project's root folder? Thanks a lot.
Re: Is it possbile to modify "ImportURI Mechanism"? [message #714842 is a reply to message #714833] Thu, 11 August 2011 15:27 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

start digging at ImportUriGlobalScopeProvider (getImportedUris) and ImportUriResolver.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Is it possbile to modify "ImportURI Mechanism"? [message #715259 is a reply to message #714842] Sat, 13 August 2011 00:22 Go to previous messageGo to next message
alex.ren2006 is currently offline alex.ren2006Friend
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 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
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

Re: Is it possbile to modify "ImportURI Mechanism"? [message #715383 is a reply to message #715291] Sat, 13 August 2011 17:04 Go to previous messageGo to next message
alex.ren2006 is currently offline alex.ren2006Friend
Messages: 46
Registered: June 2011
Member
"creating within the project a link to the file" would be my last resort. Before that, I want to try the classpath. I created a file G:\clspath\ti05.testimport as my dsl source file, which is outside the workspace. Then I added the folder "G:\claspth" as the "External Class Folder" for the test project. But I just cannot form up the correct URI using classpath. Suppose the name of the test project within the workspace is "testproj". I tried "classpath:/testproj/ti05.testimport". It didn't work. What did I miss or did I totally misunderestand your suggestion?

One more question, if I choose to use classpath, do I have to create "Java project" to hold my dsl source code? My original plan is to use the plugin under development along with CDT since my dsl language is tightly combined with "C" programming language.
Re: Is it possbile to modify "ImportURI Mechanism"? [message #715402 is a reply to message #715383] Sat, 13 August 2011 19:05 Go to previous message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

I really can't help you on that one. A little test revealed that linking to a file uri does indeed work, however, for the editor to open you have to be "within the workspace". You might try to hook into LanguageSpecificURIEditorOpener.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Previous Topic:Content-Assist question
Next Topic:Content Assist Question
Goto Forum:
  


Current Time: Tue Apr 13 05:53:33 GMT 2021

Powered by FUDForum. Page generated in 0.01965 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top