Exemple #1
0
        public void Load(bool isAsync)
        {
            if (IsLoaded || IsLoading)
            {
                return;
            }

            IsLoading = true;
            if (!isAsync)
            {
                int currentLine = -1;
                List <GrepSearchResult.GrepLine> linesWithContext = new List <GrepSearchResult.GrepLine>();
                if (GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext))
                {
                    linesWithContext = result.GetLinesWithContext(GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesBefore),
                                                                  GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesAfter));
                }
                else
                {
                    linesWithContext = result.GetLinesWithContext(0, 0);
                }

                if (this.Count == 1 && this[0].GrepLine.LineNumber == -1)
                {
                    this.Clear();
                }

                for (int i = 0; i < linesWithContext.Count; i++)
                {
                    GrepSearchResult.GrepLine line = linesWithContext[i];
                    bool isSectionBreak            = false;

                    // Adding separator
                    if (this.Count > 0 && GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext) &&
                        (currentLine != line.LineNumber && currentLine + 1 != line.LineNumber))
                    {
                        isSectionBreak = true;
                    }

                    currentLine = line.LineNumber;
                    if (currentLine <= 999 && LineNumberColumnWidth < 30)
                    {
                        LineNumberColumnWidth = 30;
                    }
                    else if (currentLine > 999 && LineNumberColumnWidth < 35)
                    {
                        LineNumberColumnWidth = 35;
                    }
                    else if (currentLine > 9999 && LineNumberColumnWidth < 47)
                    {
                        LineNumberColumnWidth = 47;
                    }
                    else if (currentLine > 99999 && LineNumberColumnWidth < 50)
                    {
                        LineNumberColumnWidth = 50;
                    }

                    this.Add(new FormattedGrepLine(line, formattedResult, LineNumberColumnWidth, isSectionBreak));
                }
                IsLoaded  = true;
                IsLoading = false;
            }
            else
            {
                int currentLine = -1;
                var asyncTask   = Task.Factory.StartNew <List <GrepSearchResult.GrepLine> >(() =>
                {
                    List <GrepSearchResult.GrepLine> linesWithContext = new List <GrepSearchResult.GrepLine>();
                    if (GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext))
                    {
                        linesWithContext = result.GetLinesWithContext(GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesBefore),
                                                                      GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesAfter));
                    }
                    else
                    {
                        linesWithContext = result.GetLinesWithContext(0, 0);
                    }

                    return(linesWithContext);
                }).ContinueWith(task =>
                {
                    if (this.Count == 1 && this[0].GrepLine.LineNumber == -1)
                    {
                        if (Application.Current != null)
                        {
                            Application.Current.Dispatcher.Invoke(new Action(() =>
                                                                             this.Clear()
                                                                             ));
                        }
                    }

                    List <GrepSearchResult.GrepLine> linesWithContext = task.Result;
                    List <FormattedGrepLine> tempList = new List <FormattedGrepLine>();
                    for (int i = 0; i < linesWithContext.Count; i++)
                    {
                        GrepSearchResult.GrepLine line = linesWithContext[i];
                        bool isSectionBreak            = false;

                        // Adding separator
                        if (tempList.Count > 0 && GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext) &&
                            (currentLine != line.LineNumber && currentLine + 1 != line.LineNumber))
                        {
                            isSectionBreak = true;
                        }

                        currentLine = line.LineNumber;
                        if (currentLine <= 999 && LineNumberColumnWidth < 30)
                        {
                            LineNumberColumnWidth = 30;
                        }
                        else if (currentLine > 999 && LineNumberColumnWidth < 35)
                        {
                            LineNumberColumnWidth = 35;
                        }
                        else if (currentLine > 9999 && LineNumberColumnWidth < 47)
                        {
                            LineNumberColumnWidth = 47;
                        }
                        else if (currentLine > 99999 && LineNumberColumnWidth < 50)
                        {
                            LineNumberColumnWidth = 50;
                        }
                        tempList.Add(new FormattedGrepLine(line, formattedResult, LineNumberColumnWidth, isSectionBreak));
                    }

                    if (Application.Current != null)
                    {
                        Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            foreach (var l in tempList)
                            {
                                this.Add(l);
                            }
                        }
                                                                         ));
                    }
                    IsLoaded  = true;
                    IsLoading = false;
                    LoadFinished(this, EventArgs.Empty);
                });
            }
        }