summaryrefslogtreecommitdiff
path: root/java/debugger/impl/src/com/intellij/debugger/ui/ValueHint.java
blob: 8a0877273335c0752c3512222e6487057cba6f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright 2000-2011 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.debugger.ui;

import com.intellij.codeInsight.hint.HintUtil;
import com.intellij.debugger.DebuggerBundle;
import com.intellij.debugger.DebuggerInvocationUtil;
import com.intellij.debugger.DebuggerManagerEx;
import com.intellij.debugger.actions.DebuggerActions;
import com.intellij.debugger.engine.DebugProcessImpl;
import com.intellij.debugger.engine.JVMName;
import com.intellij.debugger.engine.JVMNameUtil;
import com.intellij.debugger.engine.evaluation.*;
import com.intellij.debugger.engine.evaluation.expression.EvaluatorBuilderImpl;
import com.intellij.debugger.engine.evaluation.expression.ExpressionEvaluator;
import com.intellij.debugger.engine.events.DebuggerContextCommandImpl;
import com.intellij.debugger.impl.DebuggerContextImpl;
import com.intellij.debugger.impl.DebuggerSession;
import com.intellij.debugger.impl.DebuggerUtilsEx;
import com.intellij.debugger.impl.EditorTextProvider;
import com.intellij.debugger.ui.impl.DebuggerTreeRenderer;
import com.intellij.debugger.ui.impl.InspectDebuggerTree;
import com.intellij.debugger.ui.impl.watch.NodeDescriptorImpl;
import com.intellij.debugger.ui.impl.watch.WatchItemDescriptor;
import com.intellij.debugger.ui.tree.render.DescriptorLabelListener;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.*;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.ui.SimpleColoredText;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.IncorrectOperationException;
import com.intellij.xdebugger.impl.evaluate.quick.common.AbstractValueHint;
import com.intellij.xdebugger.impl.evaluate.quick.common.ValueHintType;
import com.sun.jdi.Method;
import com.sun.jdi.PrimitiveValue;
import com.sun.jdi.Value;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * @author lex
 * @since Nov 24, 2003
 */
public class ValueHint extends AbstractValueHint {
  private static final Logger LOG = Logger.getInstance("#com.intellij.debugger.ui.ValueHint");
  private PsiElement myCurrentExpression = null;
  private Value myValueToShow = null;

  private ValueHint(Project project, Editor editor, Point point, ValueHintType type, final PsiElement selectedExpression, final TextRange textRange) {
    super(project, editor, point, type, textRange);
    myCurrentExpression = selectedExpression;
  }

  public static ValueHint createValueHint(Project project, Editor editor, Point point, ValueHintType type) {
    Trinity<PsiElement, TextRange, Value> trinity = getSelectedExpression(project, editor, point, type);
    final ValueHint hint = new ValueHint(project, editor, point, type, trinity.getFirst(), trinity.getSecond());
    hint.myValueToShow = trinity.getThird();
    return hint;
  }

  @Override
  protected boolean canShowHint() {
    return myCurrentExpression != null;
  }


