Skip to content

Commit db5c6b2

Browse files
committed
Merge cleanup. Add extra test
1 parent c54c88d commit db5c6b2

File tree

3 files changed

+39
-53
lines changed

3 files changed

+39
-53
lines changed

src/com/goide/inspections/GoSelfImportInspection.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,45 @@
1919
import com.goide.psi.GoFile;
2020
import com.goide.psi.GoImportSpec;
2121
import com.goide.runconfig.testing.GoTestFinder;
22+
import com.intellij.codeInspection.LocalQuickFixBase;
23+
import com.intellij.codeInspection.ProblemDescriptor;
2224
import com.intellij.codeInspection.ProblemsHolder;
25+
import com.intellij.openapi.command.WriteCommandAction;
26+
import com.intellij.openapi.project.Project;
27+
import com.intellij.psi.PsiElement;
28+
import com.intellij.psi.PsiFile;
2329
import org.jetbrains.annotations.NotNull;
2430

2531
public class GoSelfImportInspection extends GoInspectionBase {
2632
@Override
2733
protected void checkFile(@NotNull GoFile file, @NotNull ProblemsHolder problemsHolder) {
28-
if (GoTestFinder.isTestFile(file) && GoTestFinder.getTestTargetPackage(file) != null) return;
34+
if (GoTestFinder.getTestTargetPackage(file) != null) return;
2935

3036
String fileImportPath = file.getImportPath();
3137
for (GoImportSpec importSpec : file.getImports()) {
3238
String path = importSpec.getPath();
3339
if (path.equals(fileImportPath) || path.equals(".")) {
34-
problemsHolder.registerProblem(importSpec, "Self import is not allowed", new GoSelfImportQuickFix("Remove self import"));
40+
problemsHolder.registerProblem(importSpec, "Self import is not allowed", new GoSelfImportQuickFix());
3541
}
3642
}
3743
}
44+
45+
public static class GoSelfImportQuickFix extends LocalQuickFixBase {
46+
protected GoSelfImportQuickFix() {
47+
super("Remove self import");
48+
}
49+
@Override
50+
public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
51+
final PsiElement element = descriptor.getPsiElement();
52+
final PsiFile file = element != null ? element.getContainingFile() : null;
53+
if (!(element instanceof GoImportSpec && file instanceof GoFile)) return;
54+
55+
WriteCommandAction.runWriteCommandAction(project, new Runnable() {
56+
@Override
57+
public void run() {
58+
((GoFile)file).deleteImport((GoImportSpec)element);
59+
}
60+
});
61+
}
62+
}
3863
}

src/com/goide/inspections/GoSelfImportQuickFix.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

tests/com/goide/quickfix/GoSelfImportQuickFixTest.java renamed to tests/com/goide/quickfix/GoSelfImportInspectionTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import java.io.IOException;
2323

24-
public class GoSelfImportQuickFixTest extends GoQuickFixTestBase {
24+
public class GoSelfImportInspectionTest extends GoQuickFixTestBase {
2525
@Override
2626
public void setUp() throws Exception {
2727
super.setUp();
@@ -34,13 +34,22 @@ protected boolean isWriteActionRequired() {
3434
}
3535

3636
public void testRemoveSelfImport() throws IOException {
37-
VirtualFile file = myFixture.getTempDirFixture().createFile("path/a.go", "package pack; import <caret>\"path\"");
37+
VirtualFile file = myFixture.getTempDirFixture().createFile("path/a.go", "package pack;" +
38+
"import <error descr=\"Self import is not allowed\"><caret>\"path\"</error>");
3839
myFixture.configureFromExistingVirtualFile(file);
40+
myFixture.checkHighlighting();
3941
applySingleQuickFix("Remove self import");
4042
}
4143

4244
public void testRemoveRelativeSelfImport() {
43-
myFixture.configureByText("a.go", "package pack; import <caret>\".\"");
45+
myFixture.configureByText("a.go", "package pack; import <error descr=\"Self import is not allowed\"><caret>\".\"</error>");
46+
myFixture.checkHighlighting();
4447
applySingleQuickFix("Remove self import");
4548
}
49+
50+
public void testDoNotConsiderImportFromTestPackageAsSelfImport() throws IOException {
51+
VirtualFile file = myFixture.getTempDirFixture().createFile("path/a_test.go", "package pack_test; import <caret>\"path\"");
52+
myFixture.configureFromExistingVirtualFile(file);
53+
myFixture.checkHighlighting();
54+
}
4655
}

0 commit comments

Comments
 (0)