Skip to content

Commit

Permalink
Extracted helper PSI related operations into its own file
Browse files Browse the repository at this point in the history
Summary: No logic changes, just moving code around

Reviewed By: cortinico

Differential Revision: D55526890

fbshipit-source-id: 376265c425bda61867fb7c6cad7aefd7e6871a2e
  • Loading branch information
Nivaldo Bondança authored and facebook-github-bot committed Apr 2, 2024
1 parent 9419841 commit a5946fa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -399,8 +397,8 @@ class KotlinInputAstVisitor(
}
}

private fun genSym(): Output.BreakTag {
return Output.BreakTag()
private fun genSym(): BreakTag {
return BreakTag()
}

private fun emitBracedBlock(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
48 changes: 48 additions & 0 deletions core/src/main/java/com/facebook/ktfmt/format/PsiUtils.kt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a5946fa

Please sign in to comment.