From 9d0834ac82b66b643f6a548aed1482c87ee251c5 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 23 Oct 2011 16:13:51 +0200 Subject: [PATCH] optimized IHighlightedLineBuilder creation --- .../Project/Engine/SearchManager.cs | 11 +++++++++-- .../Src/Editor/Search/SearchResultsPad.cs | 6 +----- .../FindReferencesAndRenameHelper.cs | 19 +++++++++++++++++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs index ce0169d249..8532c7c6c8 100644 --- a/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs +++ b/src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs @@ -92,6 +92,7 @@ namespace SearchAndReplace IEnumerable fileList; IProgressMonitor monitor; int count; + double oneStep; CancellationTokenSource cts; public bool UseParallel { get; set; } @@ -117,6 +118,7 @@ namespace SearchAndReplace delegate { var list = fileList.ToList(); this.count = list.Count; + this.oneStep = 1.0 / this.count; monitor.CancellationToken.ThrowIfCancellationRequested(); cts.Token.ThrowIfCancellationRequested(); Parallel.ForEach(list, new ParallelOptions { CancellationToken = monitor.CancellationToken, MaxDegreeOfParallelism = Environment.ProcessorCount }, fileName => SearchFile(fileName, strategy, monitor.CancellationToken)); @@ -126,6 +128,7 @@ namespace SearchAndReplace } else { var list = fileList.ToList(); this.count = list.Count; + this.oneStep = 1.0 / this.count; monitor.CancellationToken.ThrowIfCancellationRequested(); cts.Token.ThrowIfCancellationRequested(); foreach (var file in list) { @@ -166,14 +169,18 @@ namespace SearchAndReplace 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); + var match = new SearchResultMatch(fileName, start, end, result.Offset, result.Length, builder); lock (observer) - observer.OnNext(new SearchResultMatch(fileName, start, end, result.Offset, result.Length, SearchResultsPad.CreateInlineBuilder(start, end, document, Path.GetExtension(fileName)))); + observer.OnNext(match); } lock (monitor) - monitor.Progress += 1.0 / count; + monitor.Progress += oneStep; } } diff --git a/src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs b/src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs index efd7e85df7..6f7c45024f 100644 --- a/src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs +++ b/src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs @@ -147,15 +147,11 @@ namespace ICSharpCode.SharpDevelop.Editor.Search return new DummySearchResult { Text = title }; } - public static HighlightedInlineBuilder CreateInlineBuilder(Location startPosition, Location endPosition, TextDocument document, string fileExtension) + public static HighlightedInlineBuilder CreateInlineBuilder(Location startPosition, Location endPosition, TextDocument document, IHighlighter highlighter) { if (startPosition.Line >= 1 && startPosition.Line <= document.LineCount) { var matchedLine = document.GetLineByNumber(startPosition.Line); HighlightedInlineBuilder inlineBuilder = new HighlightedInlineBuilder(document.GetText(matchedLine)); - var def = HighlightingManager.Instance.GetDefinitionByExtension(fileExtension); - if (def == null) - return inlineBuilder; - var highlighter = new DocumentHighlighter(document, def.MainRuleSet); if (highlighter != null) { HighlightedLine highlightedLine = highlighter.HighlightLine(startPosition.Line); int startOffset = highlightedLine.DocumentLine.Offset; diff --git a/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs b/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs index 7510099272..1df6f929f5 100644 --- a/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs +++ b/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs @@ -4,8 +4,10 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using ICSharpCode.AvalonEdit.Document; +using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.Core; using ICSharpCode.NRefactory; using ICSharpCode.SharpDevelop.Dom; @@ -372,11 +374,24 @@ namespace ICSharpCode.SharpDevelop.Refactoring { if (list == null) return; List results = new List(list.Count); + TextDocument document = null; + FileName fileName = null; + IHighlighter highlighter = null; foreach (Reference r in list) { - var document = new TextDocument(DocumentUtilitites.GetTextSource(ParserService.GetParseableFileContent(r.FileName))); + var f = new FileName(r.FileName); + if (document == null || !f.Equals(fileName)) { + document = new TextDocument(DocumentUtilitites.GetTextSource(ParserService.GetParseableFileContent(r.FileName))); + fileName = new FileName(r.FileName); + var def = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(r.FileName)); + if (def != null) + highlighter = new DocumentHighlighter(document, def.MainRuleSet); + else + highlighter = null; + } var start = document.GetLocation(r.Offset).ToLocation(); var end = document.GetLocation(r.Offset + r.Length).ToLocation(); - SearchResultMatch res = new SearchResultMatch(new FileName(r.FileName), start, end, r.Offset, r.Length, SearchResultsPad.CreateInlineBuilder(start, end, document, Path.GetExtension(r.FileName))); + var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter); + SearchResultMatch res = new SearchResultMatch(fileName, start, end, r.Offset, r.Length, builder); results.Add(res); } SearchResultsPad.Instance.ShowSearchResults(title, results);