IDEA-86300 Create local variable intention implemented

This commit is contained in:
Danila Ponomarenko
2012-06-07 17:56:05 +04:00
parent ac5b270ef6
commit 389ffea2d7
10 changed files with 153 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
/*
* 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.CodeInsightBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.JavaRefactoringActionHandlerFactory;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Danila Ponomarenko
*/
public class IntroduceVariableAction implements IntentionAction {
@NotNull
@Override
public String getText() {
return CodeInsightBundle.message("intention.introduce.variable.text");
}
@NotNull
@Override
public String getFamilyName() {
return getText();
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
final PsiElement element = getElement(editor, file);
if (element == null) {
return false;
}
final PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class, false);
if (statement == null || !(statement instanceof PsiExpressionStatement)) {
return false;
}
final PsiExpressionStatement expressionStatement = (PsiExpressionStatement)statement;
final PsiExpression expression = expressionStatement.getExpression();
return expression.getType() != PsiType.VOID && !(expression instanceof PsiAssignmentExpression);
}
@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 {
final RefactoringActionHandler refactoringActionHandler = JavaRefactoringActionHandlerFactory.getInstance().createIntroduceVariableHandler();
refactoringActionHandler.invoke(project, editor, file, null);
}
@Override
public boolean startInWriteAction() {
return true;
}
}

View File

@@ -0,0 +1,9 @@
// "Introduce local variable" "false"
class a {
void a() {
int i;
i = Integer.par<caret>seInt("10");
}
}

View File

@@ -0,0 +1,8 @@
// "Introduce local variable" "false"
class a {
void a() {
int i = Integer.par<caret>seInt("10");
}
}

View File

@@ -0,0 +1,9 @@
// "Introduce local variable" "false"
class a {
void a() {
a<caret>();
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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;
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
/**
* @author Danila Ponomarenko
*/
public class IntroduceVariableTest extends LightIntentionActionTestCase {
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/introduceVariable";
}
}

View File

@@ -156,6 +156,7 @@ intention.make.type.generic.family=Make Type Generic
intention.make.type.generic.text=Change type of {0} to {1}
intention.split.if.family=Split If
intention.split.if.text=Split into 2 if's
intention.introduce.variable.text=Introduce local variable
intention.implement.abstract.method.family=Implement Abstract Method
intention.implement.abstract.method.text=Implement method ''{0}''
intention.override.method.text=Override method ''{0}''

View File

@@ -0,0 +1 @@
int i = Integer.parseInt("1");

View File

@@ -0,0 +1 @@
<spot>Integer.parseInt("1")</spot>;

View File

@@ -0,0 +1,5 @@
<html>
<body>
This intention puts a result of the expression into a variable.
</body>
</html>

View File

@@ -635,6 +635,10 @@
<className>com.intellij.codeInsight.intention.impl.AddOverrideAnnotationAction</className>
<category>Declaration</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.IntroduceVariableAction</className>
<category>Declaration</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.daemon.impl.quickfix.DelegateWithDefaultParamValueIntentionAction</className>