ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / intentions / conversions / ConvertMethodToClosureIntention.java
blob5fb904c346c1f653008a5008e76841031b71a35b
1 /*
2 * Copyright 2000-2009 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.
17 package org.jetbrains.plugins.groovy.intentions.conversions;
19 import com.intellij.psi.PsiElement;
20 import com.intellij.util.IncorrectOperationException;
21 import org.jetbrains.annotations.NotNull;
22 import org.jetbrains.plugins.groovy.intentions.base.Intention;
23 import org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate;
24 import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
25 import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifier;
26 import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration;
27 import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
28 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody;
29 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
32 /**
33 * @author Maxim.Medvedev
35 public class ConvertMethodToClosureIntention extends Intention {
36 @NotNull
37 @Override
38 protected PsiElementPredicate getElementPredicate() {
39 return new MyPredicate();
42 @Override
43 protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
44 element = element.getParent();
45 final GrMethod method = (GrMethod)element;
46 StringBuilder builder = new StringBuilder(method.getTextLength());
47 String modifiers = method.getModifierList().getText();
48 if (modifiers.trim().length() == 0) {
49 modifiers = GrModifier.DEF;
51 builder.append(modifiers).append(' ');
52 builder.append(method.getName()).append("={");
53 builder.append(method.getParameterList().getText()).append(" ->");
54 final GrOpenBlock block = method.getBlock();
55 builder.append(block.getText().substring(1));
56 final GrVariableDeclaration variableDeclaration =
57 GroovyPsiElementFactory.getInstance(element.getProject()).createFieldDeclarationFromText(builder.toString());
58 method.replace(variableDeclaration);
61 private static class MyPredicate implements PsiElementPredicate {
62 public boolean satisfiedBy(PsiElement element) {
63 final PsiElement parent = element.getParent();
64 return parent instanceof GrMethod &&
65 element == ((GrMethod)parent).getNameIdentifierGroovy() &&
66 ((GrMethod)parent).getBlock() != null &&
67 parent.getParent() instanceof GrTypeDefinitionBody;
68 // return element instanceof GrMethod && ((GrMethod)element).getBlock() != null && element.getParent() instanceof GrTypeDefinitionBody;