~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor/ISearchEngine.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
using System;
27
27
using System.Text.RegularExpressions;
 
28
using System.Collections.Generic;
28
29
 
29
30
namespace Mono.TextEditor
30
31
{
49
50
                SearchResult GetMatchAt (int offset);
50
51
                SearchResult GetMatchAt (int offset, int length);
51
52
                
52
 
                SearchResult SearchForward (System.ComponentModel.BackgroundWorker worker, int fromOffset);
53
 
                SearchResult SearchBackward (System.ComponentModel.BackgroundWorker worker, int fromOffset);
 
53
                SearchResult SearchForward (System.ComponentModel.BackgroundWorker worker, TextViewMargin.SearchWorkerArguments args, int fromOffset);
 
54
                SearchResult SearchBackward (System.ComponentModel.BackgroundWorker worker, TextViewMargin.SearchWorkerArguments args, int fromOffset);
54
55
                
55
56
                SearchResult SearchForward (int fromOffset);
56
57
                SearchResult SearchBackward (int fromOffset);
57
58
                
58
59
                void Replace (SearchResult result, string pattern);
 
60
 
 
61
                int ReplaceAll (string withPattern);
59
62
                
60
63
                ISearchEngine Clone ();
61
64
        }
111
114
                public abstract SearchResult GetMatchAt (int offset);
112
115
                public abstract SearchResult GetMatchAt (int offset, int length);
113
116
                
114
 
                public abstract SearchResult SearchForward (System.ComponentModel.BackgroundWorker worker, int fromOffset);
115
 
                public abstract SearchResult SearchBackward (System.ComponentModel.BackgroundWorker worker, int fromOffset);
 
117
                public abstract SearchResult SearchForward (System.ComponentModel.BackgroundWorker worker, TextViewMargin.SearchWorkerArguments args, int fromOffset);
 
118
                public abstract SearchResult SearchBackward (System.ComponentModel.BackgroundWorker worker, TextViewMargin.SearchWorkerArguments args, int fromOffset);
116
119
                
117
120
                public SearchResult SearchForward (int fromOffset)
118
121
                {
119
 
                        return SearchForward (null, fromOffset);
 
122
                        return SearchForward (null, new TextViewMargin.SearchWorkerArguments () { Text = textEditorData.Text }, fromOffset);
120
123
                }
 
124
 
121
125
                public SearchResult SearchBackward (int fromOffset)
122
126
                {
123
 
                        return SearchBackward (null, fromOffset);
 
127
                        return SearchBackward (null, new TextViewMargin.SearchWorkerArguments () { Text = textEditorData.Text }, fromOffset);
124
128
                }
125
129
                public abstract void Replace (SearchResult result, string pattern);
 
130
 
 
131
                public virtual int ReplaceAll (string withPattern)
 
132
                {
 
133
                        int result = 0;
 
134
                        using (var undo = textEditorData.OpenUndoGroup ()) {
 
135
                                int offset = 0;
 
136
                                if (!SearchRequest.SearchRegion.IsInvalid)
 
137
                                        offset = SearchRequest.SearchRegion.Offset;
 
138
                                SearchResult searchResult; 
 
139
                                var text = textEditorData.Text;
 
140
                                var args = new TextViewMargin.SearchWorkerArguments () { Text = text };
 
141
                                while (true) {
 
142
                                        searchResult = SearchForward (null, args, offset);
 
143
                                        if (searchResult == null || searchResult.SearchWrapped)
 
144
                                                break;
 
145
                                        Replace (searchResult, withPattern);
 
146
                                        offset = searchResult.EndOffset;
 
147
                                        result++;
 
148
                                }
 
149
                                if (result > 0)
 
150
                                        textEditorData.ClearSelection ();
 
151
                        }
 
152
                        return result;
 
153
                }
 
154
 
126
155
                public virtual ISearchEngine Clone ()
127
156
                {
128
157
                        ISearchEngine result = (ISearchEngine)MemberwiseClone ();
183
212
                        return null;
184
213
                }
185
214
                
186
 
                public override SearchResult SearchForward (System.ComponentModel.BackgroundWorker worker, int fromOffset)
 
215
                public override SearchResult SearchForward (System.ComponentModel.BackgroundWorker worker, TextViewMargin.SearchWorkerArguments args, int fromOffset)
