|
| 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.*; |
| 20 | +import com.goide.psi.impl.GoElementFactory; |
| 21 | +import com.intellij.codeInspection.LocalQuickFixBase; |
| 22 | +import com.intellij.codeInspection.ProblemDescriptor; |
| 23 | +import com.intellij.openapi.command.WriteCommandAction; |
| 24 | +import com.intellij.openapi.project.Project; |
| 25 | +import com.intellij.psi.PsiElement; |
| 26 | +import com.intellij.psi.PsiFile; |
| 27 | +import com.intellij.psi.PsiRecursiveElementVisitor; |
| 28 | +import com.intellij.psi.PsiReference; |
| 29 | +import com.intellij.psi.util.PsiTreeUtil; |
| 30 | +import com.intellij.util.containers.ContainerUtil; |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | + |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +public class GoSelfImportQuickFix extends LocalQuickFixBase { |
| 36 | + |
| 37 | + protected GoSelfImportQuickFix(@NotNull String name) { |
| 38 | + super(name); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) { |
| 43 | + final PsiElement element = descriptor.getPsiElement(); |
| 44 | + final PsiFile file = element != null ? element.getContainingFile() : null; |
| 45 | + if (!(element instanceof GoImportSpec && file instanceof GoFile)) return; |
| 46 | + |
| 47 | + final Map<GoReferenceExpressionBase, String> usages = ContainerUtil.newHashMap(); |
| 48 | + file.acceptChildren(new PsiRecursiveElementVisitor() { |
| 49 | + public void visitElement(@NotNull PsiElement element) { |
| 50 | + if (element instanceof GoReferenceExpression || element instanceof GoTypeReferenceExpression) { |
| 51 | + PsiReference reference = element.getReference(); |
| 52 | + PsiElement definition = reference != null ? reference.resolve() : null; |
| 53 | + PsiFile definitionFile = definition instanceof GoNamedElement ? definition.getContainingFile() : null; |
| 54 | + if (definitionFile != null && definitionFile.getParent() == file.getParent()) { |
| 55 | + usages.put((GoReferenceExpressionBase)element, ((GoNamedElement)definition).getName()); |
| 56 | + return; |
| 57 | + } |
| 58 | + } |
| 59 | + super.visitElement(element); |
| 60 | + } |
| 61 | + }); |
| 62 | + WriteCommandAction.runWriteCommandAction(project, new Runnable() { |
| 63 | + @Override |
| 64 | + public void run() { |
| 65 | + for (Map.Entry<GoReferenceExpressionBase, String> usage : usages.entrySet()) { |
| 66 | + GoReferenceExpressionBase expression = usage.getKey(); |
| 67 | + expression.replace(expression instanceof GoReferenceExpression |
| 68 | + ? GoElementFactory.createReferenceExpression(project, usage.getValue()) |
| 69 | + : GoElementFactory.createTypeReferenceExpression(project, usage.getValue())); |
| 70 | + } |
| 71 | + GoImportDeclaration importDeclaration = PsiTreeUtil.getParentOfType(element, GoImportDeclaration.class); |
| 72 | + assert importDeclaration != null; |
| 73 | + PsiElement elementToDelete = importDeclaration.getImportSpecList().size() == 1 ? importDeclaration : element; |
| 74 | + elementToDelete.delete(); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
0 commit comments