ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / refactoring / inline / GroovyInlineVariableUtil.java
blobf30b9423de32c2bf14ea22a903d1b19fd01980bd
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.refactoring.inline;
19 import com.intellij.lang.refactoring.InlineHandler;
20 import com.intellij.openapi.application.Application;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.openapi.fileEditor.FileEditorManager;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.wm.WindowManager;
26 import com.intellij.psi.PsiElement;
27 import com.intellij.psi.PsiReference;
28 import com.intellij.psi.search.searches.ReferencesSearch;
29 import com.intellij.refactoring.HelpID;
30 import com.intellij.refactoring.util.CommonRefactoringUtil;
31 import com.intellij.refactoring.util.RefactoringMessageDialog;
32 import com.intellij.usageView.UsageInfo;
33 import org.jetbrains.annotations.Nullable;
34 import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
35 import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
36 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression;
37 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
38 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression;
39 import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle;
40 import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil;
42 import java.util.*;
44 /**
45 * @author ilyas
47 public class GroovyInlineVariableUtil {
49 public static final String REFACTORING_NAME = GroovyRefactoringBundle.message("inline.variable.title");
51 private GroovyInlineVariableUtil() {
55 /**
56 * Creates new inliner for local variable occurences
58 static InlineHandler.Inliner createInlinerForLocalVariable(final GrVariable variable) {
59 return new InlineHandler.Inliner() {
61 @Nullable
62 public Map<PsiElement, String> getConflicts(PsiReference reference, PsiElement referenced) {
63 Map<PsiElement, String> conflicts = new HashMap<PsiElement, String>();
64 GrExpression expr = (GrExpression) reference.getElement();
65 if (expr.getParent() instanceof GrAssignmentExpression) {
66 GrAssignmentExpression parent = (GrAssignmentExpression) expr.getParent();
67 if (expr.equals(parent.getLValue())) {
68 conflicts.put(expr, GroovyRefactoringBundle.message("local.varaible.is.lvalue"));
71 return conflicts;
74 public void inlineUsage(final UsageInfo usage, final PsiElement referenced) {
75 GrExpression exprToBeReplaced = (GrExpression)usage.getElement();
76 if (exprToBeReplaced == null) return;
77 assert variable.getInitializerGroovy() != null;
78 GrExpression initializerGroovy = variable.getInitializerGroovy();
79 assert initializerGroovy != null;
80 GrExpression tempExpr = initializerGroovy;
81 while (tempExpr instanceof GrParenthesizedExpression) {
82 tempExpr = ((GrParenthesizedExpression)tempExpr).getOperand();
84 Project project = variable.getProject();
85 GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
86 GrExpression newExpr;
87 newExpr = factory.createExpressionFromText(tempExpr.getText());
88 newExpr = exprToBeReplaced.replaceWithExpression(newExpr, true);
89 FileEditorManager manager = FileEditorManager.getInstance(project);
90 Editor editor = manager.getSelectedTextEditor();
91 GroovyRefactoringUtil.highlightOccurrences(project, editor, new PsiElement[]{newExpr});
92 WindowManager.getInstance().getStatusBar(project)
93 .setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
98 /**
99 * Returns Settings object for referenced definition in case of local variable
101 @Nullable
102 static InlineHandler.Settings inlineLocalVariableSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
103 final String localName = variable.getName();
104 final Project project = variable.getProject();
105 final Collection<PsiReference> refs = ReferencesSearch.search(variable).findAll();
106 ArrayList<PsiElement> exprs = new ArrayList<PsiElement>();
107 for (PsiReference ref : refs) {
108 exprs.add(ref.getElement());
111 GroovyRefactoringUtil.highlightOccurrences(project, editor, exprs.toArray(new PsiElement[exprs.size()]));
112 if (variable.getInitializerGroovy() == null) {
113 String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
114 CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, REFACTORING_NAME, HelpID.INLINE_VARIABLE);
115 return null;
117 if (refs.isEmpty()) {
118 String message = GroovyRefactoringBundle.message("variable.is.never.used.0", variable.getName());
119 CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, REFACTORING_NAME, HelpID.INLINE_VARIABLE);
120 return null;
123 return inlineDialogResult(localName, project, refs);
127 * Shows dialog with question to inline
129 @Nullable
130 private static InlineHandler.Settings inlineDialogResult(String localName, Project project, Collection<PsiReference> refs) {
131 Application application = ApplicationManager.getApplication();
132 if (!application.isUnitTestMode()) {
133 final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName, refs.size());
134 RefactoringMessageDialog dialog = new RefactoringMessageDialog(
135 REFACTORING_NAME,
136 question,
137 HelpID.INLINE_VARIABLE,
138 "OptionPane.questionIcon",
139 true,
140 project);
141 dialog.show();
142 if (!dialog.isOK()) {
143 WindowManager.getInstance().getStatusBar(project).setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
144 return null;
148 return new InlineHandler.Settings() {
149 public boolean isOnlyOneReferenceToInline() {
150 return false;