How to enable “type information” for streams returned from methods?

巧了我就是萌 提交于 2019-12-06 21:24:22

问题


Since a few versions, IntelliJ has a very helpful feature: when you put the individual method calls of a stream() statement on separate lines, IntelliJ puts type information on each line:

But when you don't call stream() directly, like when it is returned from another method, that information is omitted:

Is there a way to convince IntelliJ to show such type information for such situations, too?

As pure text, with manually inserted comments to "show" the problem with pure text:

public Stream<Entry<String, String>> withTypeInformation() {
    return generateMap() // Map<String, String>
            .entrySet()  // Set<Entry<String, String>>
            .stream()    // Stream<Set<Entry<String, String>>>
            .filter(e -> !e.getKey().equals("foo")) // Stream<Set<Entry<String, String>>>
            .filter(e -> !e.getKey().equals("bar")) // Stream<Set<Entry<String, String>>>
            .filter(e -> !e.getKey().equals("now"));
}

public Stream<Entry<String, String>> withoutTypeInformation() {
    return withTypeInformation() // no such info 
            .filter(e -> !e.getKey().equals("foo")) // not here either
            .filter(e -> !e.getKey().equals("bar")) // guess what, nothing, too
            .filter(e -> !e.getKey().equals("now"));
}

And note: the first method uses a generator method that returns a map instance. There IntelliJ is smart enough to give me the type information?!


回答1:


Actually, there is a heuristic, that makes IDEA not show this hints. If the count of different types the single chain is less than 3, they won't be shown. It is required to avoid spamming this hints, when the type of expression is obvious (e. g. builders).

In IntelliJ IDEA 2019.2 count of different types, required to show hints can be adjusted in settings (if set it to 1, hints will be always shown).

Note: to get to that setting, one has to turn to Preferences -> Editor -> Inlay Hints -> Java and change the "unique type count" for "Method hints".



来源:https://stackoverflow.com/questions/56489184/how-to-enable-type-information-for-streams-returned-from-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!