-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
False positive finding for WRONG_INDENTATION
#1312
Comments
Thank you for you report! It is very confusing, but it should be working. We will check asap! |
@WebTiger89 hm, I checked it in our online demo and it works fine... |
Which version are you using? May be code snippet is more complex? |
Damn I checked the diktat-analysis.yml file and by default
When I disable it, it works at this point like expected :) EDIT: I'm on the latest version |
I took the configuration from here: |
You might want to adjust the default setting. Can be closed. Thanks for the fast support :) |
yeah, that is the legacy support for old IDEAs :) |
|
…s` to `false` ### What's done: * The `WRONG_INDENTATION:extendedIndentOfParameters` flag is now unset by default. This closes #1312.
…s` to `false` ### What's done: * The `WRONG_INDENTATION:extendedIndentOfParameters` flag is now unset by default. This closes #1312.
…s` to `false` ### What's done: * The `WRONG_INDENTATION:extendedIndentOfParameters` flag is now unset by default. This closes #1312.
…s` to `false` ### What's done: * The `WRONG_INDENTATION:extendedIndentOfParameters` flag is now unset by default. This closes #1312.
@akuleshov7 Sorry I have to reopen this. While example above is not reported anymore after setting Example: @Composable
fun YoutubeChip(selected: Boolean, text: String, modifier: Modifier = Modifier) {
Surface(
color = when {
selected -> MaterialTheme.colors.onSurface
else -> Color.Transparent
},
)
}
etc. Ktlint online reports it too. Here the whole rule of my config file: # Checks that indentation is correct
- name: WRONG_INDENTATION
enabled: true
configuration:
# Is newline at the end of a file needed
newlineAtEnd: true
# If true: in parameter list when parameters are split by newline they are indented with two indentations instead of one
extendedIndentOfParameters: false
# If true: if first parameter in parameter list is on the same line as opening parenthesis, then other parameters can be aligned with it
alignedParameters: true
# If true: if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one
extendedIndentAfterOperators: true
# The indentation size for each file
indentationSize: 4 To be honest it does not make any sense why the first example is not reported but this one is. It happens everywhere in code, not only Jetpack Compose code. If you need more examples just let me know. |
I might figured out on which code snippets it fails mainly. It seems 2 specific cases can not be handled properly:
In these cases diktatCheck reports WRONG_INDENTATION and would fix it by adding 4 extra indents to the lines below the line with the '='. Example for 1:
or
or
or
or
or
Example for 2:
Note: Git won't indent last example properly, but you know how it should look like. |
@WebTiger89, thank you for your feedback. I'm looking into the issue and will get back to you in a short while. |
code provided by @WebTiger89 is a legacy issue in IDEA - I have described it here: #1323 (comment) |
@WebTiger89, today we've made a minor fix (0706885) which may affect your experience. From now on, Apart from that, your first example is correct: @Composable
fun YoutubeChip(selected: Boolean, text: String, modifier: Modifier = Modifier) {
Surface(
color = when {
selected -> MaterialTheme.colors.onSurface
else -> Color.Transparent
},
)
} — and consistent with the indentation config you've provided: - name: WRONG_INDENTATION
enabled: true
configuration:
newlineAtEnd: true
extendedIndentOfParameters: false
alignedParameters: true
extendedIndentAfterOperators: true
indentationSize: 4 If you change the value of @Composable
fun YoutubeChip(selected: Boolean, text: String, modifier: Modifier = Modifier) {
Surface(
color = when {
selected -> MaterialTheme.colors.onSurface
else -> Color.Transparent
},
)
} — note that I've increased the indentation in lines 4 through 7: the 4-line Additional examples you've supplied are affected by other flags in the
I'll provide the detailed feedback for each example tomorrow morning. Yet, the way DiKTat's indentation checker behaves looks good to me, I don't see any issues so far. |
Expression bodies, example 1 of 6: val currentTime: Time
get() =
with(currentDateTime) {
Time(hour = hour, minute = minute, second = second)
} — unaffected by the If you want to un-set the val currentTime: Time
get() =
with(currentDateTime) {
Time(hour = hour, minute = minute, second = second)
} |
Expression bodies, example 2 of 6: fun formatDateByPattern(date: String, pattern: String = "ddMMyy"): String =
DateTimeFormatter.ofPattern(pattern).format(LocalDate.parse(date)) — same as the previous example, only affected by the fun formatDateByPattern(date: String, pattern: String = "ddMMyy"): String =
DateTimeFormatter.ofPattern(pattern).format(LocalDate.parse(date)) |
Expression bodies, example 3 of 6: fun LocalDateTime.updateTime(
hour: Int? = null,
minute: Int? = null,
second: Int? = null,
): LocalDateTime = withHour(hour ?: getHour())
.withMinute(minute ?: getMinute())
.withSecond(second ?: getSecond()) — with the current formatting compliant with fun LocalDateTime.updateTime(
hour: Int? = null,
minute: Int? = null,
second: Int? = null,
): LocalDateTime = withHour(hour ?: getHour())
.withMinute(minute ?: getMinute())
.withSecond(second ?: getSecond()) With fun LocalDateTime.updateTime(
hour: Int? = null,
minute: Int? = null,
second: Int? = null,
): LocalDateTime = withHour(hour ?: getHour())
.withMinute(minute ?: getMinute())
.withSecond(second ?: getSecond()) The expected indentation level being incremented with each new chained method call is a bug and will be fixed separately (see gh-1336). |
Expression bodies, example 4 of 6: DiKTat reports Wrapping expressions before an operator in Kotlin is discouraged, partially because the same operators may be used for both unary and binary operations, e. g.: fun foo() {
val a = 3 +
2 // a = 5
val b = 3 // b = 3
+ 2 // and this is just an unary plus: 2.unaryPlus()
} For the same reason, DiKTat will additionally report private fun createLayoutParams(): WindowManager.LayoutParams =
WindowManager.LayoutParams().apply {
type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL
token = composeView.applicationWindowToken
width = WindowManager.LayoutParams.MATCH_PARENT
height = WindowManager.LayoutParams.MATCH_PARENT
format = PixelFormat.TRANSLUCENT
// TODO make composable configurable
// see https://stackoverflow.com/questions/43511326/android-making-activity-full-screen-with-status-bar-on-top-of-it
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
windowInsetsController?.hide(WindowInsets.Type.statusBars())
} else {
@Suppress("DEPRECATION")
systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
}
} You will be able to control whether a regular (single) or a continuation (double) indent level is used using the At the same time, there's indeed an issue with the way how DiKTat checks expression bodies which start at the next line. Should be (IDEA 2022.1 with default code style settings, using continuation indent for expression body functions): private fun createLayoutParams(): WindowManager.LayoutParams =
WindowManager.LayoutParams().apply { /* ... */ } DiKTat, private fun createLayoutParams(): WindowManager.LayoutParams =
WindowManager.LayoutParams().apply { /* ... */ } DiKTat, private fun createLayoutParams(): WindowManager.LayoutParams =
WindowManager.LayoutParams().apply { /* ... */ } The same is true (continuation indent should be used) for other examples you've submitted: val offsetDelta =
if (shimmerAnimationType != ShimmerAnimationType.FADE) translateAnim.dp
else 2000.dp and private fun lerp(start: Float, stop: Float, fraction: Float): Float =
(1 - fraction) * start + fraction * stop I believe this is gh-1330. |
WRONG_INDENTATION
@WebTiger89, your other examples: and are well-formatted provided |
@akuleshov7, I suggest we switch the default value of
Update: the above is not true, see below. |
The previous comment turned out to be not true. IDEA behaves differently, depending on how exactly Kotlin style guide is applied. By default (i. e. unless any style guide is applied, empty XML file), continuation indent is on wherever applicable, equivalent to: <JetCodeStyleSettings>
<option name="CONTINUATION_INDENT_IN_PARAMETER_LISTS" value="true" />
<option name="CONTINUATION_INDENT_IN_ARGUMENT_LISTS" value="true" />
<option name="CONTINUATION_INDENT_FOR_EXPRESSION_BODIES" value="true" />
<option name="CONTINUATION_INDENT_FOR_CHAINED_CALLS" value="true" />
<option name="CONTINUATION_INDENT_IN_SUPERTYPE_LISTS" value="true" />
<option name="CONTINUATION_INDENT_IN_IF_CONDITIONS" value="true" />
<option name="CONTINUATION_INDENT_IN_ELVIS" value="true" />
</JetCodeStyleSettings> Now, if the code style is set via Preferences -> Editor -> Code Style -> Kotlin -> Load/Save -> Use defaults from: -> Kotlin Coding Conventions: — then the following settings are explicitly set in XML: <JetCodeStyleSettings>
<option name="CONTINUATION_INDENT_IN_PARAMETER_LISTS" value="true" />
<option name="CONTINUATION_INDENT_IN_ARGUMENT_LISTS" value="true" />
<option name="CONTINUATION_INDENT_FOR_EXPRESSION_BODIES" value="true" />
<option name="CONTINUATION_INDENT_FOR_CHAINED_CALLS" value="true" />
<option name="CONTINUATION_INDENT_IN_SUPERTYPE_LISTS" value="true" />
<option name="CONTINUATION_INDENT_IN_IF_CONDITIONS" value="true" />
<option name="CONTINUATION_INDENT_IN_ELVIS" value="true" />
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings> Note that Next, if we re-set our code style settings to the defaults and re-apply the same Kotlin style guide, now from the "Set from..." hyperlink: — then <JetCodeStyleSettings>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings> — and the effective values of all continuation indent related flags will be That said, IDEA behaviour is inconsistent and misleading, and the official style guide (in which there's not a single mention of a continuation indent) should be consulted from now on. See also Differences between "Kotlin coding conventions" and "IntelliJ IDEA default code style":
Kotlin style guide for Android prescribes exactly the same:
|
…dIndentBeforeDot` to `false` ### What's done: * Since Kotlin style guide mentions no continuation indent, and IDEA defaults to no continuation indent as well, all `extendedIndent*` flags default to `false` from now on. * Closes: #1312
…dIndentBeforeDot` to `false` ### What's done: * Since Kotlin style guide mentions no continuation indent, and IDEA defaults to no continuation indent as well, all `extendedIndent*` flags default to `false` from now on. * Closes: #1312
…dIndentBeforeDot` to `false` (#1342) ### What's done: * Since Kotlin style guide mentions no continuation indent, and IDEA defaults to no continuation indent as well, all `extendedIndent*` flags default to `false` from now on. * Reformatted the codebase by running `mvn diktat:fix@diktat` so that it conforms to the new rules. * Unit tests adapted to the reformatted test resources. * Unit tests adapted to the new defaults. * Closes: #1312
When running diktatCheck I get a false-positive finding for the following code snippet:
Warning: expected 20 but was 16
So it expects:
Maybe this is Jetpack Compose specific and you want to adjust the rule in this direction.
The text was updated successfully, but these errors were encountered: