SyntheticElement check added

This commit is contained in:
Danila Ponomarenko
2012-06-24 18:24:04 +04:00
parent b500c9f1fb
commit 9d546e47fa
5 changed files with 76 additions and 47 deletions
@@ -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;
}
}
@@ -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);
}
}
@@ -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);
@@ -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;
}
}
@@ -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;
}
}