From 9d546e47fa590f16e48ffcad7791b71abcac3a77 Mon Sep 17 00:00:00 2001 From: Danila Ponomarenko Date: Thu, 21 Jun 2012 16:29:45 +0400 Subject: [PATCH] SyntheticElement check added --- .../intention/impl/BaseRefactoringAction.java | 54 +++++++++++++++++++ .../impl/EncapsulateFieldAction.java | 21 +++----- .../IntroduceVariableIntentionAction.java | 19 +------ ...ringAction.java => RefactoringAction.java} | 15 +----- .../intention/impl/RunRefactoringAction.java | 14 ++++- 5 files changed, 76 insertions(+), 47 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseRefactoringAction.java rename java/java-impl/src/com/intellij/codeInsight/intention/impl/{BaseRunRefactoringAction.java => RefactoringAction.java} (70%) diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseRefactoringAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseRefactoringAction.java new file mode 100644 index 000000000000..79341c634aea --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseRefactoringAction.java @@ -0,0 +1,54 @@ +/* + * Copyright 2000-2012 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.codeInsight.intention.impl; + +import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.SyntheticElement; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; + +/** + * @author Danila Ponomarenko + */ +public abstract class BaseRefactoringAction extends PsiElementBaseIntentionAction implements RefactoringAction{ + @Override + public final boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + return super.isAvailable(project, editor, file); + } + + @Override + public final boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { + return !(element instanceof SyntheticElement) && isAvailableOverride(project, editor, element); + } + + protected abstract boolean isAvailableOverride(@NotNull Project project, Editor editor, @NotNull PsiElement element); + + @Override + public final boolean startInWriteAction() { + return false; + } + + @Override + public final Icon getIcon(int flags) { + return REFACTORING_BULB; + } +} \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/EncapsulateFieldAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/EncapsulateFieldAction.java index cc6f7bee9a25..73d10c8ef713 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/EncapsulateFieldAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/EncapsulateFieldAction.java @@ -16,7 +16,6 @@ package com.intellij.codeInsight.intention.impl; import com.intellij.codeInsight.CodeInsightBundle; -import com.intellij.openapi.editor.CaretModel; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.*; @@ -28,7 +27,7 @@ import org.jetbrains.annotations.Nullable; /** * @author Danila Ponomarenko */ -public class EncapsulateFieldAction extends BaseRunRefactoringAction { +public class EncapsulateFieldAction extends BaseRefactoringAction { @NotNull @Override @@ -43,15 +42,14 @@ public class EncapsulateFieldAction extends BaseRunRefactoringAction { } @Override - public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { - final PsiField field = getField(getElement(editor, file)); + protected boolean isAvailableOverride(@NotNull Project project, Editor editor, @NotNull PsiElement element) { + final PsiField field = getField(element); return field != null && !field.hasModifierProperty(PsiModifier.FINAL) && !field.hasModifierProperty(PsiModifier.PRIVATE); } - @Override - public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { - final PsiField field = getField(getElement(editor, file)); + public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { + final PsiField field = getField(element); if (field == null) { return; } @@ -59,6 +57,7 @@ public class EncapsulateFieldAction extends BaseRunRefactoringAction { new EncapsulateFieldsHandler().invoke(project, new PsiElement[]{field}, null); } + @Nullable protected static PsiField getField(@Nullable PsiElement element) { if (element == null || !(element instanceof PsiIdentifier)) { @@ -81,12 +80,4 @@ public class EncapsulateFieldAction extends BaseRunRefactoringAction { } return (PsiField)resolved; } - - @Nullable - protected static PsiElement getElement(Editor editor, @NotNull PsiFile file) { - if (!file.getManager().isInProject(file)) return null; - final CaretModel caretModel = editor.getCaretModel(); - final int position = caretModel.getOffset(); - return file.findElementAt(position); - } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/IntroduceVariableIntentionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/IntroduceVariableIntentionAction.java index 5fe4582ece56..1fe33e453a66 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/IntroduceVariableIntentionAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/IntroduceVariableIntentionAction.java @@ -16,7 +16,6 @@ package com.intellij.codeInsight.intention.impl; import com.intellij.codeInsight.CodeInsightBundle; -import com.intellij.openapi.editor.CaretModel; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.*; @@ -29,8 +28,7 @@ import org.jetbrains.annotations.Nullable; /** * @author Danila Ponomarenko */ -public class IntroduceVariableIntentionAction extends BaseRunRefactoringAction { - +public class IntroduceVariableIntentionAction extends BaseRefactoringAction { @NotNull @Override public String getText() { @@ -44,12 +42,7 @@ public class IntroduceVariableIntentionAction extends BaseRunRefactoringAction { } @Override - public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { - final PsiElement element = getElement(editor, file); - if (element == null) { - return false; - } - + protected boolean isAvailableOverride(@NotNull Project project, Editor editor, @NotNull PsiElement element) { final PsiExpression expression = getExpression(element); if (expression == null || !(expression.getParent() instanceof PsiExpressionStatement)) { return false; @@ -67,14 +60,6 @@ public class IntroduceVariableIntentionAction extends BaseRunRefactoringAction { return expression; } - @Nullable - protected static PsiElement getElement(Editor editor, @NotNull PsiFile file) { - if (!file.getManager().isInProject(file)) return null; - final CaretModel caretModel = editor.getCaretModel(); - final int position = caretModel.getOffset(); - return file.findElementAt(position); - } - @Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { new IntroduceVariableHandler().invoke(project, editor, file, null); diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseRunRefactoringAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/RefactoringAction.java similarity index 70% rename from java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseRunRefactoringAction.java rename to java/java-impl/src/com/intellij/codeInsight/intention/impl/RefactoringAction.java index 01678b5fceb5..b581af515168 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseRunRefactoringAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/RefactoringAction.java @@ -15,29 +15,16 @@ */ package com.intellij.codeInsight.intention.impl; -import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInsight.intention.LowPriorityAction; import com.intellij.icons.AllIcons; import com.intellij.openapi.util.Iconable; -import com.intellij.refactoring.RefactoringActionHandler; -import org.jetbrains.annotations.NotNull; import javax.swing.*; /** * @author Danila Ponomarenko */ -public abstract class BaseRunRefactoringAction implements IntentionAction, Iconable, LowPriorityAction { +public interface RefactoringAction extends IntentionAction, Iconable, LowPriorityAction { public static final Icon REFACTORING_BULB = AllIcons.Actions.RefactoringBulb; - - @Override - public final boolean startInWriteAction() { - return false; - } - - @Override - public final Icon getIcon(int flags) { - return REFACTORING_BULB; - } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/RunRefactoringAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/RunRefactoringAction.java index eaffe844dda9..2cd350667d25 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/RunRefactoringAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/RunRefactoringAction.java @@ -22,11 +22,13 @@ import com.intellij.refactoring.RefactoringActionHandler; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +import javax.swing.*; + /** * User: anna * Date: 9/5/11 */ -public class RunRefactoringAction extends BaseRunRefactoringAction { +public class RunRefactoringAction implements RefactoringAction { private final RefactoringActionHandler myHandler; private final String myCommandName; @@ -56,4 +58,14 @@ public class RunRefactoringAction extends BaseRunRefactoringAction { public final void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { myHandler.invoke(project, editor, file, null); } + + @Override + public boolean startInWriteAction() { + return false; + } + + @Override + public Icon getIcon(@IconFlags int flags) { + return REFACTORING_BULB; + } }