Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Can't get Importing Namespaces to work
Can't get Importing Namespaces to work [message #636939] Wed, 03 November 2010 13:22 Go to next message
Thomas Delissen is currently offline Thomas DelissenFriend
Messages: 11
Registered: September 2010
Location: Eindhoven
Junior Member
Hello,

I am trying to use namespace aware importing (Xtext 1.0.0), in such a way that I can import things like:

import file1.*

I followed the instructions in the manual and the example of the domainmodel, but still I can't get it to work. What am I forgetting?

My rules in de grammar look as follows:

Import:
   'import' importedNamespace=QualifiedNameWithWildCard
;

QualifiedNameWithWildCard:
   QualifiedName '.*'
;

QualifiedName:
   ID ('.' ID)*
;

Bar:
   'bar' name=ID
;

Foo:
   'foo' barreference=[Bar|QualifiedName]
;


My workflow contains the following relevant fragments:
(The other ones are outcommented)

fragment = scoping.ImportNamespacesScopingFragment {}
fragment = exporting.QualifiedNamesFragment {}


And my scope-provider, which I have not implemented anything in, yet:

public class FoobarScopeProvider extends ImportedNamespaceAwareLocalScopeProvider {
}


Now, say I have a file written in this grammar called foobar2, containing:

bar barlabel


and my main file foobar1 contains the following:

import foobar2.*

foo foobar2.barlabel


Is this how this should work? In my current tests, my editor cannot see the imported elements: foobar2.barlabel cannot be found.

I hope someone has some clues as to where my error could be, or what I am forgetting. If I should provide more information, please let me know.


Kind regards,

Thomas
Re: Can't get Importing Namespaces to work [message #637017 is a reply to message #636939] Wed, 03 November 2010 18:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hello Thomas,

the problem is that the resource name is not part of the full qualified name of a Bar. So in your case the FQN of The Bar might be barlabel.

You can change this using a IQualifiedNameProvider like (just a very dirty draft)
import java.io.File;
import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
import org.xtext.example.mydsl1.myDsl1.Bar;

public class MyQNP extends DefaultDeclarativeQualifiedNameProvider {

	public String qualifiedName(Bar bar) {
		return new File(bar.eResource().getURI().toFileString()).getName()+"."+bar.getName();
	}
	
}



~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Can't get Importing Namespaces to work [message #637051 is a reply to message #637017] Wed, 03 November 2010 22:57 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Christian, hi Thomas,

just a minor addition: I'd use
resource.getUri().trimExtension().lastSegment() instead.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 03.11.10 19:56, schrieb Christian Dietrich:
> Hello Thomas,
>
> the problem is that the resource name is not part of the full qualified
> name of a Bar. So in your case the FQN of The Bar might be barlabel.
>
> You can change this using a IQualifiedNameProvider like (just a very
> dirty draft)
>
> import java.io.File;
> import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProv ider;
> import org.xtext.example.mydsl1.myDsl1.Bar;
>
> public class MyQNP extends DefaultDeclarativeQualifiedNameProvider {
>
> public String qualifiedName(Bar bar) {
> return new
> File(bar.eResource().getURI().toFileString()).getName()+"."+bar.getName();
> }
>
> }
>
>
>
> ~Christian
>
Re: Can't get Importing Namespaces to work [message #637115 is a reply to message #636939] Thu, 04 November 2010 09:27 Go to previous messageGo to next message
Thomas Delissen is currently offline Thomas DelissenFriend
Messages: 11
Registered: September 2010
Location: Eindhoven
Junior Member
Okay... So, to see if I understand: Because ImportedNamespaceAwareLocalScopeProvider uses this DefaultDeclaritiveQualifiedNameProvider, you can not refer to the filename (the resource), so I should override this function.

Yes, that is helpful, I can do that, but I think my problem is something else. More basic, perhaps. The linking isn't established at all, I need to understand the ImportedNamespaceAwareLocalScopeProvider.

If, for example, each file has a top level element called 'Model', with a name=ID , which contains the imports, I should be able to use elements from the other files if I import this Model, right?

So, if my entry rule looks like this:

Model: 
   name=ID
   imports+=Import*
   foos+=Foo*
   bars+=Bar*
;


What do I need to type after 'import' to be able to reference all the elements from within another file?

<filename>.<Modelname>.*
or
<Modelname>.*

I do not get any suggestions from the editor in this. It cannot make the link. The files are in the same directory, and if I use other scoping methods (such as the DefaultGlobalScopeProvider), it finds everything, but that is also not what I want. I would like to be able to only import certain namespaces.


Kind regards,

Thomas
Re: Can't get Importing Namespaces to work [message #637425 is a reply to message #637115] Fri, 05 November 2010 12:49 Go to previous message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
I'd recommend to read the section on scoping in the docs.

Qualified names are by default composed of the names of the object and
all its containers. In you case, the file name, too.

An importedNamespace only makes elements from the global scope available
by a name that's the fully qualified name without the importedNamespace
as a prefix. If your objects are further nested, the import might not
work as you expect.

It also sounds like you want an implicit import for elements in the same
file. You could add this by overriding
org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalSc opeProvider.internalGetImportNormalizers(EObject)

Regards
Jan



Am 04.11.10 10:27, schrieb Thomas Delissen:
> Okay... So, to see if I understand: Because
> ImportedNamespaceAwareLocalScopeProvider uses this
> DefaultDeclaritiveQualifiedNameProvider, you can not refer to the
> filename (the resource), so I should override this function.
>
> Yes, that is helpful, I can do that, but I think my problem is something
> else. More basic, perhaps. The linking isn't established at all, I need
> to understand the ImportedNamespaceAwareLocalScopeProvider.
> If, for example, each file has a top level element called 'Model', with
> a name=ID , which contains the imports, I should be able to use elements
> from the other files if I import this Model, right?
>
> So, if my entry rule looks like this:
>
>
> Model: name=ID
> imports+=Import*
> foos+=Foo*
> bars+=Bar*
> ;
>
>
> What do I need to type after 'import' to be able to reference all the
> elements from within another file?
> <filename>.<Modelname>.* or
> <Modelname>.*
>
> I do not get any suggestions from the editor in this. It cannot make the
> link. The files are in the same directory, and if I use other scoping
> methods (such as the DefaultGlobalScopeProvider), it finds everything,
> but that is also not what I want. I would like to be able to only import
> certain namespaces.


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Previous Topic:XText 1.0.0 vs 1.0.1: Changes in implementation of ContentAssistContext ?
Next Topic:If, then and else Structure
Goto Forum:
  


Current Time: Wed Apr 24 20:37:15 GMT 2024

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

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

Back to the top