-
Notifications
You must be signed in to change notification settings - Fork 567
Unresolved struct field inspection #2892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright 2013-2017 Sergey Ignatov, Alexander Zolotov, Florin Patan | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.goide.inspections.unresolved; | ||
|
|
||
| import com.goide.psi.GoFieldDeclaration; | ||
| import com.goide.psi.GoStructType; | ||
| import com.goide.psi.impl.GoElementFactory; | ||
| import com.intellij.codeInspection.LocalQuickFixOnPsiElement; | ||
| import com.intellij.openapi.project.Project; | ||
| import com.intellij.psi.PsiElement; | ||
| import com.intellij.psi.PsiFile; | ||
| import com.intellij.util.ObjectUtils; | ||
| import com.intellij.util.containers.ContainerUtil; | ||
| import org.jetbrains.annotations.Nls; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class GoAddStructFieldFix extends LocalQuickFixOnPsiElement { | ||
| public static final String QUICK_FIX_NAME = "Add missing field"; | ||
| private final String myFieldText; | ||
| private final String myTypeText; | ||
|
|
||
| protected GoAddStructFieldFix(String fieldText, String typeText, @NotNull GoStructType element) { | ||
| super(element); | ||
| myFieldText = fieldText; | ||
| myTypeText = typeText; | ||
| } | ||
|
|
||
| @NotNull | ||
| @Override | ||
| public String getText() { | ||
| return QUICK_FIX_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { | ||
| GoStructType structType = ObjectUtils.tryCast(startElement, GoStructType.class); | ||
| if (structType == null) return; | ||
| List<GoFieldDeclaration> declarations = structType.getFieldDeclarationList(); | ||
| PsiElement anchor = !declarations.isEmpty() ? ContainerUtil.getLastItem(declarations) : structType.getLbrace(); | ||
| if (anchor != null) structType.addAfter(GoElementFactory.createFieldDeclaration(project, myFieldText, myTypeText), anchor); | ||
|
||
| } | ||
|
|
||
| @Nls | ||
| @NotNull | ||
| @Override | ||
| public String getFamilyName() { | ||
| return QUICK_FIX_NAME; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| import com.goide.codeInsight.imports.GoImportPackageQuickFix; | ||
| import com.goide.inspections.GoInspectionBase; | ||
| import com.goide.psi.*; | ||
| import com.goide.psi.impl.GoPsiImplUtil; | ||
| import com.goide.psi.impl.GoReference; | ||
| import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector; | ||
| import com.intellij.codeInspection.LocalInspectionToolSession; | ||
|
|
@@ -33,6 +34,7 @@ | |
| import com.intellij.psi.formatter.FormatterUtil; | ||
| import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference; | ||
| import com.intellij.psi.util.PsiTreeUtil; | ||
| import com.intellij.util.ObjectUtils; | ||
| import com.intellij.util.containers.ContainerUtil; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
@@ -74,7 +76,12 @@ public void visitReferenceExpression(@NotNull GoReferenceExpression o) { | |
| } | ||
| else if (reference.resolve() == null) { | ||
| LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY; | ||
| if (isProhibited(o, qualifier)) { | ||
| GoType type = qualifier != null ? qualifier.getGoType(null) : null; | ||
| GoStructType structType = type != null ? ObjectUtils.tryCast(type.getUnderlyingType(), GoStructType.class) : null; | ||
| if (!"_".equals(reference.getCanonicalText()) && structType != null) { | ||
| fixes = new LocalQuickFix[]{new GoAddStructFieldFix(reference.getCanonicalText(), getTypeName(o), structType)}; | ||
|
||
| } | ||
| else if (isProhibited(o, qualifier)) { | ||
| fixes = createImportPackageFixes(o, reference, holder.isOnTheFly()); | ||
| } | ||
| else if (holder.isOnTheFly()) { | ||
|
|
@@ -159,6 +166,14 @@ else if (holder.isOnTheFly()) { | |
| }; | ||
| } | ||
|
|
||
| private static String getTypeName(GoReferenceExpression referenceExpression) { | ||
| GoAssignmentStatement assignment = PsiTreeUtil.getParentOfType(referenceExpression, GoAssignmentStatement.class); | ||
| if (assignment == null) return "interface {}"; | ||
| GoExpression expression = GoPsiImplUtil.getRightExpression(assignment, referenceExpression); | ||
| GoType type = expression != null ? expression.getGoType(null) : null; | ||
| return type != null ? type.getText() : "interface {}"; | ||
| } | ||
|
|
||
| @NotNull | ||
| private static LocalQuickFix[] createImportPackageFixes(@NotNull PsiElement target, @NotNull PsiReference reference, boolean onTheFly) { | ||
| if (onTheFly) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package main | ||
|
|
||
| type S struct { | ||
| bb interface{} | ||
| cc interface{} | ||
| } | ||
|
|
||
| func main() { | ||
| s := S{} | ||
| s.<error descr="Unresolved reference '_'">_<caret></error> = "aa" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package main | ||
|
|
||
| type S struct { | ||
| bb interface{} | ||
| cc interface{} | ||
| aa string | ||
| } | ||
|
|
||
| func main() { | ||
| s := S{} | ||
| s.aa = "aa" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package main | ||
|
|
||
| type S struct { | ||
| bb interface{} | ||
| cc interface{} | ||
| } | ||
|
|
||
| func main() { | ||
| s := S{} | ||
| s.aa<caret> = "aa" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package main | ||
|
|
||
| type S struct { | ||
| bb interface{} | ||
| cc interface{} | ||
| aa interface{} | ||
| } | ||
|
|
||
| func main() { | ||
| s := S{} | ||
| s.aa<caret> | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package main | ||
|
|
||
| type S struct { | ||
| bb interface{} | ||
| cc interface{} | ||
| } | ||
|
|
||
| func main() { | ||
| s := S{} | ||
| s.aa<caret> | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package main | ||
|
|
||
| type S struct { | ||
| aa string | ||
| } | ||
|
|
||
| func main() { | ||
| s := S{} | ||
| s.aa<caret> = "aa" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package main | ||
|
|
||
| type S struct { | ||
|
|
||
| } | ||
|
|
||
| func main() { | ||
| s := S{} | ||
| s.aa<caret> = "aa" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright 2013-2017 Sergey Ignatov, Alexander Zolotov, Florin Patan | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.goide.quickfix; | ||
|
|
||
| import com.goide.SdkAware; | ||
| import com.goide.inspections.unresolved.GoUnresolvedReferenceInspection; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| @SdkAware | ||
| public class GoAddStructFieldQuickFixTest extends GoQuickFixTestBase { | ||
|
|
||
| private static final String ADD_STRUCT_FIELD = "Add missing field"; | ||
|
|
||
| @Override | ||
| protected void setUp() throws Exception { | ||
| super.setUp(); | ||
| myFixture.enableInspections(GoUnresolvedReferenceInspection.class); | ||
| } | ||
|
|
||
| @NotNull | ||
| @Override | ||
| protected String getBasePath() { | ||
| return "quickfixes/add-struct-field"; | ||
| } | ||
|
|
||
| public void testSimple() { doTest(ADD_STRUCT_FIELD); } | ||
| public void testWithoutElements() { doTest(ADD_STRUCT_FIELD); } | ||
| public void testWithoutAssignment() { doTest(ADD_STRUCT_FIELD); } | ||
| public void testBlank() { doTestNoFix(ADD_STRUCT_FIELD, true); } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
= ContainerUtil.getLastItem(declarations, structType.getLbrace)