I know that Xtext 2.4 has not been released yet, but I could not resist blogging about a very cool new feature in Xbase: improved automaticimportfunctionalities!
Actually, import functionalities were already great when using Xbase also in previous versions of Xtext, but now they provide a much better experience for the user of your DSL! Indeed, all the import functionalities you are used to with JDT (like automatic import insertion, and organize imports) are available also for your Xbase language; these features were already available in Xtend, and they have been ported to Xbase itself.
At the time of writing, you need to get the very latest updates of Xtext 2.4, using the update sitehttp://download.eclipse.org/modeling/tmf/xtext/updates/composite/latest/.
Before you used to do something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | grammarorg.xtext.example.helloinferrer.HelloInferrerwith org.eclipse.xtext.xbase.Xbase generatehelloInferrer"http://www.xtext.org/example/helloinferrer/HelloInferrer" Model: imports+=Import* greetings+=Greeting*; Import: 'import'importedNamespace=QualifiedNameWithWildcard ; ... |
Now, you can use in your grammar the new Xbase rule:XImportSection:
1 2 3 4 5 6 7 8 9 10 | grammarorg.xtext.example.helloinferrer.HelloInferrerwith org.eclipse.xtext.xbase.Xbase generatehelloInferrer"http://www.xtext.org/example/helloinferrer/HelloInferrer" Model: importSection=XImportSection? greetings+=Greeting*; ... |
In this post I’m reusing some experiments you can find here (https://github.com/LorenzoBettini/Xtext2-experiments, I had blogged about these experiments inprevious posts).
If you now rerun the MWE2 generator, and make sure you merge theplugin.xml_genwithplugin.xmlin the .ui project, your editor will provide some interesting features for free (if you use my examples, you can find a project wizard “New Project” => “Xtext” => “HelloInferrer Project”):
Imports with wildcards are deprecated:
You now have the context menu “Organize Imports” (Shift + Control + O); try that one in the presence of such deprecation warning and imports are organized for you:
Similarly, unused imports are reported as warnings:
Again, use “Organize Imports” to fix that!
The new feature I like most is the automatic insertion of imports! (just like in JDT and Xtend): try to get content assist for a Java type, for instance,
Accept a proposal and the import will be automatically inserted (instead of the fully qualified name):
Xtext rocks! 🙂





Hi,
Thanks for your tutorials about xtext.
I’m begginer in xtext. How can I transform some loop like if, for.. for example:
IfStatement:
‘if’ condition=XExpression then=Block
(=>’else’ else=Block)?;
from xtext to java (mapping in jvmModelInferrer class)?
Do you have some examples about it?
Thanks a lot for your help
Best regards,
Dejan
Hi
Xbase already has if, for statements…