intellij-community icon indicating copy to clipboard operation
intellij-community copied to clipboard

[kotlin] J2K: Port RemoveUnnecessaryParentheses to JKTree

Open ermattt opened this issue 1 year ago • 2 comments
trafficstars

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

ermattt avatar Apr 17 '24 22:04 ermattt

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
    },

abelkov avatar Apr 19 '24 09:04 abelkov

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.

abelkov avatar Apr 25 '24 12:04 abelkov