~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/DebugValueTooltipProvider.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// DebugValueTooltipProvider.cs
2
2
//
3
 
// Author:
4
 
//   Lluis Sanchez Gual <lluis@novell.com>
 
3
// Authors: Lluis Sanchez Gual <lluis@novell.com>
 
4
//          Jeffrey Stedfast <jeff@xamarin.com>
5
5
//
6
6
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
 
7
// Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
7
8
//
8
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
10
// of this software and associated documentation files (the "Software"), to deal
27
28
 
28
29
using System;
29
30
using System.Collections.Generic;
 
31
 
30
32
using Mono.TextEditor;
31
 
using MonoDevelop.Ide.Gui;
 
33
using MonoDevelop.Components;
32
34
using Mono.Debugging.Client;
33
35
using TextEditor = Mono.TextEditor.TextEditor;
34
 
using MonoDevelop.Ide.CodeCompletion;
35
36
using MonoDevelop.Debugger;
36
37
 
37
38
using ICSharpCode.NRefactory.TypeSystem;
39
40
 
40
41
namespace MonoDevelop.SourceEditor
41
42
{
42
 
        public class DebugValueTooltipProvider: ITooltipProvider, IDisposable
 
43
        public class DebugValueTooltipProvider: TooltipProvider, IDisposable
43
44
        {
44
45
                Dictionary<string,ObjectValue> cachedValues = new Dictionary<string,ObjectValue> ();
 
46
                DebugValueWindow tooltip;
45
47
                
46
 
                public DebugValueTooltipProvider()
47
 
                {
48
 
                        DebuggingService.CurrentFrameChanged += HandleCurrentFrameChanged;
49
 
                }
50
 
 
51
 
                void HandleCurrentFrameChanged (object sender, EventArgs e)
 
48
                public DebugValueTooltipProvider ()
 
49
                {
 
50
                        DebuggingService.CurrentFrameChanged += CurrentFrameChanged;
 
51
                        DebuggingService.DebugSessionStarted += DebugSessionStarted;
 
52
                }
 
53
 
 
54
                void DebugSessionStarted (object sender, EventArgs e)
 
55
                {
 
56
                        DebuggingService.DebuggerSession.TargetExited += TargetProcessExited;
 
57
                }
 
58
 
 
59
                void CurrentFrameChanged (object sender, EventArgs e)
52
60
                {
53
61
                        // Clear the cached values every time the current frame changes
54
62
                        cachedValues.Clear ();
55
 
                }
56
 
 
57
 
                #region ITooltipProvider implementation 
 
63
 
 
64
                        if (tooltip != null)
 
65
                                tooltip.Hide ();
 
66
                }
 
67
 
 
68
                void TargetProcessExited (object sender, EventArgs e)
 
69
                {
 
70
                        if (tooltip != null)
 
71
                                tooltip.Hide ();
 
72
                }
 
73
 
 
74
                #region ITooltipProvider implementation
 
75
 
 
76
                static int IndexOfLastWhiteSpace (string text)
 
77
                {
 
78
                        int index = text.Length - 1;
 
79
 
 
80
                        while (index >= 0) {
 
81
                                if (char.IsWhiteSpace (text[index]))
 
82
                                        break;
 
83
                                index--;
 
84
                        }
 
85
 
 
86
                        return index;
 
87
                }
58
88
                
59
 
                public TooltipItem GetItem (Mono.TextEditor.TextEditor editor, int offset)
 
89
                public override TooltipItem GetItem (TextEditor editor, int offset)
60
90
                {
61
91
                        if (offset >= editor.Document.TextLength)
62
92
                                return null;
77
107
                                startOffset = ed.SelectionRange.Offset;
78
108
                                length = ed.SelectionRange.Length;
79
109
                        } else {
80
 
                                ICSharpCode.NRefactory.TypeSystem.DomRegion expressionRegion;
 
110
                                DomRegion expressionRegion;
81
111
                                ResolveResult res = ed.GetLanguageItem (offset, out expressionRegion);
82
112
                                
83
113
                                if (res == null || res.IsError || res.GetType () == typeof (ResolveResult))
87
117
                                
88
118
                                if (expressionRegion.IsEmpty)
89
119
                                        return null;
90
 
                                
 
120
 
91
121
                                if (res is NamespaceResolveResult ||
92
122
                                    res is ConversionResolveResult ||
93
123
                                    res is ForEachResolveResult ||
94
124
                                    res is TypeIsResolveResult ||
95
125
                                    res is TypeOfResolveResult ||
96
 
                                    res is TypeResolveResult ||
97
126
                                    res is ErrorResolveResult)
98
127
                                        return null;
99
128
                                
107
136
                                if (res is LocalResolveResult) {
108
137
                                        var lr = (LocalResolveResult) res;
109
138
                                        
110
 
                                        // Capture only the local variable portion of the expression...
111
 
                                        expression = lr.Variable.Name;
 
139
                                        // Use the start and end offsets of the variable region so that we get the "@" in variable names like "@class"
 
140
                                        start = new DocumentLocation (lr.Variable.Region.BeginLine, lr.Variable.Region.BeginColumn);
 
141
                                        end = new DocumentLocation (lr.Variable.Region.EndLine, lr.Variable.Region.EndColumn);
 
142
                                        startOffset = editor.Document.LocationToOffset (start);
 
143
                                        endOffset = editor.Document.LocationToOffset (end);
 
144
 
 
145
                                        expression = ed.GetTextBetween (startOffset, endOffset).Trim ();
 
146
 
 
147
                                        // Note: When the LocalResolveResult is a parameter, the Variable.Region includes the type
 
148
                                        if (lr.IsParameter) {
 
149
                                                int index = IndexOfLastWhiteSpace (expression);
 
150
                                                if (index != -1)
 
151
                                                        expression = expression.Substring (index + 1);
 
152
                                        }
 
153
 
112
154
                                        length = expression.Length;
113
 
                                        
114
 
                                        // Calculate start offset based on the end offset because we don't want to include the type information.
115
 
                                        startOffset = endOffset - length;
116
155
                                } else if (res is InvocationResolveResult) {
117
156
                                        var ir = (InvocationResolveResult) res;
118
157
                                        
141
180
                                                } else if (mr.Member is IField) {
142
181
                                                        var field = (IField) mr.Member;
143
182
                                                        
144
 
                                                        expression = field.Name;
 
183
                                                        if (field.IsStatic)
 
184
                                                                expression = field.FullName;
 
185
                                                        else
 
186
                                                                expression = field.Name;
145
187
                                                } else {
146
188
                                                        return null;
147
189
                                                }
152
194
                                        // Fall through...
153
195
                                } else if (res is ThisResolveResult) {
154
196
                                        // Fall through...
 
197
                                } else if (res is TypeResolveResult) {
 
198
                                        // Fall through...
155
199
                                } else {
156
200
                                        return null;
157
201
                                }
165
209
                        
166
210
                        ObjectValue val;
167
211
                        if (!cachedValues.TryGetValue (expression, out val)) {
168
 
                                val = frame.GetExpressionValue (expression, false);
 
212
                                val = frame.GetExpressionValue (expression, true);
169
213
                                cachedValues [expression] = val;
170
214
                        }
171
215
                        
196
240
                        return char.IsLetterOrDigit (c) || c == '_';
197
241
                }
