Skip to content

Commit 679d612

Browse files
committed
Add 'replace with return' quick fix
This change adds a 'replace with return' quick fix and addresses #2002.
1 parent 4c669ec commit 679d612

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

src/com/goide/inspections/GoContinueNotInLoopInspection.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@
1919
import com.goide.psi.GoContinueStatement;
2020
import com.goide.psi.GoForStatement;
2121
import com.goide.psi.GoVisitor;
22-
import com.intellij.codeInspection.LocalInspectionToolSession;
23-
import com.intellij.codeInspection.ProblemHighlightType;
24-
import com.intellij.codeInspection.ProblemsHolder;
22+
import com.goide.psi.impl.GoElementFactory;
23+
import com.intellij.codeInspection.*;
24+
import com.intellij.openapi.command.WriteCommandAction;
25+
import com.intellij.openapi.project.Project;
26+
import com.intellij.psi.PsiElement;
2527
import com.intellij.psi.util.PsiTreeUtil;
2628
import org.jetbrains.annotations.NotNull;
2729

2830
/**
2931
* Raises an error when a 'continue' statement is found outside of a for loop.
3032
*/
3133
public class GoContinueNotInLoopInspection extends GoInspectionBase {
34+
public static String QUICK_FIX_NAME = "Replace with 'return'";
35+
3236
@NotNull
3337
@Override
3438
protected GoVisitor buildGoVisitor(@NotNull final ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
@@ -37,9 +41,28 @@ protected GoVisitor buildGoVisitor(@NotNull final ProblemsHolder holder, @NotNul
3741
public void visitContinueStatement(@NotNull GoContinueStatement o) {
3842
if (PsiTreeUtil.getParentOfType(o.getContinue(), GoForStatement.class) == null) {
3943
holder
40-
.registerProblem(o.getContinue(), "Continue statement not inside a for loop.", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
44+
.registerProblem(o.getContinue(), "Continue statement not inside a for loop.", ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
45+
new ReplaceWithReturnQuickFix());
4146
}
4247
}
4348
};
4449
}
50+
51+
private static class ReplaceWithReturnQuickFix extends LocalQuickFixBase {
52+
private ReplaceWithReturnQuickFix() {
53+
super(QUICK_FIX_NAME);
54+
}
55+
56+
@Override
57+
public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
58+
final PsiElement element = descriptor.getPsiElement();
59+
if (element == null) return;
60+
new WriteCommandAction.Simple(project, getName(), element.getContainingFile()) {
61+
@Override
62+
protected void run() throws Throwable {
63+
element.replace(GoElementFactory.createReturnStatement(project));
64+
}
65+
}.execute();
66+
}
67+
}
4568
}

src/com/goide/psi/impl/GoElementFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ private static GoFile createFileFromText(@NotNull Project project, @NotNull Stri
3838
return (GoFile)PsiFileFactory.getInstance(project).createFileFromText("a.go", GoLanguage.INSTANCE, text);
3939
}
4040

41+
@NotNull
42+
public static PsiElement createReturnStatement(@NotNull Project project) {
43+
GoFile file = createFileFromText(project, "package main\nfunc _() { return; }");
44+
return PsiTreeUtil.findChildOfType(file, GoReturnStatement.class);
45+
}
46+
4147
@NotNull
4248
public static PsiElement createIdentifierFromText(@NotNull Project project, String text) {
4349
GoFile file = createFileFromText(project, "package " + text);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package continue_outside_loop
2+
3+
func _() {
4+
return
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package continue_outside_loop
2+
3+
func _() {
4+
c<caret>ontinue
5+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2013-2015 Sergey Ignatov, Alexander Zolotov, Florin Patan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.goide.quickfix;
18+
19+
import com.goide.inspections.GoContinueNotInLoopInspection;
20+
import com.goide.inspections.GoRenameToBlankQuickFix;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
public class GoContinueNotInLoopQuickFixTest extends GoQuickFixTestBase {
24+
@Override
25+
protected void setUp() throws Exception {
26+
super.setUp();
27+
myFixture.enableInspections(GoContinueNotInLoopInspection.class);
28+
}
29+
30+
@NotNull
31+
@Override
32+
protected String getBasePath() {
33+
return "quickfixes/continue-outside-loop";
34+
}
35+
36+
public void testSimple() { doTest(GoContinueNotInLoopInspection.QUICK_FIX_NAME); }
37+
}

0 commit comments

Comments
 (0)