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 automatic import functionalities!
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 site http://download.eclipse.org/modeling/tmf/xtext/updates/composite/latest/ .
Before you used to do something like
grammar org.xtext.example.helloinferrer.HelloInferrer with org.eclipse.xtext.xbase.Xbase generate helloInferrer "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:
grammar org.xtext.example.helloinferrer.HelloInferrer with org.eclipse.xtext.xbase.Xbase generate helloInferrer "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 in previous posts).
If you now rerun the MWE2 generator, and make sure you merge the plugin.xml_gen with plugin.xml in 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):