intellij-community
intellij-community copied to clipboard
[kotlin] J2K: Port RemoveUnnecessaryParentheses to JKTree
In the spreadsheet, this one is listed as "RemoveUnnecessaryParenthesesIntention".
There were a few small changes in formatting required to make the tests pass, but they seemed harmless enough.
Adding this JKTree processing step unblocked 17 K2 tests, but wasn't sufficient to remove the RemoveUnnecessaryParenthesesIntention postprocessing step because some other postprocessing steps add parentheses, too (mostly, it seems, while appending !!).
Additional test cases included in this commit are largely based on the contents of kotlin/code-insight/intentions-shared/tests/testData/intentions/removeUnnecessaryParentheses.
@abelkov @darthorimar @jocelynluizzi13
So, now org.jetbrains.kotlin.nj2k.printing.JKCodeBuilder.Visitor#visitParenthesizedExpressionRaw can drop this code?
if (parenthesizedExpression.shouldBePreserved) {
printExplicitLabel(parenthesizedExpression)
}
and here:
intentionBasedProcessing(RemoveUnnecessaryParenthesesIntention()) {
// skip parentheses that were originally present in Java code
it.getExplicitLabelComment() == null
},
some other postprocessing steps add parentheses, too
Yes, looks like org.jetbrains.kotlin.idea.j2k.post.processing.processings.DiagnosticBasedProcessingsKt#getFixTypeMismatchDiagnosticBasedProcessing is the culprit. This is easy to fix though. After we do element.replace(psiFactory.createExpressionByPattern("($0)!!", element.text)), we can immediately remove the possibly redundant parentheses. We can call RemoveUnnecessaryParenthesesIntention here directly.
I will do this change myself, because some refactoring of the plugin code may be required.