From 7016425ae79cb99cc26041259e0051f9db46ae72 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 21 Jul 2014 19:02:54 +0200 Subject: [PATCH 1/2] Cleanup (two utility classes merged) --- .../stubs/JavaImportStatementElementType.java | 6 +- .../psi/impl/source/JavaLightStubBuilder.java | 4 +- .../PsiJavaCodeReferenceElementImpl.java | 2 +- .../psi/impl/source/tree/JavaSourceUtil.java | 81 +++++++++++++++++++ .../psi/impl/source/tree/SourceUtil.java | 79 ++---------------- .../tree/java/ExpressionPsiElement.java | 21 +++-- .../tree/java/PsiPackageStatementImpl.java | 4 +- .../tree/java/PsiReferenceExpressionImpl.java | 2 +- 8 files changed, 107 insertions(+), 92 deletions(-) diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/java/stubs/JavaImportStatementElementType.java b/java/java-psi-impl/src/com/intellij/psi/impl/java/stubs/JavaImportStatementElementType.java index 7c9af30f1d9b..93e359e6778a 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/java/stubs/JavaImportStatementElementType.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/java/stubs/JavaImportStatementElementType.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -24,7 +24,7 @@ import com.intellij.psi.impl.java.stubs.impl.PsiImportStatementStubImpl; import com.intellij.psi.impl.source.PsiImportStatementImpl; import com.intellij.psi.impl.source.PsiImportStaticStatementImpl; import com.intellij.psi.impl.source.tree.JavaElementType; -import com.intellij.psi.impl.source.tree.SourceUtil; +import com.intellij.psi.impl.source.tree.JavaSourceUtil; import com.intellij.psi.impl.source.tree.java.ImportStaticStatementElement; import com.intellij.psi.stubs.IndexSink; import com.intellij.psi.stubs.StubElement; @@ -68,7 +68,7 @@ public abstract class JavaImportStatementElementType extends JavaStubElementType for (LighterASTNode child : tree.getChildren(node)) { IElementType type = child.getTokenType(); if (type == JavaElementType.JAVA_CODE_REFERENCE || type == JavaElementType.IMPORT_STATIC_REFERENCE) { - refText = SourceUtil.getReferenceText(tree, child); + refText = JavaSourceUtil.getReferenceText(tree, child); } else if (type == JavaTokenType.ASTERISK) { isOnDemand = true; diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/JavaLightStubBuilder.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/JavaLightStubBuilder.java index ff8c80db3729..3413fda3a3fe 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/JavaLightStubBuilder.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/JavaLightStubBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -44,7 +44,7 @@ public class JavaLightStubBuilder extends LightStubBuilder { if (pkg != null) { LighterASTNode ref = LightTreeUtil.firstChildOfType(tree, pkg, JavaElementType.JAVA_CODE_REFERENCE); if (ref != null) { - refText = SourceUtil.getReferenceText(tree, ref); + refText = JavaSourceUtil.getReferenceText(tree, ref); } } return new PsiJavaFileStubImpl((PsiJavaFile)file, StringRef.fromString(refText), false); diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java index 20ee0c571400..807b42d60383 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java @@ -751,7 +751,7 @@ public class PsiJavaCodeReferenceElementImpl extends CompositePsiElement impleme private String getNormalizedText() { String whiteSpaceAndComments = myCachedNormalizedText; if (whiteSpaceAndComments == null) { - myCachedNormalizedText = whiteSpaceAndComments = SourceUtil.getReferenceText(this); + myCachedNormalizedText = whiteSpaceAndComments = JavaSourceUtil.getReferenceText(this); } return whiteSpaceAndComments; } diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/JavaSourceUtil.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/JavaSourceUtil.java index 8cf5b5bfbdd2..e2af7b08df0d 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/JavaSourceUtil.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/JavaSourceUtil.java @@ -15,13 +15,28 @@ */ package com.intellij.psi.impl.source.tree; +import com.intellij.lang.ASTFactory; import com.intellij.lang.ASTNode; +import com.intellij.lang.LighterAST; +import com.intellij.lang.LighterASTNode; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.psi.*; +import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.psi.impl.source.DummyHolder; import com.intellij.psi.impl.source.SourceJavaCodeReference; +import com.intellij.psi.impl.source.SourceTreeToPsiMap; +import com.intellij.psi.tree.IElementType; +import com.intellij.psi.tree.TokenSet; import com.intellij.util.CharTable; +import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; public class JavaSourceUtil { + private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.tree.JavaSourceUtil"); + + private static final TokenSet REF_FILTER = TokenSet.orSet( + ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET, TokenSet.create(JavaElementType.ANNOTATION)); + private JavaSourceUtil() { } public static void fullyQualifyReference(@NotNull CompositeElement reference, @NotNull PsiClass targetClass) { @@ -61,4 +76,70 @@ public class JavaSourceUtil { } } } + + @NotNull + public static String getReferenceText(@NotNull PsiJavaCodeReferenceElement ref) { + final StringBuilder buffer = new StringBuilder(); + + ((TreeElement)ref.getNode()).acceptTree(new RecursiveTreeElementWalkingVisitor() { + @Override + public void visitLeaf(LeafElement leaf) { + if (!REF_FILTER.contains(leaf.getElementType())) { + String leafText = leaf.getText(); + if (buffer.length() > 0 && !leafText.isEmpty() && Character.isJavaIdentifierPart(leafText.charAt(0))) { + char lastInBuffer = buffer.charAt(buffer.length() - 1); + if (lastInBuffer == '?' || Character.isJavaIdentifierPart(lastInBuffer)) { + buffer.append(" "); + } + } + + buffer.append(leafText); + } + } + + @Override + public void visitComposite(CompositeElement composite) { + if (!REF_FILTER.contains(composite.getElementType())) { + super.visitComposite(composite); + } + } + }); + + return buffer.toString(); + } + + @NotNull + public static String getReferenceText(@NotNull LighterAST tree, @NotNull LighterASTNode node) { + return LightTreeUtil.toFilteredString(tree, node, REF_FILTER); + } + + public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType, + @NotNull TreeElement newChild, + @NotNull PsiManager manager) { + CompositeElement parenthExpr = ASTFactory.composite(parenthType); + + TreeElement dummyExpr = (TreeElement)newChild.clone(); + final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild); + new DummyHolder(manager, parenthExpr, null, charTableByTree); + parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree); + parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "(")); + parenthExpr.rawAddChildren(dummyExpr); + parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")")); + + try { + CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); + PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr)); + parenthExpr = (CompositeElement)SourceTreeToPsiMap.psiToTreeNotNull(formatted); + } + catch (IncorrectOperationException e) { + LOG.error(e); // should not happen + } + + newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild)); + dummyExpr.getTreeParent().replaceChild(dummyExpr, newChild); + + // TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification + TreeUtil.clearCaches(TreeUtil.getFileElement(parenthExpr)); + return parenthExpr; + } } diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/SourceUtil.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/SourceUtil.java index 93e9f60c890e..554580828c0b 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/SourceUtil.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/SourceUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -15,94 +15,29 @@ */ package com.intellij.psi.impl.source.tree; -import com.intellij.lang.ASTFactory; import com.intellij.lang.LighterAST; import com.intellij.lang.LighterASTNode; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.psi.JavaTokenType; -import com.intellij.psi.PsiElement; import com.intellij.psi.PsiJavaCodeReferenceElement; import com.intellij.psi.PsiManager; -import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.psi.impl.source.DummyHolder; -import com.intellij.psi.impl.source.SourceTreeToPsiMap; import com.intellij.psi.tree.IElementType; -import com.intellij.psi.tree.TokenSet; -import com.intellij.util.CharTable; -import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +/** @deprecated use {@link JavaSourceUtil} (to be removed in IDEA 15) */ +@SuppressWarnings("UnusedDeclaration") public class SourceUtil { - private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.tree.SourceUtil"); - - private static final TokenSet REF_FILTER = TokenSet.orSet( - ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET, TokenSet.create(JavaElementType.ANNOTATION)); - private SourceUtil() { } @NotNull public static String getReferenceText(@NotNull PsiJavaCodeReferenceElement ref) { - final StringBuilder buffer = new StringBuilder(); - - ((TreeElement)ref.getNode()).acceptTree(new RecursiveTreeElementWalkingVisitor() { - @Override - public void visitLeaf(LeafElement leaf) { - if (!REF_FILTER.contains(leaf.getElementType())) { - String leafText = leaf.getText(); - if (buffer.length() > 0 && !leafText.isEmpty() && Character.isJavaIdentifierPart(leafText.charAt(0))) { - char lastInBuffer = buffer.charAt(buffer.length() - 1); - if (lastInBuffer == '?' || Character.isJavaIdentifierPart(lastInBuffer)) { - buffer.append(" "); - } - } - - buffer.append(leafText); - } - } - - @Override - public void visitComposite(CompositeElement composite) { - if (!REF_FILTER.contains(composite.getElementType())) { - super.visitComposite(composite); - } - } - }); - - return buffer.toString(); + return JavaSourceUtil.getReferenceText(ref); } @NotNull public static String getReferenceText(@NotNull LighterAST tree, @NotNull LighterASTNode node) { - return LightTreeUtil.toFilteredString(tree, node, REF_FILTER); + return JavaSourceUtil.getReferenceText(tree, node); } - public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType, - @NotNull TreeElement newChild, - @NotNull PsiManager manager) { - CompositeElement parenthExpr = ASTFactory.composite(parenthType); - - TreeElement dummyExpr = (TreeElement)newChild.clone(); - final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild); - new DummyHolder(manager, parenthExpr, null, charTableByTree); - parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree); - parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "(")); - parenthExpr.rawAddChildren(dummyExpr); - parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")")); - - try { - CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); - PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr)); - parenthExpr = (CompositeElement)SourceTreeToPsiMap.psiToTreeNotNull(formatted); - } - catch (IncorrectOperationException e) { - LOG.error(e); // should not happen - } - - newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild)); - dummyExpr.getTreeParent().replaceChild(dummyExpr, newChild); - - // TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification - TreeUtil.clearCaches(TreeUtil.getFileElement(parenthExpr)); - return parenthExpr; + public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType, @NotNull TreeElement newChild, @NotNull PsiManager manager) { + return JavaSourceUtil.addParenthToReplacedChild(parenthType, newChild, manager); } } diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/ExpressionPsiElement.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/ExpressionPsiElement.java index 1d8693efc55c..e0b122e95592 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/ExpressionPsiElement.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/ExpressionPsiElement.java @@ -13,10 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * @author max - */ package com.intellij.psi.impl.source.tree.java; import com.intellij.lang.ASTNode; @@ -24,13 +20,11 @@ import com.intellij.psi.impl.source.tree.*; import com.intellij.psi.tree.IElementType; import org.jetbrains.annotations.NotNull; +/** + * @author max + */ public class ExpressionPsiElement extends CompositePsiElement { - private final int myHC = CompositePsiElement.ourHC++; - - @Override - public final int hashCode() { - return myHC; - } + @SuppressWarnings("AssignmentToStaticFieldFromInstanceMethod") private final int myHC = CompositePsiElement.ourHC++; public ExpressionPsiElement(final IElementType type) { super(type); @@ -42,9 +36,14 @@ public class ExpressionPsiElement extends CompositePsiElement { ElementType.EXPRESSION_BIT_SET.contains(newElement.getElementType())) { boolean needParenth = ReplaceExpressionUtil.isNeedParenthesis(child, newElement); if (needParenth) { - newElement = SourceUtil.addParenthToReplacedChild(JavaElementType.PARENTH_EXPRESSION, newElement, getManager()); + newElement = JavaSourceUtil.addParenthToReplacedChild(JavaElementType.PARENTH_EXPRESSION, newElement, getManager()); } } super.replaceChildInternal(child, newElement); } + + @Override + public final int hashCode() { + return myHC; + } } diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiPackageStatementImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiPackageStatementImpl.java index 5a42715590ef..29d072970560 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiPackageStatementImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiPackageStatementImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -38,7 +38,7 @@ public class PsiPackageStatementImpl extends CompositePsiElement implements PsiP @Override public String getPackageName() { PsiJavaCodeReferenceElement ref = getPackageReference(); - return ref == null ? null : SourceUtil.getReferenceText(ref); + return ref == null ? null : JavaSourceUtil.getReferenceText(ref); } @Override diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java index 02ffaa6f3c20..8dd43933c660 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiReferenceExpressionImpl.java @@ -761,7 +761,7 @@ public class PsiReferenceExpressionImpl extends PsiReferenceExpressionBase imple private String getCachedNormalizedText() { String whiteSpaceAndComments = myCachedNormalizedText; if (whiteSpaceAndComments == null) { - myCachedNormalizedText = whiteSpaceAndComments = SourceUtil.getReferenceText(this); + myCachedNormalizedText = whiteSpaceAndComments = JavaSourceUtil.getReferenceText(this); } return whiteSpaceAndComments; } From 72a3ffa88fdc6a3403f6cba9d84941eafbddc794 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 21 Jul 2014 19:03:09 +0200 Subject: [PATCH 2/2] Cleanup (formatting) --- .../src/com/intellij/psi/impl/source/tree/JavaElementType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/JavaElementType.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/JavaElementType.java index 4fbbad26964d..8d42dbeb4f90 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/JavaElementType.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/JavaElementType.java @@ -84,8 +84,8 @@ public interface JavaElementType { IElementType PARAMETER_LIST = JavaStubElementTypes.PARAMETER_LIST; IElementType EXTENDS_BOUND_LIST = JavaStubElementTypes.EXTENDS_BOUND_LIST; IElementType THROWS_LIST = JavaStubElementTypes.THROWS_LIST; - IElementType LITERAL_EXPRESSION = new JavaCompositeElementType("LITERAL_EXPRESSION", PsiLiteralExpressionImpl.class); + IElementType LITERAL_EXPRESSION = new JavaCompositeElementType("LITERAL_EXPRESSION", PsiLiteralExpressionImpl.class); IElementType IMPORT_STATIC_REFERENCE = new JavaCompositeElementType("IMPORT_STATIC_REFERENCE", PsiImportStaticReferenceElementImpl.class); IElementType TYPE = new JavaCompositeElementType("TYPE", PsiTypeElementImpl.class); IElementType DIAMOND_TYPE = new JavaCompositeElementType("DIAMOND_TYPE", PsiDiamondTypeElementImpl.class);