diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/ExpandStaticImportAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/ExpandStaticImportAction.java index a90c1d0b360f..0d725bcf91ab 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/ExpandStaticImportAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/ExpandStaticImportAction.java @@ -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 expressionToExpand = new ArrayList(); - 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 expressionToExpand = collectReferencesThrough(file, refExpr, staticImport); if (expressionToExpand.isEmpty()) { @@ -129,33 +107,6 @@ public class ExpandStaticImportAction extends PsiElementBaseIntentionAction { } } - private static void replaceAllAndDeleteImport(List expressionToExpand, - PsiJavaCodeReferenceElement refExpr, - PsiImportStaticStatement staticImport) { - expressionToExpand.add(refExpr); - Collections.sort(expressionToExpand, new Comparator() { - @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); diff --git a/java/java-impl/src/com/intellij/refactoring/inline/InlineStaticImportHandler.java b/java/java-impl/src/com/intellij/refactoring/inline/InlineStaticImportHandler.java new file mode 100644 index 000000000000..6abc17b97152 --- /dev/null +++ b/java/java-impl/src/com/intellij/refactoring/inline/InlineStaticImportHandler.java @@ -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 referenceElements = + collectReferencesThrough(element.getContainingFile(), null, staticStatement); + new WriteCommandAction(project, REFACTORING_NAME){ + @Override + protected void run(Result result) throws Throwable { + replaceAllAndDeleteImport(referenceElements, null, staticStatement); + } + }.execute(); + } +} diff --git a/java/openapi/src/com/intellij/psi/util/ImportsUtil.java b/java/openapi/src/com/intellij/psi/util/ImportsUtil.java new file mode 100644 index 000000000000..7b61ade1a8f2 --- /dev/null +++ b/java/openapi/src/com/intellij/psi/util/ImportsUtil.java @@ -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 collectReferencesThrough(PsiFile file, + @Nullable final PsiJavaCodeReferenceElement refExpr, + final PsiImportStaticStatement staticImport) { + final List expressionToExpand = new ArrayList(); + 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 expressionToExpand, + @Nullable PsiJavaCodeReferenceElement refExpr, + PsiImportStaticStatement staticImport) { + if (refExpr != null) { + expressionToExpand.add(refExpr); + } + Collections.sort(expressionToExpand, new Comparator() { + @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)); + } + } +} diff --git a/platform/lang-impl/src/com/intellij/refactoring/inline/InlineRefactoringActionHandler.java b/platform/lang-impl/src/com/intellij/refactoring/inline/InlineRefactoringActionHandler.java index db3ccfa481a9..02099b506a84 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/inline/InlineRefactoringActionHandler.java +++ b/platform/lang-impl/src/com/intellij/refactoring/inline/InlineRefactoringActionHandler.java @@ -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)) { diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index a4d3b846c2c2..269b0f2b759d 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -901,6 +901,7 @@ +