Exemplo n.º 1
0
        public void ReplaceAll(string text, string textToReplace, Document doc, SearchFlags flags, SearchScope scope)
        {
            lastSettings = null;
            scanned = null;
            var items = new List<ResultItem>();
            var sw = new Stopwatch();
            sw.Start();
            var rootEditor = (ITextEditor)App.GetService<IEditorService>().GetEditor(doc.GetType()).Instance;
            rootEditor.BeginUndoAction();

            var startPos = scope == SearchScope.Selection ? rootEditor.SelectionStart :
                scope == SearchScope.Current ? rootEditor.CaretPosition : 0;
            var endPos = scope == SearchScope.Selection ? rootEditor.SelectionEnd : 0;

            for (;;)
            {
                var res = Search(text, lastSettings != null ? lastSettings.LastDocument : doc, flags, scope, (d, r) => {
                    var editor = (ITextEditor)App.GetService<IEditorService>().GetEditor(d.GetType()).Instance;
                    var loc = editor.GetLocationFromPosition(d, r.StartPosition);
                    var txt = editor.GetContent(d, loc.Line).TrimStart(' ', '\t');
                    var item = new ResultItem(txt, d, loc.Line, loc.Column, r.EndPosition - r.StartPosition);

                    var docSrv = App.GetService<IDocumentService>();

                    if (docSrv.GetActiveDocument() != d)
                    {
                        rootEditor.EndUndoAction();
                        docSrv.SetActiveDocument(d);
                        editor.BeginUndoAction();
                        rootEditor = editor;
                    }

                    var sci = editor.Control as ScintillaControl;

                    if (sci != null)
                        sci.ReplaceText(r.StartPosition, r.EndPosition, textToReplace);
                    else
                        editor.ReplaceText(r.StartPosition, r.EndPosition, textToReplace);

                    lastSettings.LastStartPosition = r.StartPosition;
                    lastSettings.TextToReplace = textToReplace;
                    lastSettings.LastEndPosition = r.StartPosition + textToReplace.Length;

                    if (items.Contains(item))
                        return false;
                    else
                    {
                        items.Add(item);
                        return true;
                    }
                }, true,
                lastSettings != null ? lastSettings.LastEndPosition : startPos,
                lastSettings != null ? lastSettings.MaxPosition : endPos, null);

                if (!res)
                    break;
            }

            rootEditor.EndUndoAction();
            sw.Stop();
            var s = (Single)sw.ElapsedMilliseconds / 1000;

            var svc = App.GetService<IResultGridService>();
            svc.AddItems(items);
            App.OpenView("ResultGrid");
            App.GetService<IStatusBarService>().SetStatusString(StatusType.Information, "{0} entries replaced. Replace took: {1:F2} s.", items.Count, s);
        }
Exemplo n.º 2
0
        public void SearchAll(string text, Document doc, SearchFlags flags, SearchScope scope)
        {
            lastSettings = null;
            scanned = null;
            var items = new List<ResultItem>();
            var sw = new Stopwatch();
            sw.Start();
            var sm = default(SearchManager);
            var status = "{0} element(s) found. Search took: {1:F2} s.";
            var rootEditor = (ITextEditor)App.GetService<IEditorService>().GetEditor(doc.GetType()).Instance;
            var startPos = scope == SearchScope.Selection ? rootEditor.SelectionStart :
                scope == SearchScope.Current ? rootEditor.CaretPosition : 0;
            var endPos = scope == SearchScope.Selection ? rootEditor.SelectionEnd : 0;

            for(;;)
            {
                var res = Search(text, lastSettings != null ? lastSettings.LastDocument : doc, flags, scope, (d,r) =>
                    {
                        var editor = (ITextEditor)App.GetService<IEditorService>().GetEditor(d.GetType()).Instance;
                        var loc = editor.GetLocationFromPosition(d, r.StartPosition);
                        var txt = editor.GetContent(d, loc.Line).TrimStart(' ', '\t');
                        var item = new ResultItem(txt, d, loc.Line, loc.Column, r.EndPosition - r.StartPosition);

                        if (items.Count >= 1000)
                        {
                            status = "Too many entries found. Showing first {0}. Search took: {1:F2} s.";
                            return false;
                        }
                        else if (items.Contains(item))
                            return false;
                        else
                        {
                            items.Add(item);
                            return true;
                        }
                    }, true,
                    lastSettings != null ? lastSettings.LastEndPosition : 0,
                    lastSettings != null ? lastSettings.MaxPosition : endPos, sm);

                if (!res)
                    break;

                sm = lastSettings.SearchManager;
            }

            sw.Stop();
            var s = (Single)sw.ElapsedMilliseconds / 1000;

            var svc = App.GetService<IResultGridService>();
            svc.AddItems(items);
            App.OpenView("ResultGrid");
            App.GetService<IStatusBarService>().SetStatusString(StatusType.Information, status, items.Count, s);
        }