1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Editing;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Tango.Core.Commands;
using Tango.SharedUI;
namespace Tango.SharedUI.Controls
{
#region Completion
internal class CompletionData : ICompletionData
{
private String _description;
public BitmapSource Source { get; set; }
public CompletionData(string text, String description)
{
this.Text = text;
_description = description;
}
public System.Windows.Media.ImageSource Image
{
get { return Source; }
}
public string Text { get; private set; }
// Use this property if you want to show a fancy UIElement in the drop down list.
public object Content
{
get { return this.Text; }
}
public object Description
{
get { return _description; }
}
public double Priority { get { return 0; } }
public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
{
textArea.Document.Replace(completionSegment, this.Text);
}
public override string ToString()
{
return Text;
}
}
#endregion
/// <summary>
/// Interaction logic for ScriptEditorControl.xaml
/// </summary>
public partial class ScriptEditorControl : UserControl
{
private CompletionWindow completionWindow;
public ScriptEditorControl()
{
InitializeComponent();
textEditor.TextArea.IndentationStrategy = new ICSharpCode.AvalonEdit.Indentation.CSharp.CSharpIndentationStrategy();
textEditor.TextArea.TextEntering += textEditor_TextArea_TextEntering;
textEditor.TextArea.TextEntered += textEditor_TextArea_TextEntered;
}
private void textEditor_TextArea_TextEntered(object sender, TextCompositionEventArgs e)
{
if (e.Text == ".")
{
String keyword = textEditor.TextArea.GetJustCurrentWord();
if (keyword != null)
{
completionWindow = new CompletionWindow(textEditor.TextArea);
completionWindow.WindowStyle = WindowStyle.None;
completionWindow.AllowsTransparency = true;
completionWindow.ResizeMode = ResizeMode.NoResize;
IList<ICompletionData> data = completionWindow.CompletionList.CompletionData;
bool ok = false;
List<KeyValuePair<String, Type>> types = new List<KeyValuePair<String, Type>>();
types.Add(new KeyValuePair<string, Type>("Thread", typeof(Thread)));
types.Add(new KeyValuePair<string, Type>("DateTime", typeof(DateTime)));
types.Add(new KeyValuePair<string, Type>("TimeSpan", typeof(TimeSpan)));
types.Add(new KeyValuePair<string, Type>("Dispatcher", typeof(Dispatcher)));
types.Add(new KeyValuePair<string, Type>("Task", typeof(Task)));
types.Add(new KeyValuePair<string, Type>("List", typeof(IList<Object>)));
types.Add(new KeyValuePair<string, Type>("int", typeof(Int32)));
types.Add(new KeyValuePair<string, Type>("double", typeof(Double)));
types.Add(new KeyValuePair<string, Type>("String", typeof(String)));
types.Add(new KeyValuePair<string, Type>("string", typeof(String)));
if (HighlightTypes != null)
{
foreach (var t in HighlightTypes)
{
types.Add(new KeyValuePair<string, Type>(t.Key, t.Value));
}
}
var type = types.SingleOrDefault(x => x.Key == keyword);
if (type.Key != null)
{
ok = true;
FillType(type.Value, data);
}
if (ok)
{
completionWindow.Show();
completionWindow.Closed += delegate
{
completionWindow = null;
};
}
}
}
}
private void textEditor_TextArea_TextEntering(object sender, TextCompositionEventArgs e)
{
if (e.Text.Length > 0 && completionWindow != null)
{
if (!char.IsLetterOrDigit(e.Text[0]))
{
// Whenever a non-letter is typed while the completion window is open,
// insert the currently selected element.
completionWindow.CompletionList.RequestInsertion(e);
}
}
}
private void FillType(Type type, IList<ICompletionData> data)
{
List<CompletionData> items = new List<CompletionData>();
foreach (var method in type.GetMethods().Where(x => x.IsPublic && !x.IsSpecialName))
{
String desc = method.ReturnType.Name + " " + method.Name + "(" + String.Join(", ", method.GetParameters().Select(x => x.ParameterType.Name + " " + x.Name).ToArray()) + ")";
items.Add(new CompletionData(method.Name, desc) { Source = ResourceHelper.GetImageFromResources("Images/pubmethod.gif") });
}
foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
String desc = property.PropertyType.Name + " " + property.Name;
items.Add(new CompletionData(property.Name, desc) { Source = ResourceHelper.GetImageFromResources("Images/pubproperty.gif") });
}
foreach (var ev in type.GetEvents(BindingFlags.Instance | BindingFlags.Public))
{
try
{
String desc = ev.Name + " " + "(" + String.Join(", ", ev.EventHandlerType.GetMethod("Invoke").GetParameters().Select(x => x.ParameterType.Name + " " + x.Name).ToArray()) + ")";
items.Add(new CompletionData(ev.Name, desc) { Source = ResourceHelper.GetImageFromResources("Images/pubevent.gif") });
}
catch { }
}
foreach (var item in items.OrderBy(x => x.Text))
{
data.Add(item);
}
}
private void FillAssembly(Assembly asm, IList<ICompletionData> data)
{
var q = from t in asm.GetTypes()
where t.IsClass
select t;
foreach (var type in q)
{
data.Add(new CompletionData(type.Name, "Class") { Source = ResourceHelper.GetImageFromResources("Images/pubclass.gif") });
}
}
#region Properties
public String Text
{
get { return (String)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(String), typeof(ScriptEditorControl), new PropertyMetadata(null, (d, e) => (d as ScriptEditorControl).OnTextChanged()));
private void OnTextChanged()
{
if (textEditor.Text != Text)
{
textEditor.Text = Text;
}
}
public ObservableCollection<KeyValuePair<string, Type>> HighlightTypes
{
get { return (ObservableCollection<KeyValuePair<string, Type>>)GetValue(HighlightTypesProperty); }
set { SetValue(HighlightTypesProperty, value); }
}
public static readonly DependencyProperty HighlightTypesProperty =
DependencyProperty.Register("HighlightTypes", typeof(ObservableCollection<KeyValuePair<string, Type>>), typeof(ScriptEditorControl), new PropertyMetadata(null));
#endregion
#region Commands
public RelayCommand RunCommand
{
get { return (RelayCommand)GetValue(RunCommandProperty); }
set { SetValue(RunCommandProperty, value); }
}
public static readonly DependencyProperty RunCommandProperty =
DependencyProperty.Register("RunCommand", typeof(RelayCommand), typeof(ScriptEditorControl), new PropertyMetadata(null));
public RelayCommand StopCommand
{
get { return (RelayCommand)GetValue(StopCommandProperty); }
set { SetValue(StopCommandProperty, value); }
}
public static readonly DependencyProperty StopCommandProperty =
DependencyProperty.Register("StopCommand", typeof(RelayCommand), typeof(ScriptEditorControl), new PropertyMetadata(null));
public RelayCommand SaveCommand
{
get { return (RelayCommand)GetValue(SaveCommandProperty); }
set { SetValue(SaveCommandProperty, value); }
}
public static readonly DependencyProperty SaveCommandProperty =
DependencyProperty.Register("SaveCommand", typeof(RelayCommand), typeof(ScriptEditorControl), new PropertyMetadata(null));
#endregion
private void textEditor_TextChanged(object sender, EventArgs e)
{
Text = textEditor.Text;
}
}
internal static class DocumentUtils
{
private static Regex _wordRegex = new Regex(@"[^\W\d][\w]*(?<=\w)", RegexOptions.Compiled);
public static string GetJustCurrentWord(this TextArea textArea)
{
try
{
DocumentLine line = textArea.Document.GetLineByNumber(textArea.Caret.Line);
if (line.Length == 0)
return null;
int lineCaretPosition = textArea.Caret.Offset - line.Offset;
String l = textArea.Document.GetText(line);
String trimmed = l.Remove(lineCaretPosition, l.Length - lineCaretPosition);
return SplitToWords(trimmed).LastOrDefault(x => !String.IsNullOrWhiteSpace(x));
}
catch
{
return null;
}
}
public static List<String> SplitToWords(String text)
{
text = text.Replace(".", " ");
text = text.Replace("(", " ");
text = text.Replace(")", " ");
text = text.Replace(",", " ");
var punctuation = text.Where(Char.IsPunctuation).Distinct().ToArray();
var words = text.Split().Select(x => x.Trim(punctuation));
return words.ToList();
}
}
}
|