Skip to content

Commit b674221

Browse files
committed
Merge branch 'eugene-auduchinok-self_import'
2 parents 5cfb9b9 + db5c6b2 commit b674221

File tree

7 files changed

+142
-2
lines changed

7 files changed

+142
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
Inspects file for own path imports
4+
</body>
5+
</html>

src/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@
243243
<localInspection language="go" displayName="Wrong test declaration"
244244
groupName="Go" enabledByDefault="true" level="ERROR"
245245
implementationClass="com.goide.inspections.GoTestSignaturesInspection"/>
246+
<localInspection language="go" displayName="Self import"
247+
groupName="Go" enabledByDefault="true" level="ERROR"
248+
implementationClass="com.goide.inspections.GoSelfImportInspection" />
246249
</extensions>
247250
<actions>
248251
<action id="Go.NewGoFile" class="com.goide.actions.GoCreateFileAction"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.inspections;
18+
19+
import com.goide.psi.GoFile;
20+
import com.goide.psi.GoImportSpec;
21+
import com.goide.runconfig.testing.GoTestFinder;
22+
import com.intellij.codeInspection.LocalQuickFixBase;
23+
import com.intellij.codeInspection.ProblemDescriptor;
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;
29+
import org.jetbrains.annotations.NotNull;
30+
31+
public class GoSelfImportInspection extends GoInspectionBase {
32+
@Override
33+
protected void checkFile(@NotNull GoFile file, @NotNull ProblemsHolder problemsHolder) {
34+
if (GoTestFinder.getTestTargetPackage(file) != null) return;
35+
36+
String fileImportPath = file.getImportPath();
37+
for (GoImportSpec importSpec : file.getImports()) {
38+
String path = importSpec.getPath();
39+
if (path.equals(fileImportPath) || path.equals(".")) {
40+
problemsHolder.registerProblem(importSpec, "Self import is not allowed", new GoSelfImportQuickFix());
41+
}
42+
}
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+
}
63+
}

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/psi/impl/GoElementFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,14 @@ public static GoStatement createShortVarDeclarationStatement(@NotNull Project pr
111111
}
112112

113113
@NotNull
114-
public static GoReferenceExpression createVarReference(@NotNull Project project, @NotNull String name) {
114+
public static GoReferenceExpression createReferenceExpression(@NotNull Project project, @NotNull String name) {
115115
GoFile file = createFileFromText(project, "package a; var a = " + name);
116116
return PsiTreeUtil.findChildOfType(file, GoReferenceExpression.class);
117117
}
118+
119+
@NotNull
120+
public static GoTypeReferenceExpression createTypeReferenceExpression(@NotNull Project project, @NotNull String name) {
121+
GoFile file = createFileFromText(project, "package a; type " + name + " struct {}; func f() { " + name + "{} }");
122+
return PsiTreeUtil.findChildOfType(file, GoTypeReferenceExpression.class);
123+
}
118124
}

src/com/goide/refactor/GoIntroduceVariableBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void run() {
182182
for (PsiElement occurrence : occurrences) {
183183
PsiElement occurrenceParent = occurrence.getParent();
184184
if (occurrenceParent instanceof GoParenthesesExpr) occurrence = occurrenceParent;
185-
newOccurrences.add(occurrence.replace(GoElementFactory.createVarReference(project, name)));
185+
newOccurrences.add(occurrence.replace(GoElementFactory.createReferenceExpression(project, name)));
186186
}
187187
}
188188
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.GoSelfImportInspection;
20+
import com.intellij.openapi.vfs.VirtualFile;
21+
22+
import java.io.IOException;
23+
24+
public class GoSelfImportInspectionTest extends GoQuickFixTestBase {
25+
@Override
26+
public void setUp() throws Exception {
27+
super.setUp();
28+
myFixture.enableInspections(GoSelfImportInspection.class);
29+
}
30+
31+
@Override
32+
protected boolean isWriteActionRequired() {
33+
return false;
34+
}
35+
36+
public void testRemoveSelfImport() throws IOException {
37+
VirtualFile file = myFixture.getTempDirFixture().createFile("path/a.go", "package pack;" +
38+
"import <error descr=\"Self import is not allowed\"><caret>\"path\"</error>");
39+
myFixture.configureFromExistingVirtualFile(file);
40+
myFixture.checkHighlighting();
41+
applySingleQuickFix("Remove self import");
42+
}
43+
44+
public void testRemoveRelativeSelfImport() {
45+
myFixture.configureByText("a.go", "package pack; import <error descr=\"Self import is not allowed\"><caret>\".\"</error>");
46+
myFixture.checkHighlighting();
47+
applySingleQuickFix("Remove self import");
48+
}
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+
}
55+
}

0 commit comments

Comments
 (0)