How to replace param "t" to "className"?

Viewed 591
Android Studio 3.2.1
Kotlin plugin version 1.3.11-release-Studio3.2-1
ext.kotlin_version = '1.3.11' 
RxJava 1.3.0 

For example when I type in *.kt file: Observable.unsafeCreate{} and then press Ctrl+Space between {} , then IntellijIdea suggest me to type symbol t ->, but when I do the same in .java file result is subscriber -> :

*.kt file:

fun anyFunc(): Observable<Boolean>{
    return Observable.unsafeCreate { t -> t }
}

enter image description here

*.java file:

public void anyFunc() {
    Observable.unsafeCreate(subscriber -> {});
}

How to make in Kotlin file the same behavior of suggestion typing like in Java file?


[UPDATE] Seems it is because of call() method in rx.functions.Func1, it has param t:

public interface Func1<T, R> extends Function { 
    R call(T t); 
}
2 Answers

I'm using

Android Studio 3.2.1,

rxjava:2.2.0,

Kotlin plugin version 1.3.11-release-Studio3.2-1

After typing in *.kt file: Observable.unsafeCreate{} and pressing Ctrl+Space between {} Android Studio shows me next suggestion:

enter image description here

The same suggestion Android Studio shows when I'm typing in *.java file.

I didn't make any configuration changes in Android Studio Preferences.

I guess you are importing Observable from rx - the first item in the completion list on the image below. Try to import Observable from io.reactivex - highlighted item in the completion list below, it may help: import io.reactivex.Observable.

enter image description here

To use it you need to import rxjava2:

implementation 'io.reactivex.rxjava2:rxjava:2.2.0'

EDIT:

As was figured out code completion dialog showed t -> because parameter in Action1.call(T t) called t. We can see it if we look at the signature of rx.Observable.unsafeCreate method in RxJava 1.3:

public static <T> Observable<T> unsafeCreate(OnSubscribe<T> f) {
    return new Observable<T>(RxJavaHooks.onCreate(f));
}

OnSubscribe interface extends Action1<Subscriber<? super T>, and Action1 has next signature:

public interface Action1<T> extends Action {
    void call(T t);
}

So parameter is called t and Android Studio suggests it as t ->.

In RxJava2 we have different signature:

public static <T> Observable<T> unsafeCreate(ObservableSource<T> onSubscribe) {...}

public interface ObservableSource<T> {
    void subscribe(@NonNull Observer<? super T> observer);
}

We see that in ObservableSource.subscribe() method parameter is called observer, so we see it as observer->.

Conclusion: IDEA's suggestion is based on the parameter's name of the method of Functional interface you are implementing as lambda.

Actually Intellij helps you on the Java side but not (yet) on the Kotlin side.

The subscriber-parameter-name is derived from the generic type of the extended interface, i.e. Subscriber from interface OnSubscribe<T> extends Action1<Subscriber<? super T>>.

On the Kotlin side however this mechanism isn't applied and so the code completion suggests you the parameter of the implementing function instead, which in this case is from Action1#call(T t), so just t.

You can test that it uses the generic type information by just making a basic example:

class CustomObject {}
interface MyCustomConsumer extends Consumer<CustomObject> { }

static void test(MyCustomConsumer mcs) { }

public static void main(String[] args) {
     test( // <- place cursor here and start code completion and you should get "customObject"

Note that for some types it adapts the naming, e.g. String becomes s and the other boxed types (Long, Integer, etc.) are preceded by an a (e.g. aLong; of course, otherwise you would use a reserved word).

If you are curious what's happening in the source code there, you might want to go through Intellij community source code or for Android Studio you may want to go through Building Android Studio first.

EDIT: I should have added the Kotlin plugin code instead ;-) if you want to improve the completion suggestions for Kotlin you may want to have a look at JetBrains/kotlin/idea/idea-completion. In addition JetBrains/kotlin/idea/src might also be worth a look and you probably end up looking at all of the JetBrains/kotlin/idea-packages ;-)

Related