Exemple #1
0
            SearchedFile SearchFile(FileName fileName)
            {
                ITextBuffer buffer = fileFinder.Create(fileName);

                if (buffer == null)
                {
                    return(null);
                }

                ThrowIfCancellationRequested();

                var                 source      = DocumentUtilitites.GetTextSource(buffer);
                TextDocument        document    = null;
                DocumentHighlighter highlighter = null;
                int                 offset      = 0;
                int                 length      = source.TextLength;

                if (Target == SearchTarget.CurrentSelection && Selection != null)
                {
                    offset = Selection.Offset;
                    length = Selection.Length;
                }
                List <SearchResultMatch> results = new List <SearchResultMatch>();

                foreach (var result in strategy.FindAll(source, offset, length))
                {
                    ThrowIfCancellationRequested();
                    if (document == null)
                    {
                        document = new TextDocument(source);
                        var highlighting = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(fileName));
                        if (highlighting != null)
                        {
                            highlighter = new DocumentHighlighter(document, highlighting.MainRuleSet);
                        }
                        else
                        {
                            highlighter = null;
                        }
                    }
                    var start   = document.GetLocation(result.Offset).ToLocation();
                    var end     = document.GetLocation(result.Offset + result.Length).ToLocation();
                    var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter);
                    results.Add(new AvalonEditSearchResultMatch(fileName, start, end, result.Offset, result.Length, builder, result));
                }
                if (results.Count > 0)
                {
                    return(new SearchedFile(fileName, results));
                }
                else
                {
                    return(null);
                }
            }
Exemple #2
0
        /// <summary>
        /// Runs search process
        /// </summary>
        /// <param name="document"></param>
        /// <param name="context"></param>
        /// <param name="changeSelection"></param>
        /// <param name="offset"></param>
        void RunSearch(string document, SearchContext context, bool changeSelection, int offset)
        {
            var searchResults = strategy.FindAll(document, 0, document.Length, context);

            if (searchResults == null)
            {
                return;
            }
            // We cast from ISearchResult to SearchResult; this is safe because we always use the built-in strategy
            for (int index = 0; index < searchResults.Count; index++)
            {
                if (context.IsCanceled)
                {
                    return;
                }
                var result = (SearchResult)searchResults[index];
                if (changeSelection && result.StartOffset >= offset)
                {
                    RunOnDispatcher(() =>
                    {
                        if (!context.IsCanceled)
                        {
                            SelectResult(result);
                        }
                    });
                    changeSelection = false;
                }
                if (context.IsCanceled)
                {
                    return;
                }
                renderer.CurrentResults.Add(result);
            }
            if (!context.IsCanceled)
            {
                RunOnDispatcher(() =>
                {
                    if (!renderer.CurrentResults.Any())
                    {
                        messageView.IsOpen          = true;
                        messageView.Content         = Localization.NoMatchesFoundText;
                        messageView.PlacementTarget = searchTextBox;
                    }
                    else
                    {
                        messageView.IsOpen = false;
                    }
                    textArea.TextView.InvalidateLayer(KnownLayer.Selection);
                });
            }
        }
            SearchedFile SearchFile(FileName fileName)
            {
                ITextSource source = fileFinder.Create(fileName);

                if (source == null)
                {
                    return(null);
                }

                ThrowIfCancellationRequested();
                ReadOnlyDocument document    = null;
                IHighlighter     highlighter = null;
                int offset = 0;
                int length = source.TextLength;

                if (Target == SearchTarget.CurrentSelection && Selection != null)
                {
                    offset = Selection.Offset;
                    length = Selection.Length;
                }
                List <SearchResultMatch> results = new List <SearchResultMatch>();

                foreach (var result in strategy.FindAll(source, offset, length))
                {
                    ThrowIfCancellationRequested();
                    if (document == null)
                    {
                        document    = new ReadOnlyDocument(source, fileName);
                        highlighter = SD.EditorControlService.CreateHighlighter(document);
                        highlighter.BeginHighlighting();
                    }
                    var start   = document.GetLocation(result.Offset);
                    var end     = document.GetLocation(result.Offset + result.Length);
                    var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter);
                    results.Add(new AvalonEditSearchResultMatch(fileName, start, end, result.Offset, result.Length, builder, highlighter.DefaultTextColor, result));
                }
                if (highlighter != null)
                {
                    highlighter.Dispose();
                }
                if (results.Count > 0)
                {
                    return(new SearchedFile(fileName, results));
                }
                else
                {
                    return(null);
                }
            }
