intention to replace assignment with void-call (IDEA-143069)

This commit is contained in:
Anna Kozlova
2015-10-01 18:31:50 +02:00
parent 0bc58af9e6
commit a0ce4457ae
7 changed files with 154 additions and 5 deletions

View File

@@ -60,10 +60,7 @@ import com.intellij.util.ui.UIUtil;
import com.intellij.xml.util.XmlStringUtil;
import gnu.trove.THashMap;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.PropertyKey;
import org.jetbrains.annotations.*;
import java.util.*;
import java.util.regex.Matcher;
@@ -485,7 +482,7 @@ public class HighlightUtil extends HighlightUtilBase {
private static void registerChangeVariableTypeFixes(@NotNull PsiExpression expression,
@NotNull PsiType type,
@Nullable PsiExpression lExpr,
@Nullable final PsiExpression lExpr,
@Nullable HighlightInfo highlightInfo) {
if (highlightInfo == null || !(expression instanceof PsiReferenceExpression)) return;
@@ -493,6 +490,16 @@ public class HighlightUtil extends HighlightUtilBase {
if (!(element instanceof PsiVariable)) return;
registerChangeVariableTypeFixes((PsiVariable)element, type, lExpr, highlightInfo);
if (lExpr instanceof PsiMethodCallExpression && lExpr.getParent() instanceof PsiAssignmentExpression) {
final PsiElement parent = lExpr.getParent();
if (parent.getParent() instanceof PsiStatement) {
final PsiMethod method = ((PsiMethodCallExpression)lExpr).resolveMethod();
if (method != null && PsiType.VOID.equals(method.getReturnType())) {
QuickFixAction.registerQuickFixAction(highlightInfo, new ReplaceAssignmentFromVoidWithStatementIntentionAction(parent, lExpr));
}
}
}
}
private static boolean isCastIntentionApplicable(@NotNull PsiExpression expression, @Nullable PsiType toType) {

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2000-2015 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.daemon.impl.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
public class ReplaceAssignmentFromVoidWithStatementIntentionAction implements IntentionAction {
private final PsiElement myParent;
private final PsiExpression myLExpr;
public ReplaceAssignmentFromVoidWithStatementIntentionAction(PsiElement parent, PsiExpression lExpr) {
myParent = parent;
myLExpr = lExpr;
}
@Nls
@NotNull
@Override
public String getText() {
return getFamilyName();
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return "Remove left side of assignment";
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
myParent.replace(myLExpr);
}
@Override
public boolean startInWriteAction() {
return true;
}
}

View File

@@ -0,0 +1,11 @@
// "Remove left side of assignment" "true"
class FooBar {
public int baz;
{
bar();
}
void bar() {}
}

View File

@@ -0,0 +1,11 @@
// "Remove left side of assignment" "false"
class FooBar {
public String baz;
{
baz = <caret>bar();
}
int bar() { return 0;}
}

View File

@@ -0,0 +1,11 @@
// "Remove left side of assignment" "true"
class FooBar {
public int baz;
{
baz = <caret>bar();
}
void bar() {}
}

View File

@@ -0,0 +1,13 @@
// "Remove left side of assignment" "false"
class FooBar {
public int baz;
{
foo(baz = <caret>bar());
}
void foo(int i) {}
void bar() {}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2000-2015 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.daemon.quickFix;
public class ReplaceAssignmentFromVoidFixTest extends LightQuickFixParameterizedTestCase {
public void test() throws Exception {
doAllTests();
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/replaceAssignment";
}
}