forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnippetManager.cs
More file actions
242 lines (228 loc) · 8.49 KB
/
Copy pathSnippetManager.cs
File metadata and controls
242 lines (228 loc) · 8.49 KB
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
using System;
using System.IO;
using System.Text;
using System.Drawing;
using WeifenLuo.WinFormsUI;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using PluginCore.Localization;
using FlashDevelop.Utilities;
using FlashDevelop.Helpers;
using PluginCore.Utilities;
using PluginCore.Controls;
using PluginCore.Helpers;
using PluginCore.Managers;
using System.Collections;
using ScintillaNet;
using PluginCore;
namespace FlashDevelop.Managers
{
class SnippetManager
{
/// <summary>
/// Gets a snippet from a file in the snippets directory
/// </summary>
public static String GetSnippet(String word, String syntax, Encoding current)
{
String global = Path.Combine(PathHelper.SnippetDir, word + ".fds");
String specificDir = Path.Combine(PathHelper.SnippetDir, syntax);
String specific = Path.Combine(specificDir, word + ".fds");
if (File.Exists(specific))
{
EncodingFileInfo info = FileHelper.GetEncodingFileInfo(specific);
return DataConverter.ChangeEncoding(info.Contents, info.CodePage, current.CodePage);
}
else if (File.Exists(global))
{
EncodingFileInfo info = FileHelper.GetEncodingFileInfo(global);
return DataConverter.ChangeEncoding(info.Contents, info.CodePage, current.CodePage);
}
else return null;
}
/// <summary>
/// Inserts text from the snippets class
/// </summary>
public static Boolean InsertTextByWord(String word, Boolean emptyUndoBuffer)
{
ScintillaControl sci = Globals.SciControl;
if (sci == null) return false;
Boolean canShowList = false;
String snippet = null;
if (word == null)
{
canShowList = true;
word = sci.GetWordFromPosition(sci.CurrentPos);
}
if (word != null && word.Length > 0)
{
snippet = GetSnippet(word, sci.ConfigurationLanguage, sci.Encoding);
}
// let plugins handle the snippet
Hashtable data = new Hashtable();
data["word"] = word;
data["snippet"] = snippet;
DataEvent de = new DataEvent(EventType.Command, "SnippetManager.Expand", data);
EventManager.DispatchEvent(Globals.MainForm, de);
if (de.Handled) return true;
snippet = (string)data["snippet"];
if (!String.IsNullOrEmpty(sci.SelText))
{
// Remember the previous selection
ArgsProcessor.PrevSelText = sci.SelText;
}
if (snippet != null)
{
Int32 endPos = sci.SelectionEnd;
Int32 startPos = sci.SelectionStart;
String curWord = sci.GetWordFromPosition(endPos);
if (startPos == endPos)
{
endPos = sci.WordEndPosition(sci.CurrentPos, true);
startPos = sci.WordStartPosition(sci.CurrentPos, true);
sci.SetSel(startPos, endPos);
}
if (!String.IsNullOrEmpty(curWord))
{
// Remember the current word
ArgsProcessor.PrevSelWord = curWord;
}
SnippetHelper.InsertSnippetText(sci, endPos, snippet);
return true;
}
else if (canShowList)
{
ICompletionListItem item;
List<ICompletionListItem> items = new List<ICompletionListItem>();
PathWalker walker = new PathWalker(PathHelper.SnippetDir, "*.fds", false);
List<String> files = walker.GetFiles();
foreach (String file in files)
{
item = new SnippetItem(Path.GetFileNameWithoutExtension(file), file);
items.Add(item);
}
String path = Path.Combine(PathHelper.SnippetDir, sci.ConfigurationLanguage);
if (Directory.Exists(path))
{
walker = new PathWalker(path, "*.fds", false);
files = walker.GetFiles();
foreach (String file in files)
{
item = new SnippetItem(Path.GetFileNameWithoutExtension(file), file);
items.Add(item);
}
}
if (items.Count > 0)
{
items.Sort();
if (!String.IsNullOrEmpty(sci.SelText)) word = sci.SelText;
else
{
word = sci.GetWordFromPosition(sci.CurrentPos);
if (word == null) word = String.Empty;
}
CompletionList.OnInsert += new InsertedTextHandler(HandleListInsert);
CompletionList.OnCancel += new InsertedTextHandler(HandleListInsert);
CompletionList.Show(items, false, word);
return true;
}
}
return false;
}
/// <summary>
/// On completion list insert or cancel, reset the previous selection
/// </summary>
private static void HandleListInsert(ScintillaControl sender, Int32 position, String text, Char trigger, ICompletionListItem item)
{
CompletionList.OnInsert -= new InsertedTextHandler(HandleListInsert);
CompletionList.OnCancel -= new InsertedTextHandler(HandleListInsert);
ArgsProcessor.PrevSelText = String.Empty;
}
}
public class SnippetItem : ICompletionListItem, IComparable, IComparable<ICompletionListItem>
{
private String word;
private String snippet;
private String fileName;
private Bitmap icon;
public SnippetItem(String word, String fileName)
{
this.word = word;
this.fileName = fileName;
}
/// <summary>
/// Label of the snippet item
/// </summary>
public String Label
{
get { return this.word; }
}
/// <summary>
/// Description of the snippet item
/// </summary>
public String Description
{
get
{
String desc = TextHelper.GetString("Info.SnippetItemDesc");
if (this.snippet == null)
{
this.snippet = FileHelper.ReadFile(this.fileName);
this.snippet = ArgsProcessor.ProcessCodeStyleLineBreaks(this.snippet);
this.snippet = this.snippet.Replace(SnippetHelper.ENTRYPOINT, "|");
this.snippet = this.snippet.Replace(SnippetHelper.EXITPOINT, "|");
}
if (this.snippet.Length > 40) return desc + ": " + this.snippet.Substring(0, 40) + "...";
else return desc + ": " + this.snippet;
}
}
/// <summary>
/// String value if the snippet item
/// </summary>
public String Value
{
get
{
SnippetManager.InsertTextByWord(this.word, false);
return null;
}
}
/// <summary>
/// Icon if the snippet item
/// </summary>
public Bitmap Icon
{
get
{
if (icon == null)
{
Image image = Globals.MainForm.FindImage("341");
this.icon = new Bitmap(image);
}
return icon;
}
set
{
this.icon = value;
}
}
/// <summary>
/// Checks the validity of the completion list item
/// </summary>
Int32 IComparable.CompareTo(Object obj)
{
if (obj as ICompletionListItem != null) return String.Compare(Label, (obj as ICompletionListItem).Label, true);
else
{
String message = TextHelper.GetString("Info.CompareError");
throw new Exception(message);
}
}
/// <summary>
/// Compares the completion list items
/// </summary>
Int32 IComparable<ICompletionListItem>.CompareTo(ICompletionListItem other)
{
return String.Compare(Label, other.Label, true);
}
}
}