Skip to content
Newer
Older
100644 198 lines (168 sloc) 3.89 KB
1933b5e @ermau Use a smart tag instead of a custom button
authored Dec 16, 2012
1 //
2 // InstantTagToggle.cs
3 //
4 // Copyright 2012 Eric Maupin
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17
18 using System;
19 using System.Collections.ObjectModel;
20 using System.Windows;
21 using System.Windows.Media;
22 using Microsoft.VisualStudio.Language.Intellisense;
23 using Microsoft.VisualStudio.Text;
24 using Microsoft.VisualStudio.Text.Editor;
25
26 namespace Instant.VisualStudio
27 {
28 public class InstantToggleEventArgs
29 : EventArgs
30 {
31 public InstantToggleEventArgs (ITextView view, ITrackingSpan methodSpan)
32 {
33 if (view == null)
34 throw new ArgumentNullException ("view");
35 if (methodSpan == null)
36 throw new ArgumentNullException ("methodSpan");
37
38 View = view;
39 MethodSpan = methodSpan;
40 IsRunning = false;
41 }
42
43 public InstantToggleEventArgs (ITextView view, ITrackingSpan methodSpan, string testCode)
44 {
45 if (view == null)
46 throw new ArgumentNullException ("view");
47 if (methodSpan == null)
48 throw new ArgumentNullException ("methodSpan");
49 if (testCode == null)
50 throw new ArgumentNullException ("testCode");
51
52 View = view;
53 MethodSpan = methodSpan;
54 TestCode = testCode;
55 IsRunning = true;
56 }
57
58 public bool IsRunning
59 {
60 get;
61 private set;
62 }
63
64 public ITextView View
65 {
66 get;
67 private set;
68 }
69
70 public ITrackingSpan MethodSpan
71 {
72 get;
73 private set;
74 }
75
76 public string TestCode
77 {
78 get;
79 private set;
80 }
81 }
82
83 internal sealed class InstantTagToggleAction
84 : ISmartTagAction, IDisposable
85 {
86 public static event EventHandler<InstantToggleEventArgs> Toggled;
87
88 public InstantTagToggleAction (ITextView view, ITrackingSpan methodSpan, string exampleCode)
89 {
90 if (view == null)
91 throw new ArgumentNullException ("view");
92 if (exampleCode == null)
93 throw new ArgumentNullException ("exampleCode");
94 if (methodSpan == null)
95 throw new ArgumentNullException ("methodSpan");
96
97 View = view;
98 ExampleCode = exampleCode;
99 MethodSpan = methodSpan;
95a11ae @ermau Fix new methods while one is running
authored Dec 19, 2012
100 IsEnabled = !isRunning;
1933b5e @ermau Use a smart tag instead of a custom button
authored Dec 16, 2012
101
95a11ae @ermau Fix new methods while one is running
authored Dec 20, 2012
102 Toggled += OnGlobalToggled;
1933b5e @ermau Use a smart tag instead of a custom button
authored Dec 16, 2012
103 }
104
105 public ITextView View
106 {
107 get;
108 private set;
109 }
110
111 public string ExampleCode
112 {
113 get;
a1e94eb @ermau Update method invocations, fixes #38
authored Dec 21, 2012
114 set;
1933b5e @ermau Use a smart tag instead of a custom button
authored Dec 16, 2012
115 }
116
117 public ITrackingSpan MethodSpan
118 {
119 get;
120 private set;
121 }
122
123 public void Invoke()
124 {
125 if (!IsRunning)
126 {
127 var window = new TestCodeWindow();
128 window.Owner = Application.Current.MainWindow;
129 string testCode = window.ShowForTestCode (ExampleCode);
130 if (testCode == null)
131 return;
132
133 IsRunning = true;
134 OnToggled (new InstantToggleEventArgs (View, MethodSpan, testCode));
135 }
136 else
137 {
138 IsRunning = false;
139 OnToggled (new InstantToggleEventArgs (View, MethodSpan));
140 }
141 }
142
143 public ReadOnlyCollection<SmartTagActionSet> ActionSets
144 {
145 get;
146 private set;
147 }
148
149 public ImageSource Icon
150 {
151 get;
152 private set;
153 }
154
155 public string DisplayText
156 {
157 get { return (IsRunning) ? "Stop Evaluating" : "Evaluate"; }
158 }
159
160 public bool IsEnabled
161 {
162 get;
163 private set;
164 }
165
166 public bool IsRunning
167 {
168 get;
169 private set;
170 }
171
95a11ae @ermau Fix new methods while one is running
authored Dec 20, 2012
172 private static bool isRunning = false;
1933b5e @ermau Use a smart tag instead of a custom button
authored Dec 16, 2012
173 private void OnToggled (InstantToggleEventArgs args)
174 {
95a11ae @ermau Fix new methods while one is running
authored Dec 20, 2012
175 isRunning = args.IsRunning;
176
1933b5e @ermau Use a smart tag instead of a custom button
authored Dec 16, 2012
177 var handler = Toggled;
178 if (handler != null)
179 handler (this, args);
180 }
181
182 public void Dispose()
183 {
95a11ae @ermau Fix new methods while one is running
authored Dec 20, 2012
184 Toggled -= OnGlobalToggled;
c032484 @ermau Actually fix smart tag cleanup, fixes #36
authored Dec 19, 2012
185
186 if (IsRunning)
187 Invoke();
1933b5e @ermau Use a smart tag instead of a custom button
authored Dec 16, 2012
188 }
189
95a11ae @ermau Fix new methods while one is running
authored Dec 20, 2012
190 private void OnGlobalToggled (object sender, InstantToggleEventArgs args)
1933b5e @ermau Use a smart tag instead of a custom button
authored Dec 16, 2012
191 {
192 if (args.View != View || sender == this)
193 return;
194
195 IsEnabled = !args.IsRunning;
196 }
197 }
198 }
Something went wrong with that request. Please try again.