2 * Copyright 2000-2015 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.
16 package com.intellij.xdebugger.impl.actions.handlers;
18 import com.intellij.lang.Language;
19 import com.intellij.openapi.actionSystem.CommonDataKeys;
20 import com.intellij.openapi.actionSystem.DataContext;
21 import com.intellij.openapi.editor.Document;
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.util.text.StringUtil;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.ui.AppUIUtil;
27 import com.intellij.util.Consumer;
28 import com.intellij.xdebugger.XDebugSession;
29 import com.intellij.xdebugger.XExpression;
30 import com.intellij.xdebugger.XSourcePosition;
31 import com.intellij.xdebugger.evaluation.EvaluationMode;
32 import com.intellij.xdebugger.evaluation.ExpressionInfo;
33 import com.intellij.xdebugger.evaluation.XDebuggerEditorsProvider;
34 import com.intellij.xdebugger.evaluation.XDebuggerEvaluator;
35 import com.intellij.xdebugger.frame.XStackFrame;
36 import com.intellij.xdebugger.frame.XValue;
37 import com.intellij.xdebugger.impl.breakpoints.XExpressionImpl;
38 import com.intellij.xdebugger.impl.evaluate.XDebuggerEvaluationDialog;
39 import com.intellij.xdebugger.impl.ui.XDebuggerEditorBase;
40 import com.intellij.xdebugger.impl.ui.tree.actions.XDebuggerTreeActionBase;
41 import org.jetbrains.annotations.NotNull;
42 import org.jetbrains.annotations.Nullable;
47 public class XDebuggerEvaluateActionHandler extends XDebuggerActionHandler {
49 protected void perform(@NotNull final XDebugSession session, final DataContext dataContext) {
50 final XDebuggerEditorsProvider editorsProvider = session.getDebugProcess().getEditorsProvider();
51 final XStackFrame stackFrame = session.getCurrentStackFrame();
52 final XDebuggerEvaluator evaluator = session.getDebugProcess().getEvaluator();
53 if (evaluator == null) {
57 Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
59 EvaluationMode mode = EvaluationMode.EXPRESSION;
60 String selectedText = editor != null ? editor.getSelectionModel().getSelectedText() : null;
61 if (selectedText != null) {
62 selectedText = evaluator.formatTextForEvaluation(selectedText);
63 mode = evaluator.getEvaluationMode(selectedText,
64 editor.getSelectionModel().getSelectionStart(),
65 editor.getSelectionModel().getSelectionEnd(),
66 CommonDataKeys.PSI_FILE.getData(dataContext));
68 String text = selectedText;
70 if (text == null && editor != null) {
71 text = getExpressionText(evaluator, CommonDataKeys.PROJECT.getData(dataContext), editor);
74 final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
77 XValue value = XDebuggerTreeActionBase.getSelectedValue(dataContext);
79 value.calculateEvaluationExpression().done(new Consumer<XExpression>() {
81 public void consume(final XExpression expression) {
82 AppUIUtil.invokeOnEdt(new Runnable() {
85 showDialog(session, file, editorsProvider, stackFrame, evaluator, expression);
94 XExpression expression = XExpressionImpl.fromText(StringUtil.notNullize(text), mode);
95 showDialog(session, file, editorsProvider, stackFrame, evaluator, expression);
98 private static void showDialog(@NotNull XDebugSession session,
100 XDebuggerEditorsProvider editorsProvider,
101 XStackFrame stackFrame,
102 XDebuggerEvaluator evaluator,
103 XExpression expression) {
104 if (expression.getLanguage() == null) {
105 Language language = null;
106 if (stackFrame != null) {
107 XSourcePosition position = stackFrame.getSourcePosition();
108 if (position != null) {
109 language = XDebuggerEditorBase.getFileTypeLanguage(position.getFile().getFileType());
112 if (language == null && file != null) {
113 language = XDebuggerEditorBase.getFileTypeLanguage(file.getFileType());
115 expression = new XExpressionImpl(expression.getExpression(), language, expression.getCustomInfo(), expression.getMode());
117 new XDebuggerEvaluationDialog(session, editorsProvider, evaluator, expression, stackFrame == null ? null : stackFrame.getSourcePosition()).show();
121 public static String getExpressionText(@Nullable XDebuggerEvaluator evaluator, @Nullable Project project, @NotNull Editor editor) {
122 if (project == null || evaluator == null) {
126 Document document = editor.getDocument();
127 return getExpressionText(evaluator.getExpressionInfoAtOffset(project, document, editor.getCaretModel().getOffset(), true), document);
131 public static String getExpressionText(@Nullable ExpressionInfo expressionInfo, @NotNull Document document) {
132 if (expressionInfo == null) {
135 String text = expressionInfo.getExpressionText();
136 return text == null ? document.getText(expressionInfo.getTextRange()) : text;
140 public static String getDisplayText(@Nullable ExpressionInfo expressionInfo, @NotNull Document document) {
141 if (expressionInfo == null) {
144 String text = expressionInfo.getDisplayText();
145 return text == null ? document.getText(expressionInfo.getTextRange()) : text;
149 protected boolean isEnabled(final @NotNull XDebugSession session, final DataContext dataContext) {
150 return session.getDebugProcess().getEvaluator() != null;