IDEA-131100 RubyMine EAP does not detect evaluate expression code fragment mode any more - simplification after review

This commit is contained in:
Egor.Ushakov
2014-10-17 17:31:29 +04:00
parent a9033496c9
commit 10f213ef8c
3 changed files with 26 additions and 54 deletions
@@ -25,12 +25,10 @@ import com.intellij.debugger.impl.EditorTextProvider;
import com.intellij.debugger.ui.impl.watch.NodeManagerImpl;
import com.intellij.debugger.ui.impl.watch.WatchItemDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
@@ -39,7 +37,6 @@ import com.intellij.xdebugger.XSourcePosition;
import com.intellij.xdebugger.evaluation.EvaluationMode;
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator;
import com.intellij.xdebugger.impl.breakpoints.XExpressionImpl;
import com.intellij.xdebugger.impl.ui.XDebuggerEditorBase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -133,14 +130,11 @@ public class JavaDebuggerEvaluator extends XDebuggerEvaluator {
}
@Override
public XExpression getEditorExpression(@NotNull Editor editor, @Nullable PsiFile psiFile) {
String text = editor.getSelectionModel().getSelectedText();
if (psiFile == null || StringUtil.isEmpty(text)) {
return super.getEditorExpression(editor, psiFile);
public EvaluationMode getEvaluationMode(@NotNull String text, int startOffset, int endOffset, @Nullable PsiFile psiFile) {
if (psiFile != null) {
PsiElement[] range = CodeInsightUtil.findStatementsInRange(psiFile, startOffset, endOffset);
return range.length > 1 ? EvaluationMode.CODE_FRAGMENT : EvaluationMode.EXPRESSION;
}
PsiElement[] range = CodeInsightUtil.findStatementsInRange(psiFile, editor.getSelectionModel().getSelectionStart(),
editor.getSelectionModel().getSelectionEnd());
return new XExpressionImpl(formatTextForEvaluation(text), XDebuggerEditorBase.getFileTypeLanguage(psiFile.getFileType()), null,
range.length > 1 ? EvaluationMode.CODE_FRAGMENT : EvaluationMode.EXPRESSION);
return super.getEvaluationMode(text, startOffset, endOffset, null);
}
}
@@ -174,27 +174,10 @@ public abstract class XDebuggerEvaluator {
}
/**
* Returns expression that is selected or under the cursor in the Editor
* Returns mode which should be used to evaluate the text
*/
public XExpression getEditorExpression(@NotNull Editor editor, @Nullable PsiFile psiFile) {
String text = editor.getSelectionModel().getSelectedText();
if (text != null) {
text = formatTextForEvaluation(text);
}
else if (editor.getProject() != null) {
Document document = editor.getDocument();
ExpressionInfo info = getExpressionInfoAtOffset(editor.getProject(), document, editor.getCaretModel().getOffset(), true);
if (info != null) {
text = info.getExpressionText();
if (text == null) {
text = document.getText(info.getTextRange());
}
}
}
if (!StringUtil.isEmpty(text)) {
return XDebuggerUtil.getInstance().createExpression(text, null, null, text.contains("\n") ? EvaluationMode.CODE_FRAGMENT : EvaluationMode.EXPRESSION);
}
return null;
public EvaluationMode getEvaluationMode(@NotNull String text, int startOffset, int endOffset, @Nullable PsiFile psiFile) {
return text.contains("\n") ? EvaluationMode.CODE_FRAGMENT : EvaluationMode.EXPRESSION;
}
@Deprecated
@@ -21,12 +21,12 @@ import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.xdebugger.XDebugSession;
import com.intellij.xdebugger.XExpression;
import com.intellij.xdebugger.XSourcePosition;
import com.intellij.xdebugger.evaluation.EvaluationMode;
import com.intellij.xdebugger.evaluation.ExpressionInfo;
import com.intellij.xdebugger.evaluation.XDebuggerEditorsProvider;
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator;
@@ -54,34 +54,28 @@ public class XDebuggerEvaluateActionHandler extends XDebuggerActionHandler {
Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
XExpression expression = null;
if (editor != null) {
expression = evaluator.getEditorExpression(editor, CommonDataKeys.PSI_FILE.getData(dataContext));
EvaluationMode mode = EvaluationMode.EXPRESSION;
String selectedText = editor != null ? editor.getSelectionModel().getSelectedText() : null;
if (selectedText != null) {
selectedText = evaluator.formatTextForEvaluation(selectedText);
mode = evaluator.getEvaluationMode(selectedText,
editor.getSelectionModel().getSelectionStart(),
editor.getSelectionModel().getSelectionEnd(),
CommonDataKeys.PSI_FILE.getData(dataContext));
}
String text = selectedText;
if (text == null && editor != null) {
text = getExpressionText(evaluator, CommonDataKeys.PROJECT.getData(dataContext), editor);
}
Language language = calcLanguage(stackFrame, dataContext);
if (expression != null && !Comparing.equal(language, expression.getLanguage())) { // may need to change language
expression = new XExpressionImpl(expression.getExpression(), language, expression.getCustomInfo(), expression.getMode());
}
if (expression == null) {
if (text == null) {
XValue value = XDebuggerTreeActionBase.getSelectedValue(dataContext);
if (value != null) {
String text = value.getEvaluationExpression();
if (!StringUtil.isEmpty(text)) {
expression = new XExpressionImpl(text, language, null);
}
text = value.getEvaluationExpression();
}
}
if (expression == null) {
expression = new XExpressionImpl("", language, null);
}
new XDebuggerEvaluationDialog(session, editorsProvider, evaluator, expression, stackFrame == null ? null : stackFrame.getSourcePosition()).show();
}
private static Language calcLanguage(XStackFrame stackFrame, DataContext dataContext) {
Language language = null;
if (stackFrame != null) {
XSourcePosition position = stackFrame.getSourcePosition();
@@ -95,7 +89,8 @@ public class XDebuggerEvaluateActionHandler extends XDebuggerActionHandler {
language = XDebuggerEditorBase.getFileTypeLanguage(file.getFileType());
}
}
return language;
XExpression expression = new XExpressionImpl(StringUtil.notNullize(text), language, null, mode);
new XDebuggerEvaluationDialog(session, editorsProvider, evaluator, expression, stackFrame == null ? null : stackFrame.getSourcePosition()).show();
}
@Nullable