198
242
                        
199
 
                public Gtk.Window CreateTooltipWindow (Mono.TextEditor.TextEditor editor, int offset, Gdk.ModifierType modifierState, TooltipItem item)
200
 
                {
201
 
                        return new DebugValueWindow (editor, offset, DebuggingService.CurrentFrame, (ObjectValue) item.Item, null);
202
 
                }
203
 
                
204
 
                public void GetRequiredPosition (Mono.TextEditor.TextEditor editor, Gtk.Window tipWindow, out int requiredWidth, out double xalign)
205
 
                {
206
 
                        xalign = 0.1;
207
 
                        requiredWidth = tipWindow.SizeRequest ().Width;
208
 
                }
209
 
 
210
 
                public bool IsInteractive (Mono.TextEditor.TextEditor editor, Gtk.Window tipWindow)
211
 
                {
212
 
                        return true;
 
243
                public override Gtk.Window ShowTooltipWindow (TextEditor editor, int offset, Gdk.ModifierType modifierState, int mouseX, int mouseY, TooltipItem item)
 
244
                {
 
245
                        var location = editor.OffsetToLocation (item.ItemSegment.Offset);
 
246
                        var point = editor.LocationToPoint (location);
 
247
                        int lineHeight = (int) editor.LineHeight;
 
248
                        int y = point.Y;
 
249
 
 
250
                        // find the top of the line that the mouse is hovering over
 
251
                        while (y + lineHeight < mouseY)
 
252
                                y += lineHeight;
 
253
 
 
254
                        var caret = new Gdk.Rectangle (mouseX, y, 1, lineHeight);
 
255
                        tooltip = new DebugValueWindow (editor, offset, DebuggingService.CurrentFrame, (ObjectValue) item.Item, null);
 
256
                        tooltip.ShowPopup (editor, caret, PopupPosition.TopLeft);
 
257
 
 
258
                        return tooltip;
 
259
                }
 
260
 
 
261
                public override bool IsInteractive (TextEditor editor, Gtk.Window tipWindow)
 
262
                {
 
263
                        return DebuggingService.IsDebugging;
213
264
                }
214
265
                
215
266
                #endregion 
217
268
                #region IDisposable implementation
218
269
                public void Dispose ()
219
270
                {
220
 
                        DebuggingService.CurrentFrameChanged -= HandleCurrentFrameChanged;
 
271
                        DebuggingService.CurrentFrameChanged -= CurrentFrameChanged;
 
272
                        DebuggingService.DebugSessionStarted -= DebugSessionStarted;
221
273
                }
222
274
                #endregion
223
275
        }