Skip to content

Commit fabb519

Browse files
committed
Tweak the package autoimports.
1 parent ffc8606 commit fabb519

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

src/ro/redeul/google/go/inspection/UnresolvedSymbols.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public String getDisplayName() {
3838
}
3939

4040
@Override
41-
protected void doCheckFile(@NotNull GoFile file,
41+
protected void doCheckFile(@NotNull final GoFile file,
4242
@NotNull final InspectionResult result) {
4343
new GoRecursiveElementVisitor() {
4444
@Override
@@ -60,24 +60,21 @@ public void visitLiteralIdentifier(GoLiteralIdentifier identifier) {
6060
boolean hasPackageReference = false;
6161
boolean hasVarReferences = false;
6262
for (PsiReference ref : refs) {
63-
if (ref instanceof PackageReference)
64-
hasPackageReference = true;
63+
if (ref instanceof PackageReference) {
64+
getData().add(new AddImportFix(identifier));
65+
}
6566

6667
if (ref instanceof VarOrConstReference)
67-
hasVarReferences = true;
68-
}
68+
if (isGlobalVariableIdentifier(identifier))
69+
getData().add(new CreateGlobalVariableFix(identifier));
70+
else
71+
getData()
72+
.add(new CreateLocalVariableFix(identifier))
73+
.add(new CreateGlobalVariableFix(identifier))
74+
.add(new CreateFunctionFix(identifier))
75+
.add(new CreateClosureFunctionFix(identifier));
6976

70-
if ( hasVarReferences )
71-
if ( isGlobalVariableIdentifier(identifier) )
72-
getData().add(new CreateGlobalVariableFix(identifier));
73-
else
74-
getData()
75-
.add(new CreateLocalVariableFix(identifier))
76-
.add(new CreateGlobalVariableFix(identifier))
77-
.add(new CreateFunctionFix(identifier))
78-
.add(new CreateClosureFunctionFix(identifier));
79-
else if ( hasPackageReference )
80-
getData().add(new AddImportFix(identifier));
77+
}
8178

8279
((GoPsiElement) identifier.getParent()).accept(this);
8380
}
@@ -113,7 +110,7 @@ id, message("warning.unresolved.symbol", id.getText()),
113110

114111
private static boolean isGlobalVariableIdentifier(GoLiteralIdentifier ident) {
115112
return findParentOfType(ident, GoSelectorExpression.class) == null &&
116-
findParentOfType(ident, GoFunctionDeclaration.class) == null &&
117-
findParentOfType(ident, GoVarDeclarations.class) != null;
113+
findParentOfType(ident, GoFunctionDeclaration.class) == null &&
114+
findParentOfType(ident, GoVarDeclarations.class) != null;
118115
}
119116
}

src/ro/redeul/google/go/inspection/fix/AddImportFix.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public boolean execute() {
169169
@Override
170170
public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
171171
PackageReference reference = GoReferenceImporter.getPackageReferenceAt(file, startElement.getTextOffset());
172-
return reference != null && reference.resolve() == null;
172+
return reference != null && reference.resolve() == null && findPotentialImports(startElement).size() > 0;
173173
}
174174

175175
@NotNull

src/ro/redeul/google/go/lang/psi/typing/GoTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static GoType get(GoType[] types) {
7373

7474
public static GoType[] getPackageType(GoImportDeclaration declaration) {
7575
GoPackage goPackage = declaration.getPackage();
76-
return goPackage != null ? new GoType[]{new GoTypePackage(goPackage)} : GoType.EMPTY_ARRAY;
76+
return new GoType[]{new GoTypePackage(goPackage)};
7777
}
7878

7979
/**

src/ro/redeul/google/go/services/GoPsiManager.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ public <T extends GoPsiElement, V> GoType getOrCompute(T element, Function<T, Go
7878
if (type == null) {
7979
RecursionGuard.StackStamp stamp = outSecondGuard.markStack();
8080
type = valueCalculator.fun(element);
81-
if ( type != null ) {
82-
if (stamp.mayCacheNow()) {
83-
type = ConcurrencyUtil.cacheOrGet(myComputedType, element, type);
84-
} else {
85-
final GoType alreadyInferred = myComputedType.get(element);
86-
if (alreadyInferred != null) {
87-
type = alreadyInferred;
88-
}
81+
if ( type == null )
82+
type = GoType.Unknown;
83+
84+
if (stamp.mayCacheNow()) {
85+
type = ConcurrencyUtil.cacheOrGet(myComputedType, element, type);
86+
} else {
87+
final GoType alreadyInferred = myComputedType.get(element);
88+
if (alreadyInferred != null) {
89+
type = alreadyInferred;
8990
}
9091
}
9192
}

0 commit comments

Comments
 (0)