ktfmt icon indicating copy to clipboard operation
ktfmt copied to clipboard

Treat trailing Elvis operator as part of a call chain

Open sgrimm opened this issue 1 year ago • 3 comments

This is a followup to #213.

Currently, a trailing Elvis operator is placed on the same line as the last step of a call chain if it fits:

desiredWithdrawals
    ?.filter { it.id == null && it.status == Status.New }
    ?.flatMap { it.userIds } ?: emptyList()

This makes it easy to miss the fact that there's an Elvis operator if I'm quickly skimming the code, because it looks like it's part of the last step of the chained call. For example, the above code snippet isn't very visually distinct from:

desiredWithdrawals
    ?.filter { it.id == null && it.status == Status.New }
    ?.flatMap { it.userIds ?: emptyList() }

I believe it'd be clearer to treat ?: the same as ?. for purposes of line break placement and indentation, such that the first example turns into

desiredWithdrawals
    ?.filter { it.id == null && it.status == Status.New }
    ?.flatMap { it.userIds }
    ?: emptyList()

sgrimm avatar Mar 26 '24 17:03 sgrimm

I agree with this.

avi-cenna avatar Apr 05 '24 18:04 avi-cenna

The current behaviour is consistent with the other binary operators. Although, the other operators are constrained by needing to be on the same line, or else they get parsed as unary operators in a second statement.

Aligning the elvis with the dot of the call chain is also inconsistent; it would need to be indented.

nreid260 avatar Apr 09 '24 13:04 nreid260