Ejemplo n.º 1
0
        public void ShowError(Exception exc, bool scrollToEnd)
        {
            StopAll( );

            lock (this)
            {
                LastText    = null;
                LastMatches = null;
                LastExternalUnderliningSegments = null;
            }

            Dispatcher.BeginInvoke(new Action(() =>
            {
                CancelInfo( );
                ShowOne(rtbError);
                runError.Text = exc.Message.Trim( ) + Environment.NewLine;
                if (scrollToEnd)
                {
                    rtbError.ScrollToEnd( );
                }
                else
                {
                    rtbError.ScrollToHome( );
                }
            }));
        }
Ejemplo n.º 2
0
        public void SetMatches(string text, RegexMatches matches, bool showFirstOnly, bool showSucceededGroupsOnly, bool showCaptures)
        {
            if (matches == null)
            {
                throw new ArgumentNullException(nameof(matches));
            }

            lock (this)
            {
                if (LastMatches != null)
                {
                    var old_groups = LastMatches.Matches.SelectMany(m => m.Groups).Select(g => (g.Index, g.Length, g.Value, g.Name));
                    var new_groups = matches.Matches.SelectMany(m => m.Groups).Select(g => (g.Index, g.Length, g.Value, g.Name));

                    var old_captures = LastMatches.Matches.SelectMany(m => m.Groups).SelectMany(g => g.Captures).Select(c => c.Value);
                    var new_captures = matches.Matches.SelectMany(m => m.Groups).SelectMany(g => g.Captures).Select(c => c.Value);

                    if (rtbMatches.IsVisible &&
                        showFirstOnly == LastShowFirstOnly &&
                        showSucceededGroupsOnly == LastShowSucceededGroupsOnly &&
                        showCaptures == LastShowCaptures &&
                        new_groups.SequenceEqual(old_groups) &&
                        new_captures.SequenceEqual(old_captures))
                    {
                        CancelInfo( );

                        LastText    = text;
                        LastMatches = matches;
                        LastExternalUnderliningSegments = null;

                        return;
                    }
                }
            }

            ShowMatchesLoop.SendRewind( );
            LocalUnderliningLoop.SendRewind( );
            ExternalUnderliningLoop.SendRewind( );
            LocalUnderliningAdorner.SetRangesToUnderline(null);               //?
            ExternalUnderliningAdorner.SetRangesToUnderline(null);            //?

            lock (this)
            {
                LastText                        = text;
                LastMatches                     = matches;
                LastShowCaptures                = showCaptures;
                LastShowFirstOnly               = showFirstOnly;
                LastShowSucceededGroupsOnly     = showSucceededGroupsOnly;
                LastExternalUnderliningSegments = null;
            }

            ShowMatchesLoop.SendWaitAndExecute( );
            LocalUnderliningLoop.SendWaitAndExecute( );
            ExternalUnderliningLoop.SendWaitAndExecute( );
        }
Ejemplo n.º 3
0
        public void ShowNoPattern( )
        {
            StopAll( );

            lock (this)
            {
                LastText    = null;
                LastMatches = null;
                LastExternalUnderliningSegments = null;
            }

            Dispatcher.BeginInvoke(new Action(() =>
            {
                CancelInfo( );
                ShowOne(rtbNoPattern);
            }));
        }
Ejemplo n.º 4
0
        public void SetMatches(RegexMatches matches, bool showCaptures, string eol)
        {
            if (matches == null)
            {
                throw new ArgumentNullException(nameof(matches));
            }

            string       last_text;
            RegexMatches last_matches;
            bool         last_show_captures;
            string       last_eol;

            lock (this)
            {
                last_text          = LastText;
                last_matches       = LastMatches;
                last_show_captures = LastShowCaptures;
                last_eol           = LastEol;
            }

            string text = GetBaseTextData(eol).Text;

            if (last_matches != null)
            {
                var old_groups = last_matches.Matches.SelectMany(m => m.Groups).Select(g => (g.Index, g.Length, g.Value));
                var new_groups = matches.Matches.SelectMany(m => m.Groups).Select(g => (g.Index, g.Length, g.Value));

                if (string.Equals(text, last_text) &&
                    showCaptures == last_show_captures &&
                    eol == last_eol &&
                    new_groups.SequenceEqual(old_groups))
                {
                    lock (this)
                    {
                        LastMatches = matches;
                        LastExternalUnderlineInfo = null;
                    }

                    MatchesUpdatedEvent.Set( );

                    return;
                }
            }

            RecolouringLoop.SendRewind( );
            LocalUnderliningLoop.SendRewind( );
            ExternalUnderliningLoop.SendRewind( );

            lock (this)
            {
                LastText                  = text;
                LastMatches               = matches;
                LastShowCaptures          = showCaptures;
                LastEol                   = eol;
                LastExternalUnderlineInfo = null;
            }

            MatchesUpdatedEvent.Set( );

            RecolouringLoop.SendWaitAndExecute( );
            LocalUnderliningLoop.SendWaitAndExecute( );
            ExternalUnderliningLoop.SendWaitAndExecute( );
        }
Ejemplo n.º 5
0
        void FindMatchesThreadProc(ICancellable cnc)
        {
            string       eol        = null;
            string       pattern    = null;
            string       text       = null;
            bool         first_only = false;
            IRegexEngine engine     = null;

            UITaskHelper.Invoke(this,
                                () =>
            {
                eol     = GetEolOption( );
                pattern = ucPattern.GetBaseTextData(eol).Text;
                if (cnc.IsCancellationRequested)
                {
                    return;
                }
                text = ucText.GetBaseTextData(eol).Text;
                if (cnc.IsCancellationRequested)
                {
                    return;
                }
                first_only = cbShowFirstOnly.IsChecked == true;
                engine     = CurrentRegexEngine;
            });

            if (cnc.IsCancellationRequested)
            {
                return;
            }

            if (string.IsNullOrEmpty(pattern))
            {
                UITaskHelper.BeginInvoke(this,
                                         () =>
                {
                    ucText.SetMatches(RegexMatches.Empty, cbShowCaptures.IsChecked == true, GetEolOption( ));
                    ucMatches.ShowNoPattern( );
                    lblMatches.Text         = "Matches";
                    pnlShowAll.Visibility   = Visibility.Collapsed;
                    pnlShowFirst.Visibility = Visibility.Collapsed;
                });
            }
            else
            {
                IMatcher     parsed_pattern = null;
                RegexMatches matches        = null;
                bool         is_good        = false;

                try
                {
                    UITaskHelper.BeginInvoke(this, CancellationToken.None, () => ucMatches.ShowMatchingInProgress(true));

                    parsed_pattern = engine.ParsePattern(pattern);
                    var indeterminate_progress_thread = new Thread(IndeterminateProgressThreadProc)
                    {
                        IsBackground = true
                    };
                    try
                    {
                        indeterminate_progress_thread.Start( );

                        matches = parsed_pattern.Matches(text, cnc);
                    }
                    finally
                    {
                        HideIndeterminateProgress(indeterminate_progress_thread);
                    }

                    is_good = true;
                }
                catch (Exception exc)
                {
                    if (cnc.IsCancellationRequested)
                    {
                        return;
                    }

                    UITaskHelper.BeginInvoke(this, CancellationToken.None,
                                             () =>
                    {
                        ucText.SetMatches(RegexMatches.Empty, cbShowCaptures.IsChecked == true, GetEolOption( ));
                        ucMatches.ShowError(exc, engine.Capabilities.HasFlag(RegexEngineCapabilityEnum.ScrollErrorsToEnd));
                        lblMatches.Text         = "Error";
                        pnlShowAll.Visibility   = Visibility.Collapsed;
                        pnlShowFirst.Visibility = Visibility.Collapsed;
                    });

                    Debug.Assert(!is_good);
                }
                finally
                {
                    UITaskHelper.BeginInvoke(this, CancellationToken.None, () => ucMatches.ShowMatchingInProgress(false));
                }

                if (cnc.IsCancellationRequested)
                {
                    return;
                }

                if (is_good)
                {
                    int count = matches.Count;

                    var matches_to_show = first_only ?
                                          new RegexMatches(Math.Min(1, count), matches.Matches.Take(1)) :
                                          matches;

                    if (cnc.IsCancellationRequested)
                    {
                        return;
                    }

                    UITaskHelper.BeginInvoke(this,
                                             () =>
                    {
                        ucText.SetMatches(matches_to_show, cbShowCaptures.IsChecked == true, GetEolOption( ));
                        ucMatches.SetMatches(text, matches_to_show, first_only, cbShowSucceededGroupsOnly.IsChecked == true, cbShowCaptures.IsChecked == true);

                        lblMatches.Text         = count == 0 ? "Matches" : count == 1 ? "1 match" : $"{count:#,##0} matches";
                        pnlShowAll.Visibility   = first_only && count > 1 ? Visibility.Visible : Visibility.Collapsed;
                        pnlShowFirst.Visibility = !first_only && count > 1 ? Visibility.Visible : Visibility.Collapsed;
                    });
                }
            }
        }