forked from PavelTorgashov/FastColoredTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTextSourceSample2.cs
More file actions
141 lines (119 loc) · 3.94 KB
/
Copy pathCustomTextSourceSample2.cs
File metadata and controls
141 lines (119 loc) · 3.94 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using FastColoredTextBoxNS;
namespace Tester
{
public partial class CustomTextSourceSample2 : Form
{
public CustomTextSourceSample2()
{
InitializeComponent();
}
private string CreateBigString()
{
var sb = new StringBuilder();
var counter = 1;
for (int i = 0; i < 25000; i++)
{
sb.AppendLine(counter++ + " aaaaccccc");
sb.AppendLine(counter++ + " bbbbbbcc");
sb.AppendLine(counter++ + " cccaaaa");
sb.AppendLine(counter++ + " dddddaaaa");
}
return sb.ToString();
}
private void CustomTextSourceSample_Shown(object sender, EventArgs e)
{
//create our custom TextSource
var ts = new TextSourceWithLineFiltering(fctb);
//assign TextSource to the component
fctb.TextSource = ts;
//assign text to fctb
fctb.Text = CreateBigString();
fctb.ClearUndo();
}
private void tbLineFilter_TextChanged(object sender, EventArgs e)
{
(fctb.TextSource as TextSourceWithLineFiltering).LineFilterRegex = Regex.Escape(tbLineFilter.Text);
}
}
public class TextSourceWithLineFiltering : TextSource
{
List<int> toSourceIndex = new List<int>();
private string _lineFilterRegex;
public string LineFilterRegex
{
get { return _lineFilterRegex; }
set { _lineFilterRegex = value; UpdateFilter(); }
}
private void UpdateFilter()
{
toSourceIndex.Clear();
var count = base.lines.Count;
var regex = new Regex(LineFilterRegex);
for (int i = 0; i < count; i++)
{
if (regex.IsMatch(lines[i].Text))
toSourceIndex.Add(i);
}
CurrentTB.NeedRecalc(true);
CurrentTB.Invalidate();
}
public TextSourceWithLineFiltering(FastColoredTextBox tb) : base(tb)
{
}
public override int Count
{
get{ return toSourceIndex.Count;}
}
public override Line this[int i]
{
get{ return base[toSourceIndex[i]]; }
set{ base[toSourceIndex[i]] = value; }
}
public override void InsertLine(int index, Line line)
{
if (index >= toSourceIndex.Count)
{
var c = lines.Count;
while (index >= toSourceIndex.Count)
toSourceIndex.Add(c++);
}
else
{
var srcIndex = toSourceIndex[index];
toSourceIndex.Insert(index, srcIndex);
for (int i = index + 1; i < toSourceIndex.Count; i++)
toSourceIndex[i]++;
}
index = toSourceIndex[index];
base.InsertLine(index, line);
}
public override void RemoveLine(int index, int count)
{
for (int ii = index + count - 1; ii >= index; ii--)
{
var srcIndex = toSourceIndex[ii];
base.RemoveLine(srcIndex, 1);
toSourceIndex.RemoveAt(ii);
for (int i = index; i < toSourceIndex.Count; i++)
toSourceIndex[i]--;
}
}
public override int GetLineLength(int i)
{
return base.GetLineLength(toSourceIndex[i]);
}
public override bool LineHasFoldingStartMarker(int iLine)
{
return base.LineHasFoldingStartMarker(toSourceIndex[iLine]);
}
public override bool LineHasFoldingEndMarker(int iLine)
{
return base.LineHasFoldingEndMarker(toSourceIndex[iLine]);
}
}
}