[groovy] hook into common 'Expression Type' action and get rid of GrGetPsiTypeAction

This commit is contained in:
Daniil Ovchinnikov
2015-09-28 11:24:15 +03:00
parent f115b71b44
commit 99384c8304
4 changed files with 54 additions and 124 deletions
+1 -4
View File
@@ -280,6 +280,7 @@
<codeInsight.createFieldFromUsageHelper language="Groovy"
implementationClass="org.jetbrains.plugins.groovy.annotator.intentions.GroovyCreateFieldFromUsageHelper"/>
<codeInsight.typeInfo language="Groovy" implementationClass="org.jetbrains.plugins.groovy.codeInsight.hint.GroovyExpressionTypeProvider"/>
<errorHandler implementation="com.intellij.diagnostic.ITNReporter"/>
<fileTypeFactory implementation="org.jetbrains.plugins.groovy.GroovyFileTypeLoader"/>
@@ -1615,10 +1616,6 @@
</action>
<group id="Internal.Groovy" text="Groovy" popup="true" internal="true">
<action id="GetPsiTypeAction" class="org.jetbrains.plugins.groovy.actions.GrGetPsiTypeAction"
text="get PsiType" description=""
internal="true"/>
<action id="DumpGroovyControlFlowAction"
class="org.jetbrains.plugins.groovy.actions.DumpGroovyControlFlowAction"
text="dump groovy control flow"
@@ -1,120 +0,0 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.actions;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Pass;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiType;
import com.intellij.refactoring.IntroduceTargetChooser;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Function;
import org.jetbrains.plugins.groovy.editor.HandlerUtils;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceHandlerBase;
import javax.swing.*;
import java.awt.*;
import java.util.List;
/**
* @author Maxim.Medvedev
*/
public class GrGetPsiTypeAction extends AnAction implements DumbAware {
@Override
public void actionPerformed(AnActionEvent e) {
final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
if (editor == null) return;
final PsiFile psiFile = HandlerUtils.getPsiFile(editor, e.getDataContext());
if (!(psiFile instanceof GroovyFile)) return;
int offset = editor.getCaretModel().getOffset();
List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(psiFile, editor, offset, true);
if (expressions.isEmpty()) return;
if (expressions.size() == 1) {
passInner(expressions.get(0));
}
else {
IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {
@Override
public void pass(GrExpression grExpression) {
passInner(grExpression);
}
}, new Function<GrExpression, String>() {
@Override
public String fun(GrExpression grExpression) {
return grExpression.getText();
}
});
}
}
/**
* Shows an information balloon in a reasonable place at the top right of the window.
*
* @param project our project
* @param message the text, HTML markup allowed
* @param messageType message type, changes the icon and the background.
*/
// TODO: move to a better place
public static void showBalloon(Project project, String message, MessageType messageType) {
// ripped from com.intellij.openapi.vcs.changes.ui.ChangesViewBalloonProblemNotifier
final JFrame frame = WindowManager.getInstance().getFrame(project.isDefault() ? null : project);
if (frame == null) return;
final JComponent component = frame.getRootPane();
if (component == null) return;
final Rectangle rect = component.getVisibleRect();
final Point p = new Point(rect.x + rect.width - 10, rect.y + 10);
final RelativePoint point = new RelativePoint(component, p);
JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(message, messageType.getDefaultIcon(), messageType.getPopupBackground(), null)
.setShowCallout(false).setCloseButtonEnabled(true)
.createBalloon().show(point, Balloon.Position.atLeft);
}
public static void passInner(GrExpression expression) {
PsiType type = expression.getType();
String text = type == null ? "type is null" : type.getCanonicalText();
showBalloon(expression.getProject(), StringUtil.escapeXml(text), MessageType.INFO);
}
@Override
public void update(AnActionEvent e) {
Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
if (editor != null) {
PsiFile psiFile = HandlerUtils.getPsiFile(editor, e.getDataContext());
if (psiFile instanceof GroovyFile) {
e.getPresentation().setEnabled(true);
return;
}
}
e.getPresentation().setEnabled(false);
}
}
@@ -0,0 +1,48 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.codeInsight.hint;
import com.intellij.lang.ExpressionTypeProvider;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceHandlerBase;
import java.util.List;
public class GroovyExpressionTypeProvider extends ExpressionTypeProvider<GrExpression> {
@NotNull
@Override
public String getInformationHint(@NotNull GrExpression element) {
final PsiType type = element.getType();
return type == null ? "<unknown>" : StringUtil.escapeXml(type.getCanonicalText());
}
@NotNull
@Override
public String getErrorHint() {
return "No expression found";
}
@NotNull
@Override
public List<GrExpression> getExpressionsAt(@NotNull PsiElement elementAt) {
return GrIntroduceHandlerBase.collectExpressions(elementAt, true);
}
}
@@ -202,6 +202,11 @@ public abstract class GrIntroduceHandlerBase<Settings extends GrIntroduceSetting
public static List<GrExpression> collectExpressions(final PsiFile file, final Editor editor, final int offset, boolean acceptVoidCalls) {
int correctedOffset = correctOffset(editor, offset);
final PsiElement elementAtCaret = file.findElementAt(correctedOffset);
return collectExpressions(elementAtCaret, acceptVoidCalls);
}
@NotNull
public static List<GrExpression> collectExpressions(PsiElement elementAtCaret, boolean acceptVoidCalls) {
final List<GrExpression> expressions = new ArrayList<GrExpression>();
for (GrExpression expression = PsiTreeUtil.getParentOfType(elementAtCaret, GrExpression.class);