From 2e8ee403314f8b66b7b89b06c20d82fc239c769c Mon Sep 17 00:00:00 2001 From: Denis Zhdanov Date: Thu, 22 Apr 2010 16:29:39 +0400 Subject: [PATCH] IDEA-43962 Indentation error on deep nesting levels Initial version. Consists of code moved from AbstractJavaBlock class. --- .../psi/formatter/java/FormattingAstUtil.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 java/java-impl/src/com/intellij/psi/formatter/java/FormattingAstUtil.java diff --git a/java/java-impl/src/com/intellij/psi/formatter/java/FormattingAstUtil.java b/java/java-impl/src/com/intellij/psi/formatter/java/FormattingAstUtil.java new file mode 100644 index 000000000000..f23eb0f72a60 --- /dev/null +++ b/java/java-impl/src/com/intellij/psi/formatter/java/FormattingAstUtil.java @@ -0,0 +1,92 @@ +/* + * Copyright 2000-2010 JetBrains s.r.o. + * + * 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.intellij.psi.formatter.java; + +import com.intellij.lang.ASTNode; +import com.intellij.psi.PsiBinaryExpression; +import com.intellij.psi.TokenType; +import com.intellij.psi.impl.source.SourceTreeToPsiMap; +import com.intellij.psi.impl.source.tree.JavaElementType; +import com.intellij.psi.tree.IElementType; +import org.jetbrains.annotations.Nullable; + +import java.util.HashSet; +import java.util.Set; + +import static java.util.Arrays.asList; + +/** + * Contains various utility methods for AST processing during code formatting. + * + * @author Denis Zhdanov + * @since Apr 21, 2010 4:02:17 PM + */ +public class FormattingAstUtil { + + /** Holds type of AST elements that are considered to be assignments. */ + private static final Set ASSIGNMENT_ELEMENT_TYPES = new HashSet(asList( + JavaElementType.ASSIGNMENT_EXPRESSION, JavaElementType.LOCAL_VARIABLE, JavaElementType.FIELD + )); + + private FormattingAstUtil() { + } + + /** + * Tries to get previous non-white space AST node for the given one. + * + * @param node base node which left non-white space sibling is to be found + * @return left non-white space sibling of the given node if any; null otherwise + */ + @Nullable + public static ASTNode getPrevElement(final ASTNode node) { + ASTNode result = node.getTreePrev(); + while (result != null && result.getElementType() == TokenType.WHITE_SPACE) { + result = result.getTreePrev(); + } + return result; + } + + /** + * Allows to answer if given node wraps assignement operation. + * + * @param node node to check + * @return true if given node wraps assignement operation; false otherwise + */ + public static boolean isAssignment(ASTNode node) { + return ASSIGNMENT_ELEMENT_TYPES.contains(node.getElementType()); + } + + /** + * Allows to check if given AST nodes refer to binary expressions and have the same priority. + * + * @param node1 node to check + * @param node2 node to check + * @return true if given nodes are binary expressions and have the same priority; + * false otherwise + */ + public static boolean binaryExpressionHasTheSamePriority(ASTNode node1, ASTNode node2) { + if (node1 == null || node2 == null) { + return false; + } + + if (node1.getElementType() != JavaElementType.BINARY_EXPRESSION || node2.getElementType() != JavaElementType.BINARY_EXPRESSION) { + return false; + } + PsiBinaryExpression expression1 = (PsiBinaryExpression)SourceTreeToPsiMap.treeElementToPsi(node1); + PsiBinaryExpression expression2 = (PsiBinaryExpression)SourceTreeToPsiMap.treeElementToPsi(node2); + return expression1.getOperationSign().getTokenType() == expression2.getOperationSign().getTokenType(); + } +}