Skip to content
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

Unresolved struct field inspection #2892

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions src/com/goide/inspections/unresolved/GoAddStructFieldFix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.GoConstants;
import com.goide.psi.*;
import com.goide.psi.impl.GoPsiImplUtil;
import com.goide.util.GoUtil;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.codeInsight.template.impl.ConstantNode;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class GoAddStructFieldFix extends LocalQuickFixAndIntentionActionOnPsiElement {
public static final String QUICK_FIX_NAME = "Add missing field";

protected GoAddStructFieldFix(@NotNull PsiElement element) {
super(element);
}

@NotNull
@Override
public String getText() {
return QUICK_FIX_NAME;
}

@Override
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
if (editor == null) return;
GoReferenceExpression referenceExpression = ObjectUtils.tryCast(startElement, GoReferenceExpression.class);
GoStructType structType = referenceExpression != null ? resolveStructType(referenceExpression) : null;
if (structType == null) return;

List<GoFieldDeclaration> declarations = structType.getFieldDeclarationList();
PsiElement anchor = !declarations.isEmpty() ? ContainerUtil.getLastItem(declarations) : structType.getLbrace();
if (anchor == null) return;

startTemplate(project, editor, file, referenceExpression, anchor);
}

private static void startTemplate(@NotNull Project project,
@NotNull Editor editor,
@NotNull PsiFile file,
GoReferenceExpression referenceExpression,
PsiElement anchor) {
Template template = TemplateManager.getInstance(project).createTemplate("", "");
template.addTextSegment(referenceExpression.getReference().getCanonicalText() + " ");
template.addVariable(new ConstantNode(getTypeName(referenceExpression, file)), true);
template.addTextSegment("\n");
editor.getCaretModel().moveToOffset(anchor.getTextRange().getEndOffset() + 1);
template.setToReformat(true);
TemplateManager.getInstance(project).startTemplate(editor, template);
}


private static String getTypeName(GoReferenceExpression referenceExpression, PsiFile file) {
GoAssignmentStatement assignment = PsiTreeUtil.getParentOfType(referenceExpression, GoAssignmentStatement.class);
if (assignment == null) return GoConstants.INTERFACE_TYPE;
GoExpression expression = GoPsiImplUtil.getRightExpression(assignment, referenceExpression);
GoType type = expression != null ? expression.getGoType(null) : null;

if (type instanceof GoSpecType) {
GoSpecType spec = (GoSpecType)type;
GoFile typeFile = ObjectUtils.tryCast(spec.getContainingFile(), GoFile.class);
if (typeFile != null && (file.isEquivalentTo(typeFile) || GoUtil.inSamePackage(typeFile, file))) {
return spec.getIdentifier().getText();
}
}
return type != null ? GoPsiImplUtil.getText(type) : GoConstants.INTERFACE_TYPE;
}

@Nullable
private static GoStructType resolveStructType(@NotNull GoReferenceExpression referenceExpression) {
GoReferenceExpression qualifier = referenceExpression.getQualifier();
GoSpecType type = qualifier != null ? ObjectUtils.tryCast(qualifier.getGoType(null), GoSpecType.class) : null;
return type != null ? ObjectUtils.tryCast(type.getType(), GoStructType.class) : null;
}

@Nls
@NotNull
@Override
public String getFamilyName() {
return QUICK_FIX_NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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;
Expand Down Expand Up @@ -74,7 +75,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(o)};
}
else if (isProhibited(o, qualifier)) {
fixes = createImportPackageFixes(o, reference, holder.isOnTheFly());
}
else if (holder.isOnTheFly()) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/goide/psi/impl/GoElementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static GoIfStatement createIfStatement(@NotNull Project project,
public static GoImportDeclaration createEmptyImportDeclaration(@NotNull Project project) {
return PsiTreeUtil.findChildOfType(createFileFromText(project, "package main\nimport (\n\n)"), GoImportDeclaration.class);
}

@NotNull
public static GoImportDeclaration createImportDeclaration(@NotNull Project project, @NotNull String importString,
@Nullable String alias, boolean withParens) {
Expand Down
11 changes: 11 additions & 0 deletions testData/quickfixes/add-struct-field/blank.go
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"
}
12 changes: 12 additions & 0 deletions testData/quickfixes/add-struct-field/complexType-after.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

type S struct {
bb interface{}
cc interface{}
aa S
}

func main() {
s := S{}
s.aa = S{}
}
11 changes: 11 additions & 0 deletions testData/quickfixes/add-struct-field/complexType.go
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> = S{}
}
12 changes: 12 additions & 0 deletions testData/quickfixes/add-struct-field/simple-after.go
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"
}
11 changes: 11 additions & 0 deletions testData/quickfixes/add-struct-field/simple.go
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"
}
12 changes: 12 additions & 0 deletions testData/quickfixes/add-struct-field/withoutAssignment-after.go
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
}
11 changes: 11 additions & 0 deletions testData/quickfixes/add-struct-field/withoutAssignment.go
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>
}
10 changes: 10 additions & 0 deletions testData/quickfixes/add-struct-field/withoutElements-after.go
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 = "aa"
}
10 changes: 10 additions & 0 deletions testData/quickfixes/add-struct-field/withoutElements.go
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"
}
45 changes: 45 additions & 0 deletions tests/com/goide/quickfix/GoAddStructFieldQuickFixTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 testComplexType() { doTest(ADD_STRUCT_FIELD); }
public void testWithoutAssignment() { doTest(ADD_STRUCT_FIELD); }
public void testBlank() { doTestNoFix(ADD_STRUCT_FIELD, true); }
}