From f920e7b65a17d31406e91e52a2148c6c79b9fa2e Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Tue, 6 Dec 2016 19:04:12 +0300 Subject: [PATCH] Java: Moved reusable bits of code from the inspection quick fix to the API (IDEA-163139) --- ...oduleExportsPackageToItselfInspection.java | 29 +------- .../impl/java/stubs/JavaStubElementTypes.java | 10 ++- .../psi/impl/source/ExportsElement.java | 66 +++++++++++++++++++ 3 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 java/java-psi-impl/src/com/intellij/psi/impl/source/ExportsElement.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9ModuleExportsPackageToItselfInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9ModuleExportsPackageToItselfInspection.java index 96cd9ad2130f..6739cfbc096d 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9ModuleExportsPackageToItselfInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/java19modules/Java9ModuleExportsPackageToItselfInspection.java @@ -24,7 +24,6 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.List; @@ -95,34 +94,8 @@ public class Java9ModuleExportsPackageToItselfInspection extends BaseJavaLocalIn @Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement psiElement = descriptor.getPsiElement(); - if (!FileModificationService.getInstance().prepareFileForWrite(psiElement.getContainingFile())) return; - - PsiElement comma = findNearestComma(psiElement); - if (comma != null) { - comma.delete(); - } - else { - PsiKeyword keyword = PsiTreeUtil.getPrevSiblingOfType(psiElement, PsiKeyword.class); - if (keyword != null && keyword.getTokenType() == JavaTokenType.TO_KEYWORD) { - keyword.delete(); - } - } + if (!FileModificationService.getInstance().preparePsiElementForWrite(psiElement)) return; psiElement.delete(); } - - @Nullable - private static PsiElement findNearestComma(PsiElement psiElement) { - for (PsiElement next = psiElement.getNextSibling(); next != null; next = next.getNextSibling()) { - if (next instanceof PsiJavaToken && ((PsiJavaToken)next).getTokenType() == JavaTokenType.COMMA) { - return next; - } - } - for (PsiElement prev = psiElement.getPrevSibling(); prev != null; prev = prev.getPrevSibling()) { - if (prev instanceof PsiJavaToken && ((PsiJavaToken)prev).getTokenType() == JavaTokenType.COMMA) { - return prev; - } - } - return null; - } } } diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/java/stubs/JavaStubElementTypes.java b/java/java-psi-impl/src/com/intellij/psi/impl/java/stubs/JavaStubElementTypes.java index be56a34a5166..024e5d63e167 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/java/stubs/JavaStubElementTypes.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/java/stubs/JavaStubElementTypes.java @@ -16,6 +16,7 @@ package com.intellij.psi.impl.java.stubs; import com.intellij.lang.ASTNode; +import com.intellij.psi.impl.source.ExportsElement; import com.intellij.psi.impl.source.JavaFileElementType; import com.intellij.psi.impl.source.tree.java.*; import com.intellij.psi.tree.IStubFileElementType; @@ -40,7 +41,14 @@ public interface JavaStubElementTypes { JavaImportListElementType IMPORT_LIST = new JavaImportListElementType(); JavaModuleElementType MODULE = new JavaModuleElementType(); JavaRequiresStatementElementType REQUIRES_STATEMENT = new JavaRequiresStatementElementType(); - JavaExportsStatementElementType EXPORTS_STATEMENT = new JavaExportsStatementElementType(); + + JavaExportsStatementElementType EXPORTS_STATEMENT = new JavaExportsStatementElementType() { + @NotNull + @Override + public ASTNode createCompositeNode() { + return new ExportsElement(); + } + }; JavaClassElementType CLASS = new JavaClassElementType("CLASS") { @NotNull diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/ExportsElement.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/ExportsElement.java new file mode 100644 index 000000000000..47ec8dff3473 --- /dev/null +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/ExportsElement.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2016 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.impl.source; + +import com.intellij.lang.ASTNode; +import com.intellij.psi.JavaTokenType; +import com.intellij.psi.impl.PsiImplUtil; +import com.intellij.psi.impl.java.stubs.JavaStubElementTypes; +import com.intellij.psi.impl.source.tree.CompositeElement; +import com.intellij.psi.impl.source.tree.JavaElementType; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Pavel.Dolgov + */ +public class ExportsElement extends CompositeElement { + public ExportsElement() { + super(JavaStubElementTypes.EXPORTS_STATEMENT); + } + + @Override + public void deleteChildInternal(@NotNull ASTNode child) { + if (child.getElementType() == JavaElementType.MODULE_REFERENCE) { + ASTNode comma = findNearestComma(child); + if (comma != null) { + super.deleteChildInternal(comma); + } + else { + ASTNode toKeyword = findChildByType(JavaTokenType.TO_KEYWORD); + if (toKeyword != null) { + super.deleteChildInternal(toKeyword); + } + } + } + super.deleteChildInternal(child); + } + + @Nullable + private static ASTNode findNearestComma(@NotNull ASTNode child) { + ASTNode next = PsiImplUtil.skipWhitespaceAndComments(child.getTreeNext()); + if (next != null && next.getElementType() == JavaTokenType.COMMA) { + return next; + } + else { + ASTNode prev = PsiImplUtil.skipWhitespaceAndCommentsBack(child.getTreePrev()); + if (prev != null && prev.getElementType() == JavaTokenType.COMMA) { + return prev; + } + } + return null; + } +}