IDEA-51893: Quick Fix "​Create Field" when using Groovy named parameters
[fedora-idea.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / annotator / intentions / dynamic / DynamicMethodFix.java
blob2da0f65cd2445b2e4c03ea97fa2de4808a76866a
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.dynamic;
18 import com.intellij.codeInsight.intention.IntentionAction;
19 import com.intellij.openapi.editor.Editor;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.psi.PsiFile;
22 import com.intellij.psi.PsiType;
23 import com.intellij.util.IncorrectOperationException;
24 import org.jetbrains.annotations.NotNull;
25 import org.jetbrains.plugins.groovy.GroovyBundle;
26 import org.jetbrains.plugins.groovy.annotator.intentions.QuickfixUtil;
27 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.ui.DynamicDialog;
28 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.ui.DynamicElementSettings;
29 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.ui.DynamicMethodDialog;
30 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
31 import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
33 /**
34 * @author Maxim.Medvedev
36 public class DynamicMethodFix implements IntentionAction {
37 private final GrReferenceExpression myReferenceExpression;
39 public DynamicMethodFix(GrReferenceExpression referenceExpression) {
40 myReferenceExpression = referenceExpression;
43 @NotNull
44 public String getText() {
45 final PsiType[] methodArgumentsTypes = PsiUtil.getArgumentTypes(myReferenceExpression, false, false);
46 StringBuilder builder = new StringBuilder(" '").append(myReferenceExpression.getName());
47 builder.append("(");
49 assert methodArgumentsTypes != null;
50 for (int i = 0; i < methodArgumentsTypes.length; i++) {
51 PsiType type = methodArgumentsTypes[i];
53 if (i > 0) {
54 builder.append(", ");
56 builder.append(type.getPresentableText());
58 builder.append(")");
59 builder.append("' ");
61 return GroovyBundle.message("add.dynamic.method") + builder.toString();
64 @NotNull
65 public String getFamilyName() {
66 return GroovyBundle.message("add.dynamic.element");
69 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile psiFile) {
70 return myReferenceExpression.isValid();
73 public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {
74 DynamicDialog dialog = new DynamicMethodDialog(myReferenceExpression);
75 dialog.show();
78 public void invoke(Project project) throws IncorrectOperationException {
79 final DynamicElementSettings settings = QuickfixUtil.createSettings(myReferenceExpression);
80 DynamicManager.getInstance(project).addMethod(settings);
83 public boolean startInWriteAction() {
84 return false;
87 public GrReferenceExpression getReferenceExpression() {
88 return myReferenceExpression;