Exemple #4
0
        private void GetSearchResult(ref Dictionary <int, string> privateObject, ISearchStrategy searchStrategy)
        {
            foreach (ISearchResult result in searchStrategy.FindAll(document, 0, document.TextLength))
            {
                ISegment     segment    = result as ISegment;
                DocumentLine docLine    = document.GetLineByOffset(segment.Offset);
                int          lineNumber = docLine.LineNumber;
                string       lineText   = document.GetText(docLine.Offset, docLine.Length);

                if (!privateObject.ContainsKey(lineNumber))
                {
                    privateObject.Add(lineNumber, lineText);
                }
            }
        }
Exemple #5
0
        private void handleTypingTimerTimeout(object sender, EventArgs e)
        {
            var timer = sender as DispatcherTimer;             // WPF

            if (timer == null)
            {
                return;
            }

            var changeSelection = lastChangeSelection;

            renderer.CurrentResults.Clear();

            if (!string.IsNullOrEmpty(SearchPattern))
            {
                int offset = textArea.Caret.Offset;
                if (changeSelection)
                {
                    textArea.ClearSelection();
                }
                // We cast from ISearchResult to SearchResult; this is safe because we always use the built-in strategy
                foreach (SearchResult result in strategy.FindAll(textArea.Document, 0, textArea.Document.TextLength))
                {
                    if (changeSelection && result.StartOffset >= offset)
                    {
                        SelectResult(result);
                        changeSelection = false;
                    }
                    renderer.CurrentResults.Add(result);
                }
                if (!renderer.CurrentResults.Any())
                {
                    messageView.IsOpen          = true;
                    messageView.Content         = Localization.NoMatchesFoundText;
                    messageView.PlacementTarget = searchTextBox;
                }
                else
                {
                    messageView.IsOpen = false;
                }
            }
            textArea.TextView.InvalidateLayer(KnownLayer.Selection);

            timer.Stop();
        }
Exemple #6
0
        /// <summary>
        /// Causes the background renderer to draw.
        /// </summary>
        /// <param name="textView"></param>
        /// <param name="drawingContext"></param>
        public void Draw(TextView textView, DrawingContext drawingContext)
        {
            if (String.IsNullOrWhiteSpace(editor.SelectedText) ||
                !editor.SelectedText.All(Char.IsLetterOrDigit))
            {
                return;
            }
            ISearchStrategy strategy = SearchStrategyFactory.Create(editor.SelectedText, false, true, SearchMode.Normal);

            foreach (ISearchResult result in strategy.FindAll(textView.Document, 0, textView.Document.TextLength))
            {
                BackgroundGeometryBuilder builder = new BackgroundGeometryBuilder()
                {
                    CornerRadius = 1
                };
                builder.AddSegment(textView, result);
                drawingContext.DrawGeometry(BrushStyle, PenStyle, builder.CreateGeometry());
            }
        }
Exemple #7
0
        private IEnumerable <FindResult> GetResults(ISearchStrategy strategy, ISourceFile file)
        {
            using (var fileStream = file.OpenText())
            {
                using (var reader = new StreamReader(fileStream))
                {
                    var document = new TextDocument(reader.ReadToEnd());

                    var results = strategy.FindAll(document, 0, document.TextLength);

                    foreach (var result in results.GroupBy(sr => document.GetLineByOffset(sr.Offset).LineNumber).Select(group => group.First()))
                    {
                        var line = document.GetLineByOffset(result.Offset);

                        yield return(new FindResult(file, result.Offset, result.Length, line.LineNumber, line.Offset, document.GetText(line)));
                    }
                }
            }
        }
        private void DoSearch(bool changeSelection)
        {
            if (IsClosed)
            {
                return;
            }

            _renderer.CurrentResults.Clear();

            if (!string.IsNullOrEmpty(SearchPattern))
            {
                var offset = _textArea.Caret.Offset;
                if (changeSelection)
                {
                    _textArea.ClearSelection();
                }

                // We cast from ISearchResult to SearchResult; this is safe because we always use the built-in strategy
                foreach (var result in _strategy.FindAll(_textArea.Document, 0, _textArea.Document.TextLength).OfType <TextSegment>())
                {
                    if (changeSelection && result.StartOffset >= offset)
                    {
                        SelectResult(result);
                        changeSelection = false;
                    }
                    _renderer.CurrentResults.Add(result);
                }

                if (_renderer.CurrentResults.Count == 0)
                {
                    _messageView.IsOpen          = true;
                    _messageView.Content         = Localization.NoMatchesFoundText;
                    _messageView.PlacementTarget = _searchTextBox;
                }
                else
                {
                    _messageView.IsOpen = false;
                }
            }
            _textArea.TextView.InvalidateLayer(KnownLayer.Selection);
        }
Exemple #9
0
        private void DoSearch(bool changeSelection)
        {
            if (this.IsDisposed)
            {
                return;
            }

            _renderer.CurrentResults.Clear();
            if (!string.IsNullOrEmpty(txtFind.Text))
            {
                var offset = editor.Editor.TextArea.Caret.Offset;
                if (changeSelection)
                {
                    editor.Editor.TextArea.ClearSelection();
                }

                foreach (var searchResult in _strategy.FindAll(_currentDoc, 0
                                                               , _currentDoc.TextLength).OfType <TextSegment>())
                {
                    if (changeSelection && searchResult.StartOffset >= offset)
                    {
                        this.SelectResult(searchResult);
                        changeSelection = false;
                    }
                    _renderer.CurrentResults.Add(searchResult);
                }
                //if (!_renderer.CurrentResults.Any<SearchResult>())
                //{
                //  this.messageView.IsOpen = true;
                //  this.messageView.Content = this.Localization.NoMatchesFoundText;
                //  this.messageView.PlacementTarget = this.searchTextBox;
                //}
                //else
                //{
                //  this.messageView.IsOpen = false;
                //}
            }
            editor.Editor.TextArea.TextView.InvalidateLayer(KnownLayer.Selection);
        }
Exemple #10
0
        void DoSearch(bool changeSelection)
        {
            if (IsClosed || (strategy == null))
            {
                return;
            }
            renderer.CurrentResults.Clear();

            if (!string.IsNullOrEmpty(SearchPattern))
            {
                int offset = textArea.Caret.Offset;
                if (changeSelection)
                {
                    textArea.ClearSelection();
                }
                // We cast from ISearchResult to SearchResult; this is safe because we always use the built-in strategy
                foreach (SearchResult result in strategy.FindAll(textArea.Document, 0, textArea.Document.TextLength))
                {
                    if (changeSelection && result.StartOffset >= offset)
                    {
                        SelectResult(result);
                        changeSelection = false;
                    }
                    renderer.CurrentResults.Add(result);
                }
                if (!renderer.CurrentResults.Any())
                {
                    messageView.IsOpen          = true;
                    messageView.Content         = Localization.NoMatchesFoundText;
                    messageView.PlacementTarget = searchPanel;
                }
                else
                {
                    messageView.IsOpen = false;
                }
            }
            textArea.TextView.InvalidateLayer(KnownLayer.Selection);
        }
Exemple #11
0
        void DoSearch(bool changeSelection)
        {
            renderer.CurrentResults.Clear();
            currentResult = null;

            if (!string.IsNullOrEmpty(SearchPattern))
            {
                int offset = textArea.Caret.Offset;
                if (changeSelection)
                {
                    textArea.ClearSelection();
                }
                foreach (SearchResult result in strategy.FindAll(textArea.Document, 0, textArea.Document.TextLength))
                {
                    if (currentResult == null && result.StartOffset >= offset)
                    {
                        currentResult = result;
                        if (changeSelection)
                        {
                            SetResult(result);
                        }
                    }
                    renderer.CurrentResults.Add(result);
                }
                if (!renderer.CurrentResults.Any())
                {
                    messageView.IsOpen          = true;
                    messageView.Content         = Localization.NoMatchesFoundText;
                    messageView.PlacementTarget = searchTextBox;
                }
                else
                {
                    messageView.IsOpen = false;
                }
            }
            textArea.TextView.InvalidateLayer(KnownLayer.Selection);
        }
        public void DoSearch(bool changeSelection)
        {
            renderer.CurrentResults.Clear();

            if (!string.IsNullOrEmpty(SearchPattern))
            {
                int offset = textArea.Caret.Offset;
                if (changeSelection)
                {
                    textArea.ClearSelection();
                }
                // We cast from ISearchResult to SearchResult; this is safe because we always use the built-in strategy
                foreach (SearchResult result in strategy.FindAll(textArea.Document, 0, textArea.Document.TextLength))
                {
                    if (changeSelection && result.StartOffset >= offset)
                    {
                        SelectResult(result);
                        changeSelection = false;
                    }
                    renderer.CurrentResults.Add(result);
                }
            }
            textArea.TextView.InvalidateLayer(KnownLayer.Selection);
        }