-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeSearchProvider.cs
More file actions
113 lines (103 loc) · 3.89 KB
/
CodeSearchProvider.cs
File metadata and controls
113 lines (103 loc) · 3.89 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing;
namespace FIR
{
class CodeSearchProvider
{
private RichTextBox txtText;
string searchExpression;
//bool isCaseSensitive;
MatchCollection contextCodeMatched = null;
int currentSelected = -1;
public CodeSearchProvider(
RichTextBox txtText,
string searchExpression,
bool isCaseSensitive,
bool isRegex)
{
this.txtText = txtText;
if (isRegex)
//this.searchExpression = "(?s:" + searchExpression + ")";
this.searchExpression = searchExpression;
else
this.searchExpression = Regex.Replace(searchExpression, @"([\(\)\.\*\?\[\}\\\$\^\|\+])", @"\$1");// screening chars ().*?[}\$^|+
//this.isCaseSensitive = isCaseSensitive;
RecalculateContextCodeMatched(isCaseSensitive/*, isRegex*/);
}
private void RecalculateContextCodeMatched(bool IsCaseSensitive/*, bool IsRegex*/)
{
//Regex re = new Regex(searchExpression);
string text = txtText.Text;
RegexOptions optionsNonSingleString = new RegexOptions();
RegexOptions optionsSingleString;
if (!IsCaseSensitive)
optionsNonSingleString = RegexOptions.IgnoreCase;
optionsSingleString = optionsNonSingleString | RegexOptions.Singleline;
//re.Options = optionsSingleString;
if (Regex.IsMatch(text,searchExpression,optionsNonSingleString))
contextCodeMatched = Regex.Matches(text, searchExpression, optionsNonSingleString);
else
contextCodeMatched = Regex.Matches(text, searchExpression, optionsSingleString);
/* if (isCaseSensitive)
contextCodeMatched = Regex.Matches(txtText.Text, searchExpression);
else
contextCodeMatched = Regex.Matches(txtText.Text, searchExpression, RegexOptions.IgnoreCase);*/
}
public void SelectNext()
{
currentSelected++;
if (currentSelected >= contextCodeMatched.Count) currentSelected = 0;
txtText.Select(contextCodeMatched[currentSelected].Index, contextCodeMatched[currentSelected].Value.Length);
txtText.Focus();
}
public void SelectPrevious()
{
currentSelected--;
if (currentSelected < 0) currentSelected = contextCodeMatched.Count - 1;
txtText.Select(contextCodeMatched[currentSelected].Index, contextCodeMatched[currentSelected].Value.Length);
txtText.Focus();
}
public Founded[] Matches
{
get
{
Founded[] founds = new Founded[contextCodeMatched.Count];
for (int i = 0; i < contextCodeMatched.Count; i++)
founds[i] = new Founded(contextCodeMatched[i].Index, contextCodeMatched[i].Value.Length);
return founds;
}
}
public int Matched
{
get { return contextCodeMatched == null ? 0 : contextCodeMatched.Count; }
}
public int CurrentFounded
{
get { return currentSelected; }
}
}
public struct Founded
{
public Founded(int Index, int Length)
{
index = Index;
lenght = Length;
}
private int index;
public int Index
{
get { return index; }
//set { index = value; }
}
private int lenght;
public int Length
{
get { return lenght; }
//set { len = value; }
}
}
}