diff --git a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt index b01bdc3b..353aee43 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt @@ -23,7 +23,6 @@ import com.google.googlejavaformat.FormattingError import com.google.googlejavaformat.Indent import com.google.googlejavaformat.Indent.Const.ZERO import com.google.googlejavaformat.OpsBuilder -import com.google.googlejavaformat.Output import com.google.googlejavaformat.Output.BreakTag import java.util.ArrayDeque import java.util.Optional @@ -124,7 +123,6 @@ import org.jetbrains.kotlin.psi.KtWhenConditionWithExpression import org.jetbrains.kotlin.psi.KtWhenExpression import org.jetbrains.kotlin.psi.KtWhileExpression import org.jetbrains.kotlin.psi.psiUtil.children -import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespace import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespace import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.psi.psiUtil.startsWithComment @@ -399,8 +397,8 @@ class KotlinInputAstVisitor( } } - private fun genSym(): Output.BreakTag { - return Output.BreakTag() + private fun genSym(): BreakTag { + return BreakTag() } private fun emitBracedBlock( @@ -728,34 +726,6 @@ class KotlinInputAstVisitor( index == parts.indices.last } - /** Returns true if the expression represents an invocation that is also a lambda */ - private fun KtExpression.isLambda(): Boolean { - return extractCallExpression(this)?.lambdaArguments?.isNotEmpty() ?: false - } - - /** Does this list have parens with only whitespace between them? */ - private fun KtParameterList.hasEmptyParens(): Boolean { - val left = this.leftParenthesis ?: return false - val right = this.rightParenthesis ?: return false - return left.getNextSiblingIgnoringWhitespace() == right - } - - /** Does this list have parens with only whitespace between them? */ - private fun KtValueArgumentList.hasEmptyParens(): Boolean { - val left = this.leftParenthesis ?: return false - val right = this.rightParenthesis ?: return false - return left.getNextSiblingIgnoringWhitespace() == right - } - - /** - * emitQualifiedExpression formats call expressions that are either part of a qualified - * expression, or standing alone. This method makes it easier to handle both cases uniformly. - */ - private fun extractCallExpression(expression: KtExpression): KtCallExpression? { - val ktExpression = (expression as? KtQualifiedExpression)?.selectorExpression ?: expression - return ktExpression as? KtCallExpression - } - override fun visitCallExpression(callExpression: KtCallExpression) { builder.sync(callExpression) with(callExpression) { @@ -2594,7 +2564,7 @@ class KotlinInputAstVisitor( sync(psiElement.startOffset) } - /** Prevent susequent comments from being moved ahead of this point, into parent [Level]s. */ + /** Prevent subsequent comments from being moved ahead of this point, into parent [Level]s. */ private fun OpsBuilder.fenceComments() { addAll(FenceCommentsOp.AS_LIST) } diff --git a/core/src/main/java/com/facebook/ktfmt/format/PsiUtils.kt b/core/src/main/java/com/facebook/ktfmt/format/PsiUtils.kt new file mode 100644 index 00000000..3e33dabc --- /dev/null +++ b/core/src/main/java/com/facebook/ktfmt/format/PsiUtils.kt @@ -0,0 +1,48 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.facebook.ktfmt.format + +import org.jetbrains.kotlin.psi.KtCallExpression +import org.jetbrains.kotlin.psi.KtExpression +import org.jetbrains.kotlin.psi.KtParameterList +import org.jetbrains.kotlin.psi.KtQualifiedExpression +import org.jetbrains.kotlin.psi.KtValueArgumentList +import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespace + +/** Returns true if the expression represents an invocation that is also a lambda */ +fun KtExpression.isLambda(): Boolean = this.callExpression?.lambdaArguments?.isNotEmpty() ?: false + +/** Does this list have parens with only whitespace between them? */ +fun KtParameterList.hasEmptyParens(): Boolean { + val left = this.leftParenthesis ?: return false + val right = this.rightParenthesis ?: return false + return left.getNextSiblingIgnoringWhitespace() == right +} + +/** Does this list have parens with only whitespace between them? */ +fun KtValueArgumentList.hasEmptyParens(): Boolean { + val left = this.leftParenthesis ?: return false + val right = this.rightParenthesis ?: return false + return left.getNextSiblingIgnoringWhitespace() == right +} + +/** + * [Formatter.emitQualifiedExpression] formats call expressions that are either part of a qualified + * expression, or standing alone. This method makes it easier to handle both cases uniformly. + */ +private val KtExpression.callExpression: KtCallExpression? + get() = ((this as? KtQualifiedExpression)?.selectorExpression ?: this) as? KtCallExpression