IDEA-73251 Mark recursive method calls on gutter reverted to initial state

This commit is contained in:
Danila Ponomarenko
2012-06-07 15:20:08 +04:00
parent ee253392a0
commit c589a13110
9 changed files with 41 additions and 272 deletions
@@ -36,6 +36,5 @@ public class JavaCodeFoldingOptionsProvider extends BeanConfigurable<JavaCodeFol
checkBox("COLLAPSE_I18N_MESSAGES", ApplicationBundle.message("checkbox.collapse.i18n.messages"));
checkBox("COLLAPSE_SUPPRESS_WARNINGS", ApplicationBundle.message("checkbox.collapse.suppress.warnings"));
checkBox("COLLAPSE_END_OF_LINE_COMMENTS", ApplicationBundle.message("checkbox.collapse.end.of.line.comments"));
checkBox("COLLAPSE_RECURSIVE_METHOD_CALLS", ApplicationBundle.message("checkbox.collapse.recursive.method.calls"));
}
}
@@ -1,44 +0,0 @@
/*
* Copyright 2000-2012 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 com.intellij.codeInsight;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiMethodCallExpression;
import com.intellij.psi.PsiThisExpression;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
/**
* @author Danila Ponomarenko
*/
public class RecursionUtil {
public static boolean isRecursiveMethodCall(@NotNull PsiMethodCallExpression methodCall) {
final PsiMethod method = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class);
if (method == null) {
return false;
}
final PsiMethod resolvedMethod = methodCall.resolveMethod();
if (!Comparing.equal(method, resolvedMethod)) {
return false;
}
final PsiExpression qualifier = methodCall.getMethodExpression().getQualifierExpression();
return qualifier == null || qualifier instanceof PsiThisExpression;
}
}
@@ -16,33 +16,23 @@
package com.intellij.codeInsight.daemon.impl;
import com.intellij.codeHighlighting.Pass;
import com.intellij.codeInsight.RecursionUtil;
import com.intellij.codeInsight.daemon.GutterIconNavigationHandler;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBList;
import com.intellij.util.Function;
import com.intellij.util.FunctionUtil;
import com.intellij.util.NotNullFunction;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author Danila Ponomarenko
@@ -56,138 +46,56 @@ public class RecursiveCallLineMarkerProvider implements LineMarkerProvider, Dumb
}
@Override
public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
final Set<PsiMethod> recMethods = new HashSet<PsiMethod>();
public void collectSlowLineMarkers(@NotNull List<PsiElement> elements,
@NotNull Collection<LineMarkerInfo> result) {
final Set<PsiStatement> statements = new HashSet<PsiStatement>();
for (PsiElement element : elements) {
if (element instanceof PsiMethodCallExpression) {
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)element;
final PsiMethod method = methodCall.resolveMethod();
if (method != null && !recMethods.contains(method) && RecursionUtil.isRecursiveMethodCall(methodCall)) {
recMethods.add(method);
result.add(RecursiveMethodMarkerInfo.create(method));
final PsiStatement statement = PsiTreeUtil.getParentOfType(methodCall, PsiStatement.class, true, PsiMethod.class);
if (!statements.contains(statement) && isRecursiveMethodCall(methodCall)) {
statements.add(statement);
result.add(new RecursiveMethodCallMarkerInfo(methodCall));
}
}
}
}
@NotNull
private static List<PsiMethodCallExpression> getRecursiveMethodCalls(final @NotNull PsiMethod method) {
final List<PsiMethodCallExpression> result = new ArrayList<PsiMethodCallExpression>();
ApplicationManager.getApplication().runReadAction(new Runnable() {
@Override
public void run() {
method.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
if (RecursionUtil.isRecursiveMethodCall(expression)) {
result.add(expression);
}
super.visitMethodCallExpression(expression);
}
});
}
});
return result;
}
private static class RecursiveMethodMarkerInfo extends LineMarkerInfo<PsiMethod> {
public RecursiveMethodMarkerInfo(PsiMethod method,
TextRange range,
Icon icon,
int markers,
Function<PsiMethod, String> constant,
GutterIconNavigationHandler<PsiMethod> handler,
GutterIconRenderer.Alignment left) {
super(method, range, icon, markers, constant, handler, left);
public static boolean isRecursiveMethodCall(@NotNull PsiMethodCallExpression methodCall) {
final PsiMethod method = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class);
if (method == null) {
return false;
}
@NotNull
public static RecursiveMethodMarkerInfo create(@NotNull PsiMethod method){
final PsiIdentifier identifier = method.getNameIdentifier();
return new RecursiveMethodMarkerInfo(method,
identifier != null ? identifier.getTextRange() : method.getTextRange(),
RECURSIVE_METHOD_ICON,
Pass.UPDATE_OVERRIDEN_MARKERS,
FunctionUtil.<PsiMethod, String>constant("Potentially recursive method"),
getNavigationHandler(method),
GutterIconRenderer.Alignment.LEFT
final PsiMethod resolvedMethod = methodCall.resolveMethod();
if (!Comparing.equal(method, resolvedMethod)) {
return false;
}
final PsiExpression qualifier = methodCall.getMethodExpression().getQualifierExpression();
return qualifier == null || qualifier instanceof PsiThisExpression;
}
private static class RecursiveMethodCallMarkerInfo extends LineMarkerInfo<PsiMethodCallExpression> {
private RecursiveMethodCallMarkerInfo(@NotNull PsiMethodCallExpression methodCall) {
super(methodCall,
methodCall.getTextRange(),
RECURSIVE_METHOD_ICON,
Pass.UPDATE_OVERRIDEN_MARKERS,
FunctionUtil.<PsiMethodCallExpression, String>constant("Recursive call"),
null,
GutterIconRenderer.Alignment.RIGHT
);
}
private static GutterIconNavigationHandler<PsiMethod> getNavigationHandler(final @NotNull PsiMethod method) {
return new GutterIconNavigationHandler<PsiMethod>() {
@Override
public GutterIconRenderer createGutterRenderer() {
if (myIcon == null) return null;
return new LineMarkerGutterIconRenderer<PsiMethodCallExpression>(this){
@Override
public void navigate(@NotNull MouseEvent e, PsiMethod elt) {
final List<PsiMethodCallExpression> calls = getRecursiveMethodCalls(method);
if (calls.size() == 1) {
navigateToMethodCall(calls.get(0));
}
else {
showPopup(e, calls);
}
}
};
}
private static void showPopup(@NotNull MouseEvent e, @NotNull List<PsiMethodCallExpression> calls) {
final JBList list = new JBList(calls);
list.setFixedCellHeight(20);
list.installCellRenderer(createCellRenderer());
JBPopupFactory.getInstance().
createListPopupBuilder(list).
setItemChoosenCallback(createItemChosenCallback(e, list)).
createPopup().show(new RelativePoint(e));
}
@NotNull
private static Runnable createItemChosenCallback(final MouseEvent e, final @NotNull JBList list) {
return new Runnable() {
@Override
public void run() {
final Object value = list.getSelectedValue();
if (!(value instanceof PsiMethodCallExpression)) {
return;
}
navigateToMethodCall((PsiMethodCallExpression)value);
}
};
}
private static void navigateToMethodCall(@NotNull PsiMethodCallExpression methodCall) {
final PsiIdentifier identifier = PsiTreeUtil.getChildOfType(methodCall.getMethodExpression(), PsiIdentifier.class);
if (identifier != null) {
navigateTo(identifier);
}else {
navigateTo(methodCall);
}
}
private static void navigateTo(@NotNull PsiElement element) {
final Editor editor = PsiUtilBase.findEditor(element);
if (editor == null) return;
editor.getCaretModel().moveToOffset(element.getTextRange().getStartOffset());
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
@NotNull
private static NotNullFunction<Object, JComponent> createCellRenderer() {
return new NotNullFunction<Object, JComponent>() {
@NotNull
@Override
public JComponent fun(Object o) {
if (!(o instanceof PsiMethodCallExpression)) {
return new JBLabel();
}
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)o;
String text = StringUtil.first(methodCall.getText(), 100, true).replace('\n', ' ');
return new JBLabel(text, RECURSIVE_METHOD_ICON, SwingConstants.LEFT);
public AnAction getClickAction() {
return null; // to place breakpoint on mouse click
}
};
}
@@ -165,16 +165,6 @@ public class JavaCodeFoldingSettingsImpl extends JavaCodeFoldingSettings impleme
COLLAPSE_END_OF_LINE_COMMENTS = value;
}
@Override
public boolean isCollapseRecursiveMethodCalls() {
return COLLAPSE_RECURSIVE_METHOD_CALLS;
}
@Override
public void setCollapseRecursiveMethodCalls(boolean value) {
COLLAPSE_RECURSIVE_METHOD_CALLS = value;
}
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_ACCESSORS = false;
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_INNER_CLASSES = false;
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_ANONYMOUS_CLASSES = false;
@@ -184,7 +174,6 @@ public class JavaCodeFoldingSettingsImpl extends JavaCodeFoldingSettings impleme
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_I18N_MESSAGES = true;
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_SUPPRESS_WARNINGS = true;
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_END_OF_LINE_COMMENTS = false;
@SuppressWarnings({"WeakerAccess"}) public boolean COLLAPSE_RECURSIVE_METHOD_CALLS = false;
@Override
@NotNull
@@ -1,78 +0,0 @@
/*
* Copyright 2000-2012 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 com.intellij.codeInsight.folding.impl;
import com.intellij.codeInsight.RecursionUtil;
import com.intellij.codeInsight.folding.JavaCodeFoldingSettings;
import com.intellij.lang.ASTNode;
import com.intellij.lang.folding.FoldingBuilderEx;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* @author Danila Ponomarenko
*/
public class RecursiveMethodCallFoldingBuilder extends FoldingBuilderEx {
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
if (!(root instanceof PsiJavaFile) || quick || !JavaCodeFoldingSettings.getInstance().isCollapseRecursiveMethodCalls()) {
return FoldingDescriptor.EMPTY;
}
final List<FoldingDescriptor> result = new ArrayList<FoldingDescriptor>();
root.accept(
new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
if (RecursionUtil.isRecursiveMethodCall(expression)) {
final PsiIdentifier identifier = PsiTreeUtil.getChildOfType(expression.getMethodExpression(), PsiIdentifier.class);
if (identifier != null) {
result.add(new FoldingDescriptor(identifier, identifier.getTextRange()));
}
}
super.visitMethodCallExpression(expression);
}
}
);
return result.toArray(new FoldingDescriptor[result.size()]);
}
@Override
public boolean isCollapsedByDefault(@NotNull ASTNode node) {
return JavaCodeFoldingSettings.getInstance().isCollapseRecursiveMethodCalls();
}
private static final String ANTICLOCKWISE_GAPPED_CIRCLE_ARROW = "\u27F2"; //⟲
@Override
public String getPlaceholderText(@NotNull ASTNode node) {
final PsiElement element = node.getPsi();
if (element instanceof PsiIdentifier) {
final PsiIdentifier identifier = (PsiIdentifier)element;
return ANTICLOCKWISE_GAPPED_CIRCLE_ARROW + identifier.getText();
}
return element.getText();
}
}
@@ -62,7 +62,4 @@ public abstract class JavaCodeFoldingSettings {
public abstract boolean isCollapseEndOfLineComments();
public abstract void setCollapseEndOfLineComments(boolean value);
public abstract boolean isCollapseRecursiveMethodCalls();
public abstract void setCollapseRecursiveMethodCalls(boolean value);
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 454 B

After

Width:  |  Height:  |  Size: 490 B

@@ -348,7 +348,6 @@ checkbox.collapse.method.bodies=Method bodies
checkbox.collapse.javadoc.comments=Documentation comments
checkbox.collapse.title.imports=Imports
checkbox.collapse.file.header=File header
checkbox.collapse.recursive.method.calls=<html>Recursive method calls<html>
label.collapse.by.default=Collapse by default:
checkbox.show.code.folding.outline=Show code folding outline
group.tab.appearance=Tab Appearance
-1
View File
@@ -749,7 +749,6 @@
<lang.braceMatcher language="JAVA" implementationClass="com.intellij.codeInsight.highlighting.JavaBraceMatcher"/>
<lang.foldingBuilder language="JAVA" implementationClass="com.intellij.codeInsight.folding.impl.JavaFoldingBuilder"/>
<lang.foldingBuilder language="JAVA" implementationClass="com.intellij.codeInsight.folding.impl.RecursiveMethodCallFoldingBuilder"/>
<codeInsight.parameterInfo language="JAVA" implementationClass="com.intellij.codeInsight.hint.api.impls.MethodParameterInfoHandler"/>
<codeInsight.parameterInfo language="JAVA" implementationClass="com.intellij.codeInsight.hint.api.impls.ReferenceParameterInfoHandler"/>