  @Nullable
  private ExpressionEvaluator getExpressionEvaluator(DebuggerContextImpl debuggerContext) throws EvaluateException {
    if (myCurrentExpression instanceof PsiExpression) {
      return EvaluatorBuilderImpl.getInstance().build(myCurrentExpression, debuggerContext.getSourcePosition());
    }

    CodeFragmentFactory factory = DebuggerUtilsEx.getEffectiveCodeFragmentFactory(myCurrentExpression);
    TextWithImportsImpl textWithImports = new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, myCurrentExpression.getText());
    if (factory == null) return null;
    JavaCodeFragment codeFragment = factory.createCodeFragment(textWithImports, myCurrentExpression.getContext(), getProject());
    codeFragment.forceResolveScope(GlobalSearchScope.allScope(getProject()));
    return factory.getEvaluatorBuilder().build(codeFragment, debuggerContext.getSourcePosition());
  }


  @Override
  protected void evaluateAndShowHint() {
    final DebuggerContextImpl debuggerContext = DebuggerManagerEx.getInstanceEx(getProject()).getContext();

    final DebuggerSession debuggerSession = debuggerContext.getDebuggerSession();
    if(debuggerSession == null || !debuggerSession.isPaused()) return;

    try {

      final ExpressionEvaluator evaluator = getExpressionEvaluator(debuggerContext);
      if (evaluator == null) return;

      debuggerContext.getDebugProcess().getManagerThread().schedule(new DebuggerContextCommandImpl(debuggerContext) {
        @Override
        public Priority getPriority() {
          return Priority.HIGH;
        }

        @Override
        public void threadAction() {
          try {
            final EvaluationContextImpl evaluationContext = debuggerContext.createEvaluationContext();

            final String expressionText = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
              @Override
              public String compute() {
                return myCurrentExpression.getText();
              }
            });
            final TextWithImports text = new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, expressionText);
            final Value value = myValueToShow != null? myValueToShow : evaluator.evaluate(evaluationContext);

            final WatchItemDescriptor descriptor = new WatchItemDescriptor(getProject(), text, value);
            if (!isActiveTooltipApplicable(value) || getType() == ValueHintType.MOUSE_OVER_HINT) {
              if (getType() == ValueHintType.MOUSE_OVER_HINT) {
                // force using default renderer for mouse over hint in order to not to call accidentally methods while rendering
                // otherwise, if the hint is invoked explicitly, show it with the right "auto" renderer
                descriptor.setRenderer(DebugProcessImpl.getDefaultRenderer(value));
              }
              descriptor.updateRepresentation(evaluationContext, new DescriptorLabelListener() {
                @Override
                public void labelChanged() {
                  if(getCurrentRange() != null) {
                    if(getType() != ValueHintType.MOUSE_OVER_HINT || descriptor.isValueValid()) {
                      final SimpleColoredText simpleColoredText = DebuggerTreeRenderer.getDescriptorText(debuggerContext, descriptor, true);
                      if (isActiveTooltipApplicable(value)){
                        simpleColoredText.append(" (" + DebuggerBundle.message("active.tooltip.suggestion") + ")", SimpleTextAttributes.GRAYED_ATTRIBUTES);
                      }
                      showHint(simpleColoredText, descriptor);
                    }
                  }
                }
              });
            }
            else {
              createAndShowTree(expressionText, descriptor);
            }
          }
          catch (EvaluateException e) {
            LOG.debug(e);
          }
        }

      });
    }
    catch (EvaluateException e) {
      LOG.debug(e);
    }
  }

  private void createAndShowTree(final String expressionText, final NodeDescriptorImpl descriptor) {
    final DebuggerTreeCreatorImpl creator = new DebuggerTreeCreatorImpl(getProject());
    DebuggerInvocationUtil.invokeLater(getProject(), new Runnable() {
      @Override
      public void run() {
        showTreePopup(creator, Pair.create(descriptor, expressionText));
      }
    });
  }

  private static boolean isActiveTooltipApplicable(final Value value) {
    return value != null && !(value instanceof PrimitiveValue);
  }

  private void showHint(final SimpleColoredText text, final WatchItemDescriptor descriptor) {
    DebuggerInvocationUtil.invokeLater(getProject(), new Runnable() {
      @Override
      public void run() {
        if(!isHintHidden()) {
          JComponent component;
          if (!isActiveTooltipApplicable(descriptor.getValue())) {
            component = HintUtil.createInformationLabel(text);
          }
          else {
            component = createExpandableHintComponent(text, new Runnable() {
              @Override
              public void run() {
                final DebuggerContextImpl debuggerContext = DebuggerManagerEx.getInstanceEx(getProject()).getContext();
                final DebugProcessImpl debugProcess = debuggerContext.getDebugProcess();
                debugProcess.getManagerThread().schedule(new DebuggerContextCommandImpl(debuggerContext) {
                              @Override
                              public void threadAction() {
                                descriptor.setRenderer(debugProcess.getAutoRenderer(descriptor));
                                final String expressionText = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
                                  @Override
                                  public String compute() {
                                    return myCurrentExpression.getText();
                                  }
                                });

                                createAndShowTree(expressionText, descriptor);
                              }
                            });
              }
            });
          }
          if (!showHint(component)) return;
          if(getType() == ValueHintType.MOUSE_CLICK_HINT) {
            HintUtil.createInformationLabel(text).requestFocusInWindow();
          }
        }
      }
    });
  }

  public static InspectDebuggerTree createInspectTree(final NodeDescriptorImpl descriptor, Project project) {
    final InspectDebuggerTree tree = new InspectDebuggerTree(project);
    final AnAction setValueAction = ActionManager.getInstance().getAction(DebuggerActions.SET_VALUE);
    setValueAction.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0)), tree);
    Disposer.register(tree, new Disposable() {
      @Override
      public void dispose() {
        setValueAction.unregisterCustomShortcutSet(tree);
      }
    });
    tree.setInspectDescriptor(descriptor);
    DebuggerContextImpl context = DebuggerManagerEx.getInstanceEx(project).getContext();
    tree.rebuild(context);
    return tree;
  }

  @Nullable
  private static Pair<PsiElement, TextRange> findExpression(PsiElement element, boolean allowMethodCalls) {
    final EditorTextProvider textProvider = EditorTextProvider.EP.forLanguage(element.getLanguage());
    if (textProvider != null) {
      return textProvider.findExpression(element, allowMethodCalls);
    }
    return null;
  }

  private static Trinity<PsiElement, TextRange, Value> getSelectedExpression(final Project project, final Editor editor, final Point point, final ValueHintType type) {
    final Ref<PsiElement> selectedExpression = Ref.create(null);
    final Ref<TextRange> currentRange = Ref.create(null);
    final Ref<Value> preCalculatedValue = Ref.create(null);

    PsiDocumentManager.getInstance(project).commitAndRunReadAction(new Runnable() {
      @Override
      public void run() {
        // Point -> offset
        final int offset = calculateOffset(editor, point);


        PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());

        if(psiFile == null || !psiFile.isValid()) {
          return;
        }

        int selectionStart = editor.getSelectionModel().getSelectionStart();
        int selectionEnd   = editor.getSelectionModel().getSelectionEnd();

        if((type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT) && (selectionStart <= offset && offset <= selectionEnd)) {
          PsiElement ctx = (selectionStart > 0) ? psiFile.findElementAt(selectionStart - 1) : psiFile.findElementAt(selectionStart);
          try {
            String text = editor.getSelectionModel().getSelectedText();
            if(text != null && ctx != null) {
              final JVMElementFactory factory = JVMElementFactories.getFactory(ctx.getLanguage(), project);
              if (factory == null) {
                return;
              }
              selectedExpression.set(factory.createExpressionFromText(text, ctx));
              currentRange.set(new TextRange(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd()));
            }
          }
          catch (IncorrectOperationException ignored) {
          }
        }

        if(currentRange.get() == null) {
          PsiElement elementAtCursor = psiFile.findElementAt(offset);
          if (elementAtCursor == null) {
            return;
          }
          Pair<PsiElement, TextRange> pair = findExpression(elementAtCursor, type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT);
          if (pair == null) {
            if (type == ValueHintType.MOUSE_OVER_HINT) {
              final DebuggerSession debuggerSession = DebuggerManagerEx.getInstanceEx(project).getContext().getDebuggerSession();
              if(debuggerSession != null && debuggerSession.isPaused()) {
                final Pair<Method, Value> lastExecuted = debuggerSession.getProcess().getLastExecutedMethod();
                if (lastExecuted != null) {
                  final Method method = lastExecuted.getFirst();
                  if (method != null) {
                    final Pair<PsiElement, TextRange> expressionPair = findExpression(elementAtCursor, true);
                    if (expressionPair != null && expressionPair.getFirst() instanceof PsiMethodCallExpression) {
                      final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)expressionPair.getFirst();
                      final PsiMethod psiMethod = methodCallExpression.resolveMethod();
                      if (psiMethod != null) {
                        final JVMName jvmSignature = JVMNameUtil.getJVMSignature(psiMethod);
                        try {
                          if (method.name().equals(psiMethod.getName()) && method.signature().equals(jvmSignature.getName(debuggerSession.getProcess()))) {
                            pair = expressionPair;
                            preCalculatedValue.set(lastExecuted.getSecond());
                          }
                        }
                        catch (EvaluateException ignored) {
                        }
                      }
                    }
                  }
                }
              }
            }
          }
          if (pair == null) {
            return;
          }
          selectedExpression.set(pair.getFirst());
          currentRange.set(pair.getSecond());
        }
      }
    });
    return Trinity.create(selectedExpression.get(), currentRange.get(), preCalculatedValue.get());
  }
}