187
216
                {
188
217
                        if (!string.IsNullOrEmpty (SearchRequest.SearchPattern)) {
189
218
                                // TODO: Optimize
190
 
                                for (int i = 0; i < this.textEditorData.Document.TextLength; i++) {
191
 
                                        int offset = (fromOffset + i) % this.textEditorData.Document.TextLength;
 
219
                                for (int i = 0; i < args.Text.Length; i++) {
 
220
                                        int offset = (fromOffset + i) % args.Text.Length;
192
221
                                        if (worker != null && worker.CancellationPending)
193
222
                                                return null; 
194
223
                                        if (IsMatchAt (offset) && (searchRequest.SearchRegion.IsInvalid || searchRequest.SearchRegion.Contains (offset)))
198
227
                        return null;
199
228
                }
200
229
                
201
 
                public override SearchResult SearchBackward (System.ComponentModel.BackgroundWorker worker, int fromOffset)
 
230
                public override SearchResult SearchBackward (System.ComponentModel.BackgroundWorker worker, TextViewMargin.SearchWorkerArguments args, int fromOffset)
202
231
                {
203
232
                        if (!string.IsNullOrEmpty (SearchRequest.SearchPattern)) {
204
233
                                // TODO: Optimize
205
 
                                for (int i = 0; i < this.textEditorData.Document.TextLength; i++) {
206
 
                                        int offset = (fromOffset + this.textEditorData.Document.TextLength * 2 - 1 - i) % this.textEditorData.Document.TextLength;
 
234
                                for (int i = 0; i < args.Text.Length; i++) {
 
235
                                        int offset = (fromOffset + args.Text.Length * 2 - 1 - i) % args.Text.Length;
207
236
                                        if (worker != null && worker.CancellationPending)
208
237
                                                return null;
209
238
                                        if (IsMatchAt (offset) && (searchRequest.SearchRegion.IsInvalid || searchRequest.SearchRegion.Contains (offset)))
212
241
                        }
213
242
                        return null;
214
243
                }
215
 
                
 
244
 
216
245
                public override void Replace (SearchResult result, string pattern)
217
246
                {
218
247
                        textEditorData.Replace (result.Offset, result.Length, pattern);
219
248
                }
 
249
 
 
250
                public override int ReplaceAll (string withPattern)
 
251
                {
 
252
                        var searchResults = new List<SearchResult> ();
 
253
 
 
254
                        int offset = 0;
 
255
                        if (!SearchRequest.SearchRegion.IsInvalid)
 
256
                                offset = SearchRequest.SearchRegion.Offset;
 
257
                        SearchResult searchResult; 
 
258
                        var text = textEditorData.Text;
 
259
                        var args = new TextViewMargin.SearchWorkerArguments () { Text = text };
 
260
                        while (true) {
 
261
                                searchResult = SearchForward (null, args, offset);
 
262
                                if (searchResult == null || searchResult.SearchWrapped)
 
263
                                        break;
 
264
                                searchResults.Add (searchResult);
 
265
                                offset = searchResult.EndOffset;
 
266
                        }
 
267
                        if (searchResults.Count < 100) {
 
268
                                using (var undo = textEditorData.OpenUndoGroup ()) {
 
269
                                        for (int i = searchResults.Count - 1; i >= 0; i--) {
 
270
                                                Replace (searchResults [i], withPattern);
 
271
                                        }
 
272
                                        if (searchResults.Count > 0)
 
273
                                                textEditorData.ClearSelection ();
 
274
                                }
 
275
                        } else {
 
276
                                char[] oldText = text.ToCharArray ();
 
277
                                char[] newText = new char[oldText.Length + searchResults.Count * (withPattern.Length - compiledPattern.Length)];
 
278
                                char[] pattern = withPattern.ToCharArray ();
 
279
                                int curOffset = 0, destOffset = 0;
 
280
                                foreach (var sr in searchResults) {
 
281
                                        var length = sr.Offset - curOffset;
 
282
                                        Array.Copy (oldText, curOffset, newText, destOffset, length);
 
283
                                        destOffset += length;
 
284
                                        Array.Copy (pattern, 0, newText, destOffset, pattern.Length);
 
285
                                        destOffset += withPattern.Length;
 
286
                                        curOffset = sr.EndOffset;
 
287
                                }
 
288
                                var l = textEditorData.Length - curOffset;
 
289
                                Array.Copy (oldText, curOffset, newText, destOffset, l);
 
290
 
 
291
                                textEditorData.Replace (0, textEditorData.Length, new string (newText));
 
292
                                textEditorData.ClearSelection ();
 
293
                        }
 
294
                        return searchResults.Count;
 
295
                }
220
296
        }
221
297
        
222
298
        public class RegexSearchEngine : AbstractSearchEngine
272
348
                        return null;
273
349
                }
274
350
                
275
 
                public override SearchResult SearchForward (System.ComponentModel.BackgroundWorker worker, int fromOffset)
 
351
                public override SearchResult SearchForward (System.ComponentModel.BackgroundWorker worker, TextViewMargin.SearchWorkerArguments args, int fromOffset)
276
352
                {
277
353
                        if (regex == null || String.IsNullOrEmpty (searchRequest.SearchPattern))
278
354
                                return null;
279
 
                        System.Text.RegularExpressions.Match match = regex.Match (this.textEditorData.Document.Text, fromOffset);
 
355
                        System.Text.RegularExpressions.Match match = regex.Match (args.Text, fromOffset);
280
356
                        if (match.Success) {
281
357
                                return new SearchResult (match.Index, 
282
358
                                                         match.Length, false);
283
359
                        }
284
 
                        match = regex.Match (this.textEditorData.Document.Text, 0, fromOffset);
 
360
                        match = regex.Match (args.Text, 0, fromOffset);
285
361
                        if (match.Success) {
286
362
                                return new SearchResult (match.Index, 
287
363
                                                         match.Length, true);
289
365
                        return null;
290
366
                }
291
367
                
292
 
                public override SearchResult SearchBackward (System.ComponentModel.BackgroundWorker worker, int fromOffset)
 
368
                public override SearchResult SearchBackward (System.ComponentModel.BackgroundWorker worker, TextViewMargin.SearchWorkerArguments args,  int fromOffset)
293
369
                {
294
370
                        if (regex == null || String.IsNullOrEmpty (searchRequest.SearchPattern))
295
371
                                return null;
296
372
                        System.Text.RegularExpressions.Match found = null; 
297
373
                        System.Text.RegularExpressions.Match last = null; 
298
 
                        foreach (System.Text.RegularExpressions.Match match in regex.Matches (this.textEditorData.Document.Text)) {
 
374
                        foreach (System.Text.RegularExpressions.Match match in regex.Matches (args.Text)) {
299
375
                                if (match.Index < fromOffset) {
300
376
                                        found = match;
301
377
                                }