"View Text" action for debugger (IDEA-22850 String value viewer)

This commit is contained in:
Eugene Zhuravlev
2012-05-19 16:35:11 +02:00
parent 2c31763520
commit eca6b4cf49
6 changed files with 167 additions and 47 deletions
@@ -27,7 +27,7 @@ import com.intellij.debugger.impl.DebuggerContextImpl;
import com.intellij.debugger.impl.DebuggerUtilsEx;
import com.intellij.debugger.ui.impl.watch.DebuggerTreeNodeImpl;
import com.intellij.debugger.ui.impl.watch.NodeDescriptorImpl;
import com.intellij.debugger.ui.impl.watch.ValueDescriptorImpl;
import com.intellij.debugger.ui.tree.ValueDescriptor;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
@@ -41,69 +41,73 @@ import org.jetbrains.annotations.Nullable;
* @author Jeka
*/
public abstract class BaseValueAction extends DebuggerAction {
public void actionPerformed(AnActionEvent e) {
final DataContext actionContext = e.getDataContext();
final Project project = PlatformDataKeys.PROJECT.getData(actionContext);
final Value value = getValue(actionContext);
final DebuggerTreeNodeImpl node = getSelectedNode(actionContext);
final Value value = getValue(node);
if (value == null) {
return;
}
DebuggerManagerEx debuggerManager = DebuggerManagerEx.getInstanceEx(project);
if(debuggerManager != null) {
final DebuggerContextImpl debuggerContext = debuggerManager.getContext();
if(debuggerContext != null && debuggerContext.getDebuggerSession() != null) {
final ProgressWindowWithNotification progressWindow = new ProgressWindowWithNotification(true, project);
SuspendContextCommandImpl copyValueAction = new SuspendContextCommandImpl(debuggerContext.getSuspendContext()) {
public Priority getPriority() {
return Priority.HIGH;
}
public void contextAction() throws Exception {
//noinspection HardCodedStringLiteral
progressWindow.setText(DebuggerBundle.message("progress.evaluating", "toString()"));
final String valueAsString = DebuggerUtilsEx.getValueOrErrorAsString(debuggerContext.createEvaluationContext(), value);
if (progressWindow.isCanceled()) return;
DebuggerInvocationUtil.swingInvokeLater(project, new Runnable() {
public void run() {
String text = valueAsString;
if (text == null) text = "";
processText(project, text);
}
});
}
};
progressWindow.setTitle(DebuggerBundle.message("title.evaluating"));
debuggerContext.getDebugProcess().getManagerThread().startProgress(copyValueAction, progressWindow);
}
final Project project = PlatformDataKeys.PROJECT.getData(actionContext);
final DebuggerManagerEx debuggerManager = DebuggerManagerEx.getInstanceEx(project);
if(debuggerManager == null) {
return;
}
final DebuggerContextImpl debuggerContext = debuggerManager.getContext();
if (debuggerContext == null || debuggerContext.getDebuggerSession() == null) {
return;
}
final ProgressWindowWithNotification progressWindow = new ProgressWindowWithNotification(true, project);
SuspendContextCommandImpl getTextCommand = new SuspendContextCommandImpl(debuggerContext.getSuspendContext()) {
public Priority getPriority() {
return Priority.HIGH;
}
public void contextAction() throws Exception {
//noinspection HardCodedStringLiteral
progressWindow.setText(DebuggerBundle.message("progress.evaluating", "toString()"));
final String valueAsString = DebuggerUtilsEx.getValueOrErrorAsString(debuggerContext.createEvaluationContext(), value);
if (progressWindow.isCanceled()) {
return;
}
DebuggerInvocationUtil.swingInvokeLater(project, new Runnable() {
public void run() {
String text = valueAsString;
if (text == null) {
text = "";
}
processText(project, text, node, debuggerContext);
}
});
}
};
progressWindow.setTitle(DebuggerBundle.message("title.evaluating"));
debuggerContext.getDebugProcess().getManagerThread().startProgress(getTextCommand, progressWindow);
}
protected abstract void processText(final Project project, String text);
protected abstract void processText(final Project project, String text, DebuggerTreeNodeImpl node, DebuggerContextImpl debuggerContext);
public void update(AnActionEvent e) {
Presentation presentation = e.getPresentation();
Value value = getValue(e.getDataContext());
Value value = getValue(getSelectedNode(e.getDataContext()));
presentation.setEnabled(value != null);
presentation.setVisible(value != null);
}
@Nullable
public static Value getValue(DataContext context) {
DebuggerTreeNodeImpl selectedNode = getSelectedNode(context);
if (selectedNode == null) {
private static Value getValue(final DebuggerTreeNodeImpl node) {
if (node == null) {
return null;
}
NodeDescriptorImpl descriptor = selectedNode.getDescriptor();
if (!(descriptor instanceof ValueDescriptorImpl)) {
NodeDescriptorImpl descriptor = node.getDescriptor();
if (!(descriptor instanceof ValueDescriptor)) {
return null;
}
return ((ValueDescriptorImpl)descriptor).getValue();
return ((ValueDescriptor)descriptor).getValue();
}
}
@@ -16,6 +16,8 @@
package com.intellij.debugger.actions;
import com.intellij.debugger.DebuggerBundle;
import com.intellij.debugger.impl.DebuggerContextImpl;
import com.intellij.debugger.ui.impl.watch.DebuggerTreeNodeImpl;
import com.intellij.openapi.diff.*;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.project.Project;
@@ -30,7 +32,7 @@ import java.awt.datatransfer.Transferable;
* @author Jeka
*/
public class CompareValueWithClipboardAction extends BaseValueAction {
protected void processText(final Project project, final String text) {
protected void processText(final Project project, final String text, DebuggerTreeNodeImpl node, DebuggerContextImpl debuggerContext) {
DiffManager.getInstance().getDiffTool().show(new ClipboardSelectionContents(text, project));
}
@@ -15,6 +15,8 @@
*/
package com.intellij.debugger.actions;
import com.intellij.debugger.impl.DebuggerContextImpl;
import com.intellij.debugger.ui.impl.watch.DebuggerTreeNodeImpl;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.project.Project;
@@ -24,7 +26,7 @@ import java.awt.datatransfer.StringSelection;
* @author Jeka
*/
public class CopyValueAction extends BaseValueAction {
protected void processText(final Project project, final String text) {
protected void processText(final Project project, final String text, DebuggerTreeNodeImpl node, DebuggerContextImpl debuggerContext) {
CopyPasteManager.getInstance().setContents(new StringSelection(text));
}
}
@@ -0,0 +1,105 @@
/*
* 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.debugger.actions;
import com.intellij.debugger.engine.evaluation.EvaluateException;
import com.intellij.debugger.engine.evaluation.TextWithImports;
import com.intellij.debugger.impl.DebuggerContextImpl;
import com.intellij.debugger.ui.impl.watch.DebuggerTreeNodeExpression;
import com.intellij.debugger.ui.impl.watch.DebuggerTreeNodeImpl;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.EditorTextField;
import javax.swing.*;
import java.awt.*;
/*
* @author Jeka
*/
public class ViewTextAction extends BaseValueAction {
protected void processText(final Project project, final String text, DebuggerTreeNodeImpl node, DebuggerContextImpl debuggerContext) {
try {
final TextWithImports expressionText = DebuggerTreeNodeExpression.createEvaluationText(node, debuggerContext);
final MyDialog dialog = new MyDialog(project);
final String title = "View Text: \"" + expressionText.getText() + "\"";
dialog.setTitle(title);
dialog.setText(text);
dialog.show();
}
catch (EvaluateException e) {
Messages.showErrorDialog(project, e.getMessage(), "View Text");
}
}
private static class MyDialog extends DialogWrapper {
private EditorTextField myTextViewer;
private MyDialog(Project project) {
super(project, false);
setModal(false);
setCancelButtonText("Close");
setCrossClosesWindow(true);
myTextViewer = new TextViewer(project);
init();
}
public void setText(String text) {
myTextViewer.setText(text);
}
protected Action[] createActions() {
return new Action[] {getCancelAction()};
}
protected String getDimensionServiceKey() {
return "#com.intellij.debugger.actions.ViewTextAction";
}
protected JComponent createCenterPanel() {
final JPanel panel = new JPanel(new BorderLayout());
panel.add(myTextViewer, BorderLayout.CENTER);
panel.setPreferredSize(new Dimension(300, 200));
return panel;
}
}
private static class TextViewer extends EditorTextField {
private TextViewer(Project project) {
super(EditorFactory.getInstance().createDocument(""), project, FileTypes.PLAIN_TEXT, true, false);
}
protected EditorEx createEditor() {
final EditorEx editor = super.createEditor();
editor.setHorizontalScrollbarVisible(true);
editor.setVerticalScrollbarVisible(true);
editor.setEmbeddedIntoDialogWrapper(true);
editor.getComponent().setPreferredSize(null);
return editor;
}
}
}
@@ -868,6 +868,8 @@ group.XDebugger.settings.text=Settings
action.Debugger.AdjustArrayRange.text=Adjust Range...
action.Debugger.Inspect.text=Inspect
action.Debugger.CopyValue.text=Copy Value
action.Debugger.ViewText.text=View Text
action.Debugger.ViewText.description=View text value of selected node in a separate pane
action.Debugger.CompareValueWithClipboard.text=Compare Value with Clipboard
action.Debugger.CustomizeContextView.text=Customize Data Views...
action.Debugger.CustomizeThreadsView.text=Customize Threads View...
+5
View File
@@ -128,6 +128,7 @@
<group id="JavaDebuggerActions">
<action id="Debugger.AdjustArrayRange" class="com.intellij.debugger.actions.AdjustArrayRangeAction"/>
<action id="Debugger.Inspect" class="com.intellij.debugger.actions.InspectAction"/>
<action id="Debugger.ViewText" class="com.intellij.debugger.actions.ViewTextAction"/>
<action id="Debugger.CopyValue" class="com.intellij.debugger.actions.CopyValueAction"/>
<action id="Debugger.CompareValueWithClipboard" class="com.intellij.debugger.actions.CompareValueWithClipboardAction"/>
<action id="Debugger.CustomizeContextView" class="com.intellij.debugger.actions.CustomizeContextViewAction"/>
@@ -210,6 +211,7 @@
<reference ref="ToggleFieldBreakpoint"/>
<separator/>
<reference ref="Debugger.CopyValue"/>
<reference ref="Debugger.ViewText"/>
<reference ref="Debugger.CompareValueWithClipboard"/>
<reference ref="Debugger.CustomizeContextView"/>
</group>
@@ -230,6 +232,7 @@
<reference ref="ToggleFieldBreakpoint"/>
<separator/>
<reference ref="Debugger.CopyValue"/>
<reference ref="Debugger.ViewText"/>
<reference ref="Debugger.CompareValueWithClipboard"/>
<reference ref="Debugger.CustomizeContextView"/>
</group>
@@ -250,6 +253,7 @@
<reference ref="ToggleFieldBreakpoint"/>
<separator/>
<reference ref="Debugger.CopyValue"/>
<reference ref="Debugger.ViewText"/>
<reference ref="Debugger.CompareValueWithClipboard"/>
<reference ref="Debugger.CustomizeContextView"/>
</group>
@@ -290,6 +294,7 @@
<reference ref="ToggleFieldBreakpoint"/>
<separator/>
<reference ref="Debugger.CopyValue"/>
<reference ref="Debugger.ViewText"/>
<reference ref="Debugger.CompareValueWithClipboard"/>
<reference ref="Debugger.CustomizeContextView"/>
</group>