Skip to content

Commit c54c88d

Browse files
committed
Simplify self import quick fix, only delete import
1 parent c39fe57 commit c54c88d

File tree

8 files changed

+31
-76
lines changed

8 files changed

+31
-76
lines changed

src/com/goide/inspections/GoSelfImportInspection.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
public class GoSelfImportInspection extends GoInspectionBase {
2626
@Override
2727
protected void checkFile(@NotNull GoFile file, @NotNull ProblemsHolder problemsHolder) {
28-
if (GoTestFinder.isTestFile(file) && GoTestFinder.isTestPackageName(file.getPackageName())) return;
28+
if (GoTestFinder.isTestFile(file) && GoTestFinder.getTestTargetPackage(file) != null) return;
2929

30-
GoImportSpec selfImportSpec = file.getImportedPackagesMap().get(file.getImportPath());
31-
if (selfImportSpec != null) {
32-
problemsHolder.registerProblem(selfImportSpec, "Self import is not allowed", new GoSelfImportQuickFix("Remove self import"));
30+
String fileImportPath = file.getImportPath();
31+
for (GoImportSpec importSpec : file.getImports()) {
32+
String path = importSpec.getPath();
33+
if (path.equals(fileImportPath) || path.equals(".")) {
34+
problemsHolder.registerProblem(importSpec, "Self import is not allowed", new GoSelfImportQuickFix("Remove self import"));
35+
}
3336
}
3437
}
3538
}

src/com/goide/inspections/GoSelfImportQuickFix.java

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@
1616

1717
package com.goide.inspections;
1818

19-
import com.goide.psi.*;
20-
import com.goide.psi.impl.GoElementFactory;
19+
import com.goide.psi.GoFile;
20+
import com.goide.psi.GoImportSpec;
2121
import com.intellij.codeInspection.LocalQuickFixBase;
2222
import com.intellij.codeInspection.ProblemDescriptor;
2323
import com.intellij.openapi.command.WriteCommandAction;
2424
import com.intellij.openapi.project.Project;
2525
import com.intellij.psi.PsiElement;
2626
import com.intellij.psi.PsiFile;
27-
import com.intellij.psi.PsiRecursiveElementVisitor;
28-
import com.intellij.psi.PsiReference;
29-
import com.intellij.psi.util.PsiTreeUtil;
30-
import com.intellij.util.containers.ContainerUtil;
3127
import org.jetbrains.annotations.NotNull;
3228

33-
import java.util.Map;
34-
3529
public class GoSelfImportQuickFix extends LocalQuickFixBase {
3630

37-
protected GoSelfImportQuickFix(@NotNull String name) {
31+
protected GoSelfImportQuickFix(String name) {
3832
super(name);
3933
}
4034

@@ -44,34 +38,10 @@ public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor
4438
final PsiFile file = element != null ? element.getContainingFile() : null;
4539
if (!(element instanceof GoImportSpec && file instanceof GoFile)) return;
4640

47-
final Map<GoReferenceExpressionBase, String> usages = ContainerUtil.newHashMap();
48-
file.acceptChildren(new PsiRecursiveElementVisitor() {
49-
public void visitElement(@NotNull PsiElement element) {
50-
if (element instanceof GoReferenceExpression || element instanceof GoTypeReferenceExpression) {
51-
PsiReference reference = element.getReference();
52-
PsiElement definition = reference != null ? reference.resolve() : null;
53-
PsiFile definitionFile = definition instanceof GoNamedElement ? definition.getContainingFile() : null;
54-
if (definitionFile != null && definitionFile.getParent() == file.getParent()) {
55-
usages.put((GoReferenceExpressionBase)element, ((GoNamedElement)definition).getName());
56-
return;
57-
}
58-
}
59-
super.visitElement(element);
60-
}
61-
});
6241
WriteCommandAction.runWriteCommandAction(project, new Runnable() {
6342
@Override
6443
public void run() {
65-
for (Map.Entry<GoReferenceExpressionBase, String> usage : usages.entrySet()) {
66-
GoReferenceExpressionBase expression = usage.getKey();
67-
expression.replace(expression instanceof GoReferenceExpression
68-
? GoElementFactory.createReferenceExpression(project, usage.getValue())
69-
: GoElementFactory.createTypeReferenceExpression(project, usage.getValue()));
70-
}
71-
GoImportDeclaration importDeclaration = PsiTreeUtil.getParentOfType(element, GoImportDeclaration.class);
72-
assert importDeclaration != null;
73-
PsiElement elementToDelete = importDeclaration.getImportSpecList().size() == 1 ? importDeclaration : element;
74-
elementToDelete.delete();
44+
((GoFile)file).deleteImport((GoImportSpec)element);
7545
}
7646
});
7747
}

src/com/goide/psi/GoFile.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.intellij.psi.tree.IElementType;
4444
import com.intellij.psi.util.CachedValueProvider;
4545
import com.intellij.psi.util.CachedValuesManager;
46+
import com.intellij.psi.util.PsiTreeUtil;
4647
import com.intellij.util.ArrayFactory;
4748
import com.intellij.util.ArrayUtil;
4849
import com.intellij.util.Processor;
@@ -434,6 +435,13 @@ public boolean hasCPathImport() {
434435
return getImportMap().containsKey(GoConstants.C_PATH);
435436
}
436437

438+
public void deleteImport(@NotNull GoImportSpec importSpec) {
439+
GoImportDeclaration importDeclaration = PsiTreeUtil.getParentOfType(importSpec, GoImportDeclaration.class);
440+
assert importDeclaration != null;
441+
PsiElement elementToDelete = importDeclaration.getImportSpecList().size() == 1 ? importDeclaration : importSpec;
442+
elementToDelete.delete();
443+
}
444+
437445
private static boolean processChildrenDummyAware(@NotNull GoFile file, @NotNull final Processor<PsiElement> processor) {
438446
StubTree stubTree = file.getStubTree();
439447
if (stubTree != null) {

src/com/goide/runconfig/testing/GoTestFinder.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ public static boolean isTestFile(@Nullable VirtualFile file) {
4646
return file != null && file.getFileType() == GoFileType.INSTANCE && file.getNameWithoutExtension().endsWith(GoConstants.TEST_SUFFIX);
4747
}
4848

49-
public static boolean isTestPackageName(@Nullable String packageName) {
50-
return packageName != null && packageName.endsWith(GoConstants.TEST_SUFFIX);
51-
}
52-
5349
@Nullable
5450
public static String getTestFunctionName(@NotNull GoFunctionOrMethodDeclaration function) {
5551
return GoTestFunctionType.fromName(function.getName()) == GoTestFunctionType.TEST ? StringUtil.notNullize(function.getName()) : null;

testData/quickfixes/self-import/a.go

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

testData/quickfixes/self-import/b-after.go

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

testData/quickfixes/self-import/b.go

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

tests/com/goide/quickfix/GoSelfImportQuickFixTest.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,30 @@
1717
package com.goide.quickfix;
1818

1919
import com.goide.inspections.GoSelfImportInspection;
20-
import com.intellij.testFramework.LightProjectDescriptor;
20+
import com.intellij.openapi.vfs.VirtualFile;
21+
22+
import java.io.IOException;
2123

2224
public class GoSelfImportQuickFixTest extends GoQuickFixTestBase {
2325
@Override
2426
public void setUp() throws Exception {
2527
super.setUp();
2628
myFixture.enableInspections(GoSelfImportInspection.class);
27-
setUpProjectSdk();
28-
}
29-
30-
@Override
31-
protected LightProjectDescriptor getProjectDescriptor() {
32-
return createMockProjectDescriptor();
33-
}
34-
35-
@Override
36-
protected String getBasePath() {
37-
return "quickfixes/self-import";
3829
}
3930

4031
@Override
4132
protected boolean isWriteActionRequired() {
4233
return false;
4334
}
4435

45-
public void testRemoveSelfImport() {
46-
myFixture.configureByFile("b.go");
47-
//applySingleQuickFix("Remove self import");
48-
//myFixture.checkResultByFile("b-after.go");
36+
public void testRemoveSelfImport() throws IOException {
37+
VirtualFile file = myFixture.getTempDirFixture().createFile("path/a.go", "package pack; import <caret>\"path\"");
38+
myFixture.configureFromExistingVirtualFile(file);
39+
applySingleQuickFix("Remove self import");
40+
}
41+
42+
public void testRemoveRelativeSelfImport() {
43+
myFixture.configureByText("a.go", "package pack; import <caret>\".\"");
44+
applySingleQuickFix("Remove self import");
4945
}
5046
}

0 commit comments

Comments
 (0)