-
Notifications
You must be signed in to change notification settings - Fork 12
/
FindInFilesDialog.cs
604 lines (485 loc) · 22.8 KB
/
FindInFilesDialog.cs
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using Squared.Task;
using Squared.Task.Data;
using System.Text.RegularExpressions;
using Squared.Task.IO;
using System.Reflection;
using Squared.Task.Data.Mapper;
using Squared.Threading;
namespace Ndexer {
#if !MONO
public partial class FindInFilesDialog : Form {
[Mapper(Explicit=true)]
class FtsResult {
[Column("SourceFiles_Path")]
public string Path {
get;
set;
}
}
public class SearchQuery {
public readonly string Text;
public readonly Regex Regex;
public readonly string[] SearchWords;
public SearchQuery(string regex, RegexOptions options) {
Text = regex;
Regex = new Regex(regex, RegexOptions.Compiled | RegexOptions.ExplicitCapture | options);
// Ph'nglui Mglw'nafh Regex R'lyeh wgah'nagl fhtagn
var tempRe = new Regex(regex, RegexOptions.ExplicitCapture);
var recode = tempRe.GetType().GetField("code", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(tempRe);
var words = (string[])(recode.GetType().GetField("_strings", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(recode));
var result = new List<string>();
foreach (var word in words) {
if (word.Contains('\0'))
continue;
result.Add(word);
}
SearchWords = result.ToArray();
}
}
struct LineEntry {
public string Text;
public int LineNumber;
}
struct SearchResult {
public string Context;
public string Filename;
public long LineNumber;
}
static Color ErrorColor = Color.FromArgb(255, 220, 220);
ConnectionWrapper Connection = null;
IFuture ActiveSearch = null;
IFuture ActiveQueue = null;
SearchQuery ActiveSearchQuery = null;
SearchQuery PendingSearchQuery = null;
ListViewItem DefaultListItem = new ListViewItem(new string[2]);
IList<SearchResult> SearchResults = new List<SearchResult>();
float LineNumberWidth;
float LineHeight;
public const int SearchBufferSize = 1024 * 64;
public FindInFilesDialog () {
InitializeComponent();
string temp = "AaBbIiJjQqYyZz";
temp = String.Join("\r\n", new string[] { temp, temp, temp, temp });
lbResults.ItemHeight = TextRenderer.MeasureText(temp, lbResults.Font).Height + 3;
Size size = TextRenderer.MeasureText("00000", lbResults.Font);
LineNumberWidth = size.Width;
LineHeight = size.Height;
this.Enabled = false;
this.UseWaitCursor = true;
Text = String.Format("Search - {0}", Program.DatabasePath);
}
public void SetConnection(ConnectionWrapper connection) {
Connection = connection;
this.Enabled = true;
this.UseWaitCursor = false;
}
private void SetSearchResults (IList<SearchResult> items) {
SearchResults = items;
object o = new object();
lbResults.BeginUpdate();
while (lbResults.Items.Count > items.Count)
lbResults.Items.RemoveAt(lbResults.Items.Count - 1);
while (lbResults.Items.Count < items.Count)
lbResults.Items.Add(o);
if ((lbResults.SelectedIndex < 0) && (items.Count > 0))
lbResults.SelectedIndex = 0;
lbResults.EndUpdate();
}
private string FilterChars (string text, Func<char, bool> filter) {
char[] chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i++)
if (!filter(chars[i]))
chars[i] = ' ';
return new string(chars);
}
private TaskEnumerator<FtsResult> BuildQuery (SearchQuery search) {
var query = Connection.BuildQuery(
@"SELECT SourceFiles_Path FROM FullText, SourceFiles WHERE " +
@"FullText.FileText MATCH ? AND " +
@"FullText.SourceFiles_ID = SourceFiles.SourceFiles_ID"
);
var sb = new StringBuilder();
foreach (var word in search.SearchWords) {
if (sb.Length > 0)
sb.Append(" ");
var filteredWord = FilterChars(word, (ch) => char.IsLetterOrDigit(ch));
sb.Append("*");
sb.Append(filteredWord);
sb.Append("*");
}
return query.Execute<FtsResult>(sb.ToString());
}
Encoding DetectEncoding (System.IO.Stream stream) {
var reader = new System.IO.StreamReader(stream, true);
var buffer = new char[256];
reader.ReadBlock(buffer, 0, (int)Math.Min(buffer.Length, stream.Length));
var result = reader.CurrentEncoding;
stream.Seek(0, System.IO.SeekOrigin.Begin);
return result;
}
IEnumerator<object> SearchInFiles (SearchQuery search, BlockingQueue<string> filenames, IFuture completionFuture) {
var searchedFiles = new HashSet<string>(StringComparer.Ordinal);
var buffer = new List<SearchResult>();
var sb = new StringBuilder();
int numFiles = 0;
using (Finally.Do(() => {
SetSearchResults(buffer);
lblStatus.Text = String.Format("{0} result(s) found.", buffer.Count);
pbProgress.Style = ProgressBarStyle.Continuous;
pbProgress.Value = 0;
}))
while (filenames.Count > 0 || !completionFuture.Completed) {
var f = filenames.Dequeue();
yield return f;
var filename = f.Result as string;
if (filename == null)
continue;
if (searchedFiles.Contains(filename))
continue;
if (PendingSearchQuery != null)
break;
searchedFiles.Add(filename);
int lineNumber = 0;
var lineBuffer = new LineEntry[3];
var insertResult = (Action)(() => {
var item = new SearchResult();
item.Filename = filename;
item.LineNumber = lineBuffer[1].LineNumber;
sb.Remove(0, sb.Length);
for (int i = 0; i < 3; i++) {
if (lineBuffer[i].Text != null) {
var line = lineBuffer[i].Text;
if (line.Length > 512)
line = line.Substring(0, 512);
sb.Append(line);
}
if (i < 2)
sb.Append("\r\n");
}
item.Context = sb.ToString();
buffer.Add(item);
if ((buffer.Count % 250 == 0) || ((buffer.Count < 50) && (buffer.Count % 5 == 1)))
SetSearchResults(buffer);
});
var stepSearch = (Action)(() => {
string currentLine = lineBuffer[1].Text;
if ((currentLine != null) && search.Regex.IsMatch(currentLine))
insertResult();
});
var insertLine = (Action<LineEntry>)((line) => {
lineBuffer[0] = lineBuffer[1];
lineBuffer[1] = lineBuffer[2];
lineBuffer[2] = line;
stepSearch();
});
numFiles += 1;
if (numFiles % 50 == 0) {
lblStatus.Text = String.Format("Scanning '{0}'...", filename);
if (completionFuture.Completed) {
int totalNumFiles = numFiles + filenames.Count;
int progress = (numFiles * 1000 / totalNumFiles);
if (pbProgress.Value != progress)
pbProgress.Value = progress;
if (pbProgress.Style != ProgressBarStyle.Continuous)
pbProgress.Style = ProgressBarStyle.Continuous;
}
}
// Opening files is slow over network shares.
FileDataAdapter adapter = null;
var fAdapter = Future.RunInThread(
() => new FileDataAdapter(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)
);
yield return fAdapter;
if (fAdapter.Failed)
continue;
else
adapter = fAdapter.Result;
using (adapter) {
var fEncoding = Future.RunInThread(
() => DetectEncoding(adapter.BaseStream)
);
yield return fEncoding;
Future<string> thisLine = null, nextLine = null;
using (var reader = new AsyncTextReader(adapter, fEncoding.Result, SearchBufferSize))
while (true) {
thisLine = nextLine;
if (thisLine != null)
yield return thisLine;
nextLine = reader.ReadLine();
if (thisLine == null)
continue;
lineNumber += 1;
string line = thisLine.Result;
insertLine(new LineEntry { Text = line, LineNumber = lineNumber });
if (line == null)
break;
if (PendingSearchQuery != null)
break;
if (lineNumber % 10000 == 5000) {
var newStatus = String.Format("Scanning '{0}'... (line {1})", filename, lineNumber);
if (lblStatus.Text != newStatus)
lblStatus.Text = newStatus;
}
}
}
}
}
IEnumerator<object> PerformSearch (SearchQuery search) {
pbProgress.Style = ProgressBarStyle.Marquee;
lblStatus.Text = String.Format("Starting search...");
lbResults.Items.Clear();
var filenames = new BlockingQueue<string>();
var completionFuture = new Future<object>();
using (var fileSearch = Program.Scheduler.Start(
SearchInFiles(search, filenames, completionFuture),
TaskExecutionPolicy.RunAsBackgroundTask
)) {
using (var iterator = BuildQuery(search)) {
var f = Program.Scheduler.Start(iterator.Fetch());
yield return f;
if (!f.Failed) {
txtSearch.BackColor = SystemColors.Window;
while (!iterator.Disposed) {
if (PendingSearchQuery != null)
break;
foreach (var current in iterator)
filenames.Enqueue(current.Path);
yield return iterator.Fetch();
}
} else {
txtSearch.BackColor = ErrorColor;
}
}
completionFuture.Complete();
while (filenames.Count < 0)
filenames.Enqueue(null);
yield return fileSearch;
}
if (PendingSearchQuery != null) {
yield return BeginSearch();
} else {
pbProgress.Value = 0;
pbProgress.Style = ProgressBarStyle.Continuous;
}
}
IEnumerator<object> BeginSearch () {
ActiveSearchQuery = PendingSearchQuery;
PendingSearchQuery = null;
ActiveSearch = Program.Scheduler.Start(
PerformSearch(ActiveSearchQuery),
TaskExecutionPolicy.RunAsBackgroundTask
);
yield break;
}
IEnumerator<object> QueueNewSearch (string searchText) {
if ((searchText ?? "").Trim().Length == 0)
yield break;
RegexOptions regexOptions = RegexOptions.None;
if (btnEnableRegex.Checked == false) {
searchText = Regex.Escape(searchText);
regexOptions |= RegexOptions.CultureInvariant;
}
if (btnCaseSensitive.Checked == false) {
searchText = searchText.ToLower();
regexOptions |= RegexOptions.IgnoreCase;
regexOptions |= RegexOptions.CultureInvariant;
}
SearchQuery search = null;
try {
search = new SearchQuery(searchText, regexOptions);
txtSearch.BackColor = SystemColors.Window;
} catch {
txtSearch.BackColor = ErrorColor;
yield break;
}
ActiveQueue = null;
PendingSearchQuery = search;
if ((ActiveSearch == null) || (ActiveSearch.Completed)) {
yield return BeginSearch();
}
}
private void txtSearch_TextChanged (object sender, EventArgs e) {
btnClearSearchField.Visible = (txtSearch.Text.Length > 0);
SearchParametersChanged();
}
private void SearchDialog_FormClosing (object sender, FormClosingEventArgs e) {
if (Connection != null) {
Connection.Dispose();
Connection = null;
}
if (ActiveSearch != null) {
ActiveSearch.Dispose();
ActiveSearch = null;
}
if (ActiveQueue != null) {
ActiveQueue.Dispose();
ActiveQueue = null;
}
}
private void SearchParametersChanged() {
string searchText = txtSearch.Text.Trim();
ActiveQueue = Program.Scheduler.Start(
QueueNewSearch(searchText),
TaskExecutionPolicy.RunAsBackgroundTask
);
}
private void txtSearch_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
e.Handled = true;
e.SuppressKeyPress = true;
OpenItem(lbResults.SelectedIndex);
} else if (e.KeyCode == Keys.Up) {
e.Handled = true;
e.SuppressKeyPress = true;
lbResults.SelectedIndex = Math.Max(0, lbResults.SelectedIndex - 1);
} else if (e.KeyCode == Keys.Down) {
e.Handled = true;
e.SuppressKeyPress = true;
lbResults.SelectedIndex = Math.Min(lbResults.Items.Count - 1, lbResults.SelectedIndex + 1);
}
}
private void SearchDialog_FormClosed (object sender, FormClosedEventArgs e) {
this.Dispose();
}
private void lbResults_DrawItem (object sender, DrawItemEventArgs e) {
e.DrawBackground();
bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
if ((e.Index < 0) || (e.Index >= SearchResults.Count))
return;
var search = ActiveSearchQuery;
if (search == null)
return;
using (var backgroundBrush = new SolidBrush(selected ? SystemColors.Highlight : lbResults.BackColor))
using (var brush = new SolidBrush(selected ? SystemColors.HighlightText : lbResults.ForeColor)) {
var item = SearchResults[e.Index];
var format = StringFormat.GenericTypographic;
format.LineAlignment = StringAlignment.Near;
format.FormatFlags = StringFormatFlags.NoWrap;
format.Trimming = StringTrimming.EllipsisPath;
format.Alignment = StringAlignment.Near;
var rect = new RectangleF(e.Bounds.X, e.Bounds.Y + 1, e.Bounds.Width - LineNumberWidth, LineHeight);
e.Graphics.DrawString(item.Filename, lbResults.Font, brush, rect, format);
format.Trimming = StringTrimming.Character;
format.Alignment = StringAlignment.Far;
rect = new RectangleF(e.Bounds.X, e.Bounds.Y + 1, e.Bounds.Width, LineHeight);
e.Graphics.DrawString(item.LineNumber.ToString(), lbResults.Font, brush, rect, format);
string context = item.Context;
MatchEvaluator blankEvaluator = (m) => new String(' ', m.Length);
string cleanContext = search.Regex.Replace(item.Context, blankEvaluator);
format.Trimming = StringTrimming.None;
format.Alignment = StringAlignment.Near;
rect = new RectangleF(e.Bounds.X + 6, e.Bounds.Y + LineHeight + 1, e.Bounds.Width - 6, e.Bounds.Height - LineHeight);
using (var pen = new Pen(SystemColors.ButtonShadow, 1.0f))
e.Graphics.DrawLine(pen, rect.Location, new PointF(rect.Right, rect.Top));
e.Graphics.DrawString(cleanContext, lbResults.Font, brush, rect, format);
using (var maskBrush = new SolidBrush(Color.FromArgb(127, selected ? SystemColors.Highlight : lbResults.BackColor))) {
var mrect = new RectangleF(e.Bounds.X + 6, e.Bounds.Y + LineHeight + 1, e.Bounds.Width - 6, LineHeight);
e.Graphics.FillRectangle(maskBrush, mrect);
mrect = new RectangleF(e.Bounds.X + 6, e.Bounds.Y + (LineHeight * 3) + 1, e.Bounds.Width - 6, LineHeight);
e.Graphics.FillRectangle(maskBrush, mrect);
}
var matches = search.Regex.Matches(context);
if (matches.Count > 0) {
var ranges = (from m in matches.Cast<Match>() select new CharacterRange(m.Index, m.Length)).ToArray();
int blockSize = Math.Min(32, ranges.Length);
var temp = new CharacterRange[blockSize];
e.Graphics.ResetClip();
e.Graphics.ExcludeClip(e.Graphics.Clip);
for (int i = 0; i < ranges.Length; i += 32) {
Array.Copy(ranges, i, temp, 0, Math.Min(blockSize, ranges.Length - i));
format.SetMeasurableCharacterRanges(temp);
foreach (var region in e.Graphics.MeasureCharacterRanges(item.Context, lbResults.Font, rect, format))
e.Graphics.SetClip(region, System.Drawing.Drawing2D.CombineMode.Union);
}
using (var highlightBrush = new SolidBrush(SystemColors.Highlight))
using (var highlightTextBrush = new SolidBrush(SystemColors.HighlightText)) {
e.Graphics.FillRectangle(highlightBrush, rect);
e.Graphics.DrawString(context, lbResults.Font, highlightTextBrush, rect, format);
}
e.Graphics.ResetClip();
}
}
e.DrawFocusRectangle();
}
void OpenItem (int index) {
SearchResult item;
try {
item = SearchResults[index];
} catch {
return;
}
using (var director = Program.GetDirector()) {
director.OpenFile(item.Filename, item.LineNumber);
director.BringToFront();
}
}
private void lbResults_MouseDoubleClick (object sender, MouseEventArgs e) {
int index = lbResults.IndexFromPoint(e.X, e.Y);
OpenItem(index);
}
private void btnClearSearchField_Click (object sender, EventArgs e) {
txtSearch.Text = "";
if (ActiveSearch != null)
ActiveSearch.Dispose();
ActiveSearch = null;
ActiveSearchQuery = PendingSearchQuery = null;
}
private void btnCaseSensitive_Click (object sender, EventArgs e) {
SearchParametersChanged();
}
private void btnEnableRegex_Click (object sender, EventArgs e) {
SearchParametersChanged();
}
private void mnuCopyFilenames_Click (object sender, EventArgs e) {
var sr = SearchResults;
var hs = new HashSet<string>();
var sb = new StringBuilder();
foreach (var r in sr) {
if (hs.Add(r.Filename))
sb.AppendLine(r.Filename);
}
Clipboard.Clear();
Clipboard.SetText(sb.ToString());
}
private void mnuCopyFilenamesAndLineNumbers_Click (object sender, EventArgs e) {
var sr = SearchResults;
var sb = new StringBuilder();
foreach (var r in sr)
sb.AppendFormat("{0}\t{1}\r\n", r.Filename, r.LineNumber);
Clipboard.Clear();
Clipboard.SetText(sb.ToString());
}
private void mnuCopyFiles_Click (object sender, EventArgs e) {
var sr = SearchResults;
var hs = new HashSet<string>();
var sc = new System.Collections.Specialized.StringCollection();
foreach (var r in sr) {
if (hs.Add(r.Filename))
sc.Add(r.Filename);
}
Clipboard.Clear();
Clipboard.SetFileDropList(sc);
}
private void lbResults_KeyDown (object sender, KeyEventArgs e) {
if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Space)) {
e.Handled = true;
e.SuppressKeyPress = true;
OpenItem(lbResults.SelectedIndex);
}
}
private void FindInFilesDialog_Load (object sender, EventArgs e) {
AutoScaleDimensions = new SizeF(96, 96);
AutoScaleMode = AutoScaleMode.Dpi;
}
}
#endif
}