Automatic static imports

63 views
Skip to first unread message

Toby Kurien

unread,
Feb 2, 2016, 8:35:29 AM2/2/16
to Xtend Programming Language
One of the pain points of using static imports to create a DSL is manually having to statically import the relevant classes, e.g.:

import static extension org.xtendroid.utils.AlertUtils.*
import static extension org.xtendroid.utils.TimeUtils.*
...

Groovy has an interesting solution, where you define an XML file in your META-INF directory telling it to automatically import the classes defined there-in. I think Xtend could implement a similar solution, where I could say something like:

<static-imports>
  <class base="android.support.v7.app.AppCompatActivity">org.xtendroid.utils.AlertUtils.*</class>
  <class base="android.support.v7.app.AppCompatActivity">org.xtendroid.utils.TimeUtils.*</class>
</static-imports>

This way, whenever I am editing any base-class of AppCompatActivity, I automatically have the AlertUtils and TimeUtils in my auto-complete list, and if I use one, then it adds the static import automatically to the top of the xtend source. Most of the time I'm too lazy to add the static import, so I use it the ugly way:

AlertUtils.toast(this, "my message")

instead of:

toast("my message")

Alessandro Carraro

unread,
Feb 5, 2016, 7:19:26 AM2/5/16
to Xtend Programming Language
I don't know the internals, but would be nice if Xtend could auto-import using the "Favourites" feature (in Eclipse, I don't know if IntelliJ has something similar) found in 
the preference Java>Editor>Content Assist>Favorites. If implemented, I think it should use that static impot list and not duplicate it for Xtend use only:
if a class is favourited, it should be independently if used by java or any other JVM language imho

Jan Köhnlein

unread,
Feb 5, 2016, 7:26:02 AM2/5/16
to xtend...@googlegroups.com
You could vote on https://bugs.eclipse.org/bugs/show_bug.cgi?id=398024
> --
> You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Alessandro Carraro

unread,
Feb 5, 2016, 7:39:01 AM2/5/16
to xtend...@googlegroups.com
Thank you very much!

Toby Kurien

unread,
Mar 30, 2016, 6:13:56 AM3/30/16
to Xtend Programming Language, jan.ko...@gmail.com
As an alternative, and even better idea (IMHO), would it be possible that an active annotation can automatically import the static extensions? This way, we could have something like:

@MyDSL class SomeClass {
  def myMethod() {
    <Ctrl+Space> returns completions from import static extension MyUtils.*, for example
  }
}

So basically, @MyDSL annotation tells Xtend editor to import the static extensions from MyUtils (and possibly other classes).
Reply all
Reply to author
Forward
0 new messages