|
19 | 19 | import com.goide.psi.GoFile; |
20 | 20 | import com.goide.psi.GoImportSpec; |
21 | 21 | import com.goide.runconfig.testing.GoTestFinder; |
| 22 | +import com.intellij.codeInspection.LocalQuickFixBase; |
| 23 | +import com.intellij.codeInspection.ProblemDescriptor; |
22 | 24 | 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; |
23 | 29 | import org.jetbrains.annotations.NotNull; |
24 | 30 |
|
25 | 31 | public class GoSelfImportInspection extends GoInspectionBase { |
26 | 32 | @Override |
27 | 33 | 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; |
29 | 35 |
|
30 | 36 | String fileImportPath = file.getImportPath(); |
31 | 37 | for (GoImportSpec importSpec : file.getImports()) { |
32 | 38 | String path = importSpec.getPath(); |
33 | 39 | 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()); |
35 | 41 | } |
36 | 42 | } |
37 | 43 | } |
| 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 | + } |
38 | 63 | } |
0 commit comments