mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: Moved reusable bits of code from the inspection quick fix to the API (IDEA-163139)
This commit is contained in:
+1
-28
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user