|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.inspections.graphqls.fix; |
| 7 | + |
| 8 | +import com.google.common.base.Strings; |
| 9 | +import com.intellij.codeInspection.LocalQuickFix; |
| 10 | +import com.intellij.codeInspection.ProblemDescriptor; |
| 11 | +import com.intellij.openapi.project.Project; |
| 12 | +import com.intellij.openapi.util.text.StringUtil; |
| 13 | +import com.magento.idea.magento2plugin.actions.generation.NewGraphQlResolverAction; |
| 14 | +import com.magento.idea.magento2plugin.actions.generation.data.GraphQlResolverFileData; |
| 15 | +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleGraphQlResolverClassGenerator; |
| 16 | +import com.magento.idea.magento2plugin.bundles.InspectionBundle; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import org.jetbrains.annotations.Nls; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | + |
| 23 | +public class CreateResolverClassQuickFix implements LocalQuickFix { |
| 24 | + @Override |
| 25 | + public @Nls(capitalization = Nls.Capitalization.Sentence) @NotNull String getFamilyName() { |
| 26 | + return new InspectionBundle().message( |
| 27 | + "inspection.graphql.schema.resolver.fix.family" |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void applyFix( |
| 33 | + @NotNull final Project project, |
| 34 | + @NotNull final ProblemDescriptor descriptor |
| 35 | + ) { |
| 36 | + final String resolverFqn = StringUtil.unquoteString(descriptor.getPsiElement().getText()) |
| 37 | + .replace("\\\\", "\\"); |
| 38 | + final List<String> fqnPartsList |
| 39 | + = new ArrayList<>(Arrays.asList(resolverFqn.split("\\\\"))); |
| 40 | + |
| 41 | + fqnPartsList.removeIf(Strings::isNullOrEmpty); |
| 42 | + |
| 43 | + final int endIndex = fqnPartsList.size() - 1; |
| 44 | + final String moduleName = String.join("_", fqnPartsList.subList(0, 2)); |
| 45 | + final String resolverName = fqnPartsList.get(endIndex); |
| 46 | + final String directory = fqnPartsList.get(2); |
| 47 | + final String namespace = String.join("\\", fqnPartsList.subList(0, endIndex)); |
| 48 | + |
| 49 | + final GraphQlResolverFileData graphQlResolverFileData = new GraphQlResolverFileData( |
| 50 | + directory, |
| 51 | + resolverName, |
| 52 | + moduleName, |
| 53 | + resolverFqn, |
| 54 | + namespace |
| 55 | + ); |
| 56 | + |
| 57 | + final ModuleGraphQlResolverClassGenerator generator |
| 58 | + = new ModuleGraphQlResolverClassGenerator(graphQlResolverFileData, project); |
| 59 | + |
| 60 | + generator.generate(NewGraphQlResolverAction.ACTION_NAME, true); |
| 61 | + } |
| 62 | +} |
0 commit comments