mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
inline import static statement
This commit is contained in:
+4
-53
@@ -15,37 +15,27 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.codeInsight.highlighting.HighlightManager;
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.colors.EditorColors;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.popup.JBPopupFactory;
|
||||
import com.intellij.openapi.ui.popup.PopupChooserBuilder;
|
||||
import com.intellij.openapi.ui.popup.PopupStep;
|
||||
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.ImportsUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.StringStack;
|
||||
import gnu.trove.TIntArrayList;
|
||||
import gnu.trove.TIntProcedure;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.psi.util.ImportsUtil.*;
|
||||
|
||||
public class ExpandStaticImportAction extends PsiElementBaseIntentionAction {
|
||||
private static final Logger LOG = Logger.getInstance("#" + ExpandStaticImportAction.class.getName());
|
||||
private static final String REPLACE_THIS_OCCURRENCE = "Replace this occurrence and keep the method";
|
||||
@@ -81,19 +71,7 @@ public class ExpandStaticImportAction extends PsiElementBaseIntentionAction {
|
||||
final PsiImportStaticStatement staticImport = (PsiImportStaticStatement)refExpr.advancedResolve(true).getCurrentFileResolveScope();
|
||||
|
||||
|
||||
final List<PsiJavaCodeReferenceElement> expressionToExpand = new ArrayList<PsiJavaCodeReferenceElement>();
|
||||
file.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitReferenceElement(PsiJavaCodeReferenceElement expression) {
|
||||
if (refExpr != expression) {
|
||||
final PsiElement resolveScope = expression.advancedResolve(true).getCurrentFileResolveScope();
|
||||
if (resolveScope == staticImport) {
|
||||
expressionToExpand.add(expression);
|
||||
}
|
||||
}
|
||||
super.visitElement(expression);
|
||||
}
|
||||
});
|
||||
final List<PsiJavaCodeReferenceElement> expressionToExpand = collectReferencesThrough(file, refExpr, staticImport);
|
||||
|
||||
|
||||
if (expressionToExpand.isEmpty()) {
|
||||
@@ -129,33 +107,6 @@ public class ExpandStaticImportAction extends PsiElementBaseIntentionAction {
|
||||
}
|
||||
}
|
||||
|
||||
private static void replaceAllAndDeleteImport(List<PsiJavaCodeReferenceElement> expressionToExpand,
|
||||
PsiJavaCodeReferenceElement refExpr,
|
||||
PsiImportStaticStatement staticImport) {
|
||||
expressionToExpand.add(refExpr);
|
||||
Collections.sort(expressionToExpand, new Comparator<PsiJavaCodeReferenceElement>() {
|
||||
@Override
|
||||
public int compare(PsiJavaCodeReferenceElement o1, PsiJavaCodeReferenceElement o2) {
|
||||
return o2.getTextOffset() - o1.getTextOffset();
|
||||
}
|
||||
});
|
||||
for (PsiJavaCodeReferenceElement expression : expressionToExpand) {
|
||||
expand(expression, staticImport);
|
||||
}
|
||||
staticImport.delete();
|
||||
}
|
||||
|
||||
private static void expand(PsiJavaCodeReferenceElement refExpr, PsiImportStaticStatement staticImport) {
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(refExpr.getProject());
|
||||
final PsiReferenceExpression referenceExpression = elementFactory.createReferenceExpression(staticImport.resolveTargetClass());
|
||||
if (refExpr instanceof PsiReferenceExpression) {
|
||||
((PsiReferenceExpression)refExpr).setQualifierExpression(referenceExpression);
|
||||
}
|
||||
else {
|
||||
refExpr.replace(elementFactory.createReferenceFromText(referenceExpression.getText() + "." + refExpr.getText(), refExpr));
|
||||
}
|
||||
}
|
||||
|
||||
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
|
||||
invoke(project, file, editor, element);
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2000-2011 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.refactoring.inline;
|
||||
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiImportStaticStatement;
|
||||
import com.intellij.psi.PsiJavaCodeReferenceElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.psi.util.ImportsUtil.collectReferencesThrough;
|
||||
import static com.intellij.psi.util.ImportsUtil.replaceAllAndDeleteImport;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
* Date: 9/1/11
|
||||
*/
|
||||
public class InlineStaticImportHandler extends JavaInlineActionHandler {
|
||||
|
||||
private static final String REFACTORING_NAME = "Expand static import";
|
||||
|
||||
@Override
|
||||
public boolean canInlineElement(PsiElement element) {
|
||||
if (element.getContainingFile() == null) return false;
|
||||
return PsiTreeUtil.getParentOfType(element, PsiImportStaticStatement.class) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inlineElement(Project project, Editor editor, PsiElement element) {
|
||||
final PsiImportStaticStatement staticStatement = PsiTreeUtil.getParentOfType(element, PsiImportStaticStatement.class);
|
||||
final List<PsiJavaCodeReferenceElement> referenceElements =
|
||||
collectReferencesThrough(element.getContainingFile(), null, staticStatement);
|
||||
new WriteCommandAction(project, REFACTORING_NAME){
|
||||
@Override
|
||||
protected void run(Result result) throws Throwable {
|
||||
replaceAllAndDeleteImport(referenceElements, null, staticStatement);
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2000-2011 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.util;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
* Date: 9/1/11
|
||||
*/
|
||||
public class ImportsUtil {
|
||||
private ImportsUtil() {
|
||||
}
|
||||
|
||||
public static List<PsiJavaCodeReferenceElement> collectReferencesThrough(PsiFile file,
|
||||
@Nullable final PsiJavaCodeReferenceElement refExpr,
|
||||
final PsiImportStaticStatement staticImport) {
|
||||
final List<PsiJavaCodeReferenceElement> expressionToExpand = new ArrayList<PsiJavaCodeReferenceElement>();
|
||||
file.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitReferenceElement(PsiJavaCodeReferenceElement expression) {
|
||||
if (refExpr == null || refExpr != expression) {
|
||||
final PsiElement resolveScope = expression.advancedResolve(true).getCurrentFileResolveScope();
|
||||
if (resolveScope == staticImport) {
|
||||
expressionToExpand.add(expression);
|
||||
}
|
||||
}
|
||||
super.visitElement(expression);
|
||||
}
|
||||
});
|
||||
return expressionToExpand;
|
||||
}
|
||||
|
||||
public static void replaceAllAndDeleteImport(List<PsiJavaCodeReferenceElement> expressionToExpand,
|
||||
@Nullable PsiJavaCodeReferenceElement refExpr,
|
||||
PsiImportStaticStatement staticImport) {
|
||||
if (refExpr != null) {
|
||||
expressionToExpand.add(refExpr);
|
||||
}
|
||||
Collections.sort(expressionToExpand, new Comparator<PsiJavaCodeReferenceElement>() {
|
||||
@Override
|
||||
public int compare(PsiJavaCodeReferenceElement o1, PsiJavaCodeReferenceElement o2) {
|
||||
return o2.getTextOffset() - o1.getTextOffset();
|
||||
}
|
||||
});
|
||||
for (PsiJavaCodeReferenceElement expression : expressionToExpand) {
|
||||
expand(expression, staticImport);
|
||||
}
|
||||
staticImport.delete();
|
||||
}
|
||||
|
||||
public static void expand(@NotNull PsiJavaCodeReferenceElement refExpr, PsiImportStaticStatement staticImport) {
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(refExpr.getProject());
|
||||
final PsiReferenceExpression referenceExpression = elementFactory.createReferenceExpression(staticImport.resolveTargetClass());
|
||||
if (refExpr instanceof PsiReferenceExpression) {
|
||||
((PsiReferenceExpression)refExpr).setQualifierExpression(referenceExpression);
|
||||
}
|
||||
else {
|
||||
refExpr.replace(elementFactory.createReferenceFromText(referenceExpression.getText() + "." + refExpr.getText(), refExpr));
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
@@ -36,6 +36,7 @@ import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.actions.BaseRefactoringAction;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -66,6 +67,9 @@ public class InlineRefactoringActionHandler implements RefactoringActionHandler
|
||||
editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
|
||||
|
||||
PsiElement element = LangDataKeys.PSI_ELEMENT.getData(dataContext);
|
||||
if (element == null) {
|
||||
element = BaseRefactoringAction.getElementAtCaret(editor, file);
|
||||
}
|
||||
if (element != null) {
|
||||
for(InlineActionHandler handler: Extensions.getExtensions(InlineActionHandler.EP_NAME)) {
|
||||
if (handler.canInlineElementInEditor(element)) {
|
||||
|
||||
@@ -901,6 +901,7 @@
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlineMethodHandler"/>
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlineLocalHandler"/>
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlineConstantFieldHandler"/>
|
||||
<inlineActionHandler implementation="com.intellij.refactoring.inline.InlineStaticImportHandler"/>
|
||||
|
||||
<previewHintProvider implementation="com.intellij.codeInsight.preview.JavaPreviewHintProvider"/>
|
||||
<filePropertyPusher implementation="com.intellij.openapi.roots.impl.JavaLanguageLevelPusher"/>
|
||||
|
||||
Reference in New Issue
Block a user