IDEA-51893: Quick Fix "​Create Field" when using Groovy named parameters
[fedora-idea.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / annotator / intentions / CreateFieldFromConstructorLabelFix.java
blob798bc31cd9c888ea5504b9a14fbc92628adfc74c
1 /*
2 * Copyright 2000-2010 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.jetbrains.plugins.groovy.annotator.intentions;
18 import com.intellij.openapi.editor.Editor;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.psi.PsiFile;
21 import com.intellij.psi.PsiType;
22 import com.intellij.util.ArrayUtil;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.annotations.Nullable;
25 import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel;
26 import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument;
27 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
28 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrMemberOwner;
29 import org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SupertypeConstraint;
30 import org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint;
32 /**
33 * @author Maxim.Medvedev
35 public class CreateFieldFromConstructorLabelFix extends CreateFieldFix {
36 private final GrNamedArgument myNamedArgument;
38 public CreateFieldFromConstructorLabelFix(GrMemberOwner targetClass, GrNamedArgument namedArgument) {
39 super(targetClass);
40 myNamedArgument = namedArgument;
43 @Nullable
44 @Override
45 protected String getFieldName() {
46 final GrArgumentLabel label = myNamedArgument.getLabel();
47 assert label != null;
48 return label.getName();
51 @Override
52 protected TypeConstraint[] calculateTypeConstrains() {
53 final GrExpression expression = myNamedArgument.getExpression();
54 PsiType type = null;
55 if (expression != null) {
56 type = expression.getType();
58 return new TypeConstraint[]{SupertypeConstraint.create(type, type)};
61 @Override
62 protected String[] generateModifiers() {
63 return ArrayUtil.EMPTY_STRING_ARRAY;
66 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
67 return super.isAvailable(project, editor, file) && myNamedArgument.isValid();