Skip to content

Commit 58aee87

Browse files
committed
Merge cleanup
1 parent 24a6879 commit 58aee87

File tree

9 files changed

+72
-59
lines changed

9 files changed

+72
-59
lines changed

src/com/goide/codeInsight/imports/GoImportPackageQuickFix.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.intellij.openapi.ui.popup.JBPopupFactory;
4040
import com.intellij.openapi.util.Comparing;
4141
import com.intellij.openapi.util.TextRange;
42-
import com.intellij.openapi.util.text.StringUtil;
4342
import com.intellij.psi.PsiDirectory;
4443
import com.intellij.psi.PsiElement;
4544
import com.intellij.psi.PsiFile;
@@ -162,22 +161,25 @@ private Collection<String> getPackagesToImport(@NotNull PsiElement element) {
162161
if (myPackagesToImport == null) {
163162
final GlobalSearchScope scope = GoUtil.moduleScope(element);
164163
PsiFile file = element.getContainingFile();
165-
boolean isTestFile = GoTestFinder.isTestFile(file) && GoTestFinder.isTestPackageName(((GoFile)file).getPackageName());
166-
final String packageName = isTestFile ? ((GoFile)file).getPackageNameWithoutTestSuffix() : null;
167-
final boolean allowSamePath = !StringUtil.isEmpty(packageName);
168164
Project project = element.getProject();
169165
final PsiDirectory parentDirectory = file != null ? file.getParent() : null;
170166
final GoExcludedPathsSettings excludedSettings = GoExcludedPathsSettings.getInstance(project);
167+
final String testTargetPackage = GoTestFinder.getTestTargetPackage(file);
171168
Collection<GoFile> es = StubIndex.getElements(GoPackagesIndex.KEY, myPackageName, project, scope, GoFile.class);
172169
myPackagesToImport = sorted(skipNulls(map2Set(
173170
es,
174171
new Function<GoFile, String>() {
175172
@Nullable
176173
@Override
177174
public String fun(@NotNull GoFile file) {
178-
String importPath = parentDirectory == null || !parentDirectory.isEquivalentTo(file.getParent()) ||
179-
(allowSamePath && packageName.equals(file.getPackageName())) ? file.getImportPath() : null;
180-
return importPath != null && !excludedSettings.isExcluded(importPath) ? importPath : null;
175+
if (parentDirectory != null && parentDirectory.isEquivalentTo(file.getParent())) {
176+
if (testTargetPackage == null || !testTargetPackage.equals(file.getPackageName())) {
177+
return null;
178+
}
179+
}
180+
181+
String importPath = file.getImportPath();
182+
return !excludedSettings.isExcluded(importPath) ? importPath : null;
181183
}
182184
}
183185
)), new MyImportsComparator(element));

src/com/goide/completion/GoAutoImportCompletionContributor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,8 @@ protected void addCompletions(@NotNull CompletionParameters parameters,
8787
Project project = position.getProject();
8888
GlobalSearchScope scope = GoUtil.moduleScopeWithoutTests(file);
8989
VirtualFile containingDirectory = file.getVirtualFile().getParent();
90-
boolean isTestFile = GoTestFinder.isTestFile(file) && GoTestFinder.isTestPackageName(file.getPackageName());
91-
String allowedPackageInDirectory = isTestFile ? file.getPackageNameWithoutTestSuffix() : null;
9290
if (containingDirectory != null) {
93-
scope = new GoUtil.ExceptChildOfDirectory(containingDirectory, scope, allowedPackageInDirectory);
91+
scope = new GoUtil.ExceptChildOfDirectory(containingDirectory, scope, GoTestFinder.getTestTargetPackage(file));
9492
}
9593
Set<String> sortedKeys = sortMatching(matcher, StubIndex.getInstance().getAllKeys(ALL_PUBLIC_NAMES, project), file);
9694
for (String name : sortedKeys) {
@@ -109,7 +107,7 @@ private CompletionResultSet adjustMatcher(@NotNull CompletionParameters paramete
109107
});
110108
}
111109

112-
private static Set<String> sortMatching(@NotNull PrefixMatcher matcher, @NotNull Collection<String> names, @NotNull GoFile file) {
110+
private static Set<String> sortMatching(@NotNull PrefixMatcher matcher, @NotNull Collection<String> names, @NotNull GoFile file) {
113111
ProgressManager.checkCanceled();
114112
String prefix = matcher.getPrefix();
115113
if (prefix.isEmpty()) return ContainerUtil.newLinkedHashSet(names);

src/com/goide/completion/GoImportPathsCompletionProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.goide.GoFileType;
2020
import com.goide.project.GoExcludedPathsSettings;
21-
import com.goide.psi.GoFile;
2221
import com.goide.psi.GoImportString;
2322
import com.goide.runconfig.testing.GoTestFinder;
2423
import com.goide.sdk.GoSdkUtil;
@@ -66,13 +65,13 @@ public static void addCompletions(@NotNull CompletionResultSet result,
6665
GoExcludedPathsSettings excludedSettings = GoExcludedPathsSettings.getInstance(project);
6766
GlobalSearchScope scope = withLibraries ? GoUtil.moduleScope(module) : GoUtil.moduleScopeWithoutLibraries(module);
6867
PsiFile contextFile = context != null ? context.getContainingFile() : null;
69-
boolean isTestFile = GoTestFinder.isTestFile(contextFile) && GoTestFinder.isTestPackageName(((GoFile)contextFile).getPackageName());
68+
boolean testFileWithTestPackage = GoTestFinder.isTestFileWithTestPackage(contextFile);
7069
for (VirtualFile file : FileTypeIndex.getFiles(GoFileType.INSTANCE, scope)) {
7170
VirtualFile parent = file.getParent();
7271
if (parent == null) continue;
7372
String importPath = GoSdkUtil.getPathRelativeToSdkAndLibraries(parent, project, module);
7473
if (!StringUtil.isEmpty(importPath) && !excludedSettings.isExcluded(importPath) &&
75-
(!importPath.equals(contextImportPath) || isTestFile)) {
74+
(testFileWithTestPackage || !importPath.equals(contextImportPath))) {
7675
result.addElement(GoCompletionUtil.createPackageLookupElement(importPath, contextImportPath, false));
7776
}
7877
}

src/com/goide/project/GoExcludedPathsSettings.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public void setExcludedPackages(String... excludedPackages) {
5959
incModificationCount();
6060
}
6161

62-
public boolean isExcluded(@NotNull String importPath) {
62+
public boolean isExcluded(@Nullable String importPath) {
63+
if (importPath == null) {
64+
return false;
65+
}
6366
for (String excludedPath : myExcludedPackages) {
6467
if (FileUtil.isAncestor(excludedPath, importPath, false)) return true;
6568
}

src/com/goide/psi/GoFile.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,6 @@ public String getPackageName() {
423423
return null;
424424
}
425425

426-
@Nullable
427-
public String getPackageNameWithoutTestSuffix() {
428-
String name = getPackageName();
429-
return name != null ? StringUtil.trimEnd(name, GoConstants.TEST_SUFFIX) : null;
430-
}
431-
432426
@Nullable
433427
@Override
434428
public GoFileStub getStub() {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,26 @@ 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;
5652
}
5753

54+
public static boolean isTestFileWithTestPackage(@Nullable PsiFile file) {
55+
return getTestTargetPackage(file) != null;
56+
}
57+
58+
@Nullable
59+
public static String getTestTargetPackage(@Nullable PsiFile file) {
60+
if (isTestFile(file)) {
61+
String packageName = ((GoFile)file).getPackageName();
62+
if (packageName != null && packageName.endsWith(GoConstants.TEST_SUFFIX)) {
63+
return StringUtil.nullize(StringUtil.trimEnd(packageName, GoConstants.TEST_SUFFIX));
64+
}
65+
}
66+
return null;
67+
}
68+
5869
@Nullable
5970
@Override
6071
public PsiElement findSourceElement(@NotNull PsiElement from) {

src/com/goide/util/GoUtil.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,30 @@ public static class ExceptChildOfDirectory extends DelegatingGlobalSearchScope {
248248
@NotNull private final VirtualFile myParent;
249249
@Nullable private final String myAllowedPackageInExcludedDirectory;
250250

251-
public ExceptChildOfDirectory(@NotNull VirtualFile parent, @NotNull GlobalSearchScope baseScope, @Nullable String allowedPackageInExcludedDirectory) {
251+
public ExceptChildOfDirectory(@NotNull VirtualFile parent,
252+
@NotNull GlobalSearchScope baseScope,
253+
@Nullable String allowedPackageInExcludedDirectory) {
252254
super(baseScope);
253255
myParent = parent;
254256
myAllowedPackageInExcludedDirectory = allowedPackageInExcludedDirectory;
255257
}
256258

257259
@Override
258260
public boolean contains(@NotNull VirtualFile file) {
259-
Project project = getProject();
260-
PsiFile psiFile = project != null ? PsiManager.getInstance(project).findFile(file) : null;
261-
if (!(psiFile instanceof GoFile)) return false;
262-
263-
return (!myParent.equals(file.getParent()) ||
264-
myAllowedPackageInExcludedDirectory != null &&
265-
myAllowedPackageInExcludedDirectory.equals(((GoFile)psiFile).getPackageName())) &&
266-
super.contains(file);
261+
if (myParent.equals(file.getParent())) {
262+
if (myAllowedPackageInExcludedDirectory == null) {
263+
return false;
264+
}
265+
Project project = getProject();
266+
PsiFile psiFile = project != null ? PsiManager.getInstance(project).findFile(file) : null;
267+
if (!(psiFile instanceof GoFile)) {
268+
return false;
269+
}
270+
if (!myAllowedPackageInExcludedDirectory.equals(((GoFile)psiFile).getPackageName())) {
271+
return false;
272+
}
273+
}
274+
return super.contains(file);
267275
}
268276
}
269277

tests/com/goide/completion/GoCompletionSdkAwareTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,26 @@ public void testDoNotAutoImportWithTheSameImportPath() throws IOException {
302302
VirtualFile file = myFixture.getTempDirFixture().createFile("pack1/file1.go", "package pack1; func test() { pack1.MyFunc<caret> }");
303303
myFixture.configureFromExistingVirtualFile(file);
304304
myFixture.completeBasic();
305-
myFixture.checkResult("package pack1;\n" +
306-
"import \"pack2\" func test() { pack1.MyFunctionFromOtherPath() }");
305+
myFixture.checkResult("package pack1;\nimport \"pack2\" func test() { pack1.MyFunctionFromOtherPath() }");
306+
}
307+
308+
public void testAutoImportOwnImportPathFromTest() throws IOException {
309+
myFixture.getTempDirFixture().createFile("pack/a.go", "package myPack; func Func() {}");
310+
VirtualFile testFile = myFixture.getTempDirFixture()
311+
.createFile("pack/a_test.go", "package myPack_test; func TestFunc() { myPack.Fun<caret> }");
312+
myFixture.configureFromExistingVirtualFile(testFile);
313+
myFixture.completeBasic();
314+
myFixture.checkResult("package myPack_test;\nimport \"pack\" func TestFunc() { myPack.Func() }");
307315
}
308316

317+
public void testDoNotAutoImportDifferentPackageInSamePathFromTest() throws IOException {
318+
String text = "package foo_test; func TestFunc() { bar.Fun<caret> }";
319+
myFixture.getTempDirFixture().createFile("pack/a.go", "package bar; func Func() {}");
320+
myFixture.configureFromExistingVirtualFile(myFixture.getTempDirFixture().createFile("pack/a_test.go", text));
321+
myFixture.completeBasic();
322+
myFixture.checkResult(text);
323+
}
324+
309325
public void testImportOwnPathFromTestFile() throws IOException {
310326
TempDirTestFixture dir = myFixture.getTempDirFixture();
311327
VirtualFile testFile = dir.createFile("fuzz/fuzy_test.go", "package fuzy_test; import \"<caret>\"");

tests/com/goide/completion/GoCompletionTest.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,31 +75,13 @@ public void testDoNotCompleteFullPackagesForRelativeImports() throws IOException
7575
assertSameElements(lookupElementStrings, "package1", "package2");
7676
}
7777

78-
private void doTestCompleteOwnPath(String existingFile, String existingText, String newFile, String newText, String result)
79-
throws IOException {
80-
myFixture.getTempDirFixture().createFile(existingFile, existingText);
81-
VirtualFile testFile = myFixture.getTempDirFixture().createFile(newFile, newText);
78+
public void testDoNotCompleteOwnImportPath() throws IOException {
79+
myFixture.getTempDirFixture().createFile("package/long/long/path/test.go", "package pack");
80+
VirtualFile testFile = myFixture.getTempDirFixture()
81+
.createFile("package/very/long/path/but/same/package/test.go", "package pack; import `package/<caret>`");
8282
myFixture.configureFromExistingVirtualFile(testFile);
8383
myFixture.completeBasic();
84-
myFixture.checkResult(result);
85-
}
86-
87-
public void testDoNotCompleteOwnImportPath() throws IOException {
88-
doTestCompleteOwnPath("package/long/long/path/test.go", "package pack",
89-
"package/very/long/path/but/same/package/test.go", "package pack; import `package/<caret>`",
90-
"package pack; import `package/long/long/path`");
91-
}
92-
93-
public void testCompleteOwnImportPathFromTest() throws IOException {
94-
doTestCompleteOwnPath("pack/a.go", "package myPack; func Func() {}",
95-
"pack/a_test.go", "package myPack_test; func TestFunc() { myPack.Fun<caret> }",
96-
"package myPack_test;\nimport \"pack\" func TestFunc() { myPack.Func() }");
97-
}
98-
99-
public void testDoNotCompleteDifferentPackageInSamePathFromTest() throws IOException {
100-
String text = "package foo_test; func TestFunc() { bar.Fun<caret> }";
101-
doTestCompleteOwnPath("pack/a.go", "package bar; func Func() {}",
102-
"pack/a_test.go", text, text);
84+
myFixture.checkResult("package pack; import `package/long/long/path`");
10385
}
10486

10587
public void testImportsPriority() throws IOException {

0 commit comments

Comments
 (0)