Пример #1
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;
                    });
                }
            }
        }