diff options
author | David Karlaš <david.karlas@xamarin.com> | 2016-01-26 11:05:02 +0300 |
---|---|---|
committer | David Karlaš <david.karlas@xamarin.com> | 2016-01-26 13:33:06 +0300 |
commit | 8c20a653af25f9e297f12d9db76c925960eb0e2d (patch) | |
tree | 901eec05923c0190a1a33c839d308e1649a499d9 /main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/FileSearchCategory.cs | |
parent | 514b5072b45bb16f2845d6999c6711c6645349da (diff) |
[Ide] Global Search Files Category now caches files so it's much faster
Diffstat (limited to 'main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/FileSearchCategory.cs')
-rw-r--r-- | main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/FileSearchCategory.cs | 83 |
1 files changed, 55 insertions, 28 deletions
diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/FileSearchCategory.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/FileSearchCategory.cs index 4eda2000c4..5db3ed8216 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/FileSearchCategory.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/FileSearchCategory.cs @@ -42,32 +42,50 @@ namespace MonoDevelop.Components.MainToolbar { class FileSearchCategory : SearchCategory { - public FileSearchCategory () : base (GettextCatalog.GetString("Files")) + public FileSearchCategory () : base (GettextCatalog.GetString ("Files")) { } - IEnumerable<ProjectFile> AllFiles { - get { - foreach (var doc in IdeApp.Workbench.Documents) { - // We only want to check it here if it's not part - // of the open combine. Otherwise, it will get - // checked down below. - if (doc.Project == null && doc.IsFile) - yield return new ProjectFile (doc.Name); + static FileSearchCategory () + { + IdeApp.Workspace.SolutionLoaded += delegate { allFilesCache = null; }; + IdeApp.Workspace.SolutionUnloaded += delegate { allFilesCache = null; }; + IdeApp.Workspace.ItemAddedToSolution += delegate { allFilesCache = null; }; + IdeApp.Workspace.ItemRemovedFromSolution += delegate { allFilesCache = null; }; + IdeApp.Workspace.FileAddedToProject += delegate { allFilesCache = null; }; + IdeApp.Workspace.FileRemovedFromProject += delegate { allFilesCache = null; }; + IdeApp.Workspace.FileRenamedInProject += delegate { allFilesCache = null; }; + IdeApp.Workbench.DocumentOpened += delegate { allFilesCache = null; }; + IdeApp.Workbench.DocumentClosed += delegate { allFilesCache = null; }; + } + + List<Tuple<string, string, ProjectFile>> GenerateAllFiles () + { + //Slowest thing here is GetRelProjectPath, hence Tuple<,,> needs to be cached + var list = new List<Tuple<string, string, ProjectFile>> (); + foreach (var doc in IdeApp.Workbench.Documents) { + // We only want to check it here if it's not part + // of the open combine. Otherwise, it will get + // checked down below. + if (doc.Project == null && doc.IsFile) { + var pf = new ProjectFile (doc.Name); + list.Add (new Tuple<string, string, ProjectFile> (System.IO.Path.GetFileName (pf.FilePath), FileSearchResult.GetRelProjectPath (pf), pf)); } - - var projects = IdeApp.Workspace.GetAllProjects (); + } + + var projects = IdeApp.Workspace.GetAllProjects (); - foreach (var p in projects) { - foreach (ProjectFile file in p.Files) { - if (file.Subtype != Subtype.Directory && (file.Flags & ProjectItemFlags.Hidden) != ProjectItemFlags.Hidden) - yield return file; + foreach (var p in projects) { + foreach (ProjectFile pf in p.Files) { + if (pf.Subtype != Subtype.Directory && (pf.Flags & ProjectItemFlags.Hidden) != ProjectItemFlags.Hidden) { + list.Add (new Tuple<string, string, ProjectFile> (System.IO.Path.GetFileName (pf.FilePath), FileSearchResult.GetRelProjectPath (pf), pf)); } } } + return list; } - string[] validTags = new [] { "file" , "f" }; + string [] validTags = new [] { "file", "f" }; public override string [] Tags { get { @@ -80,25 +98,34 @@ namespace MonoDevelop.Components.MainToolbar return validTags.Any (t => t == tag); } + static List<Tuple<string, string, ProjectFile>> allFilesCache; + public override Task GetResults (ISearchResultCallback searchResultCallback, SearchPopupSearchPattern pattern, CancellationToken token) { return Task.Run (delegate { - var files = AllFiles.ToList (); + var files = allFilesCache = allFilesCache ?? GenerateAllFiles (); var matcher = StringMatcher.GetMatcher (pattern.Pattern, false); var savedMatches = new Dictionary<string, MatchResult> (); - foreach (ProjectFile file in files) { + foreach (var file in files) { if (token.IsCancellationRequested) break; - int rank; - string matchString = System.IO.Path.GetFileName (file.FilePath); - if (MatchName (savedMatches, matcher, matchString, out rank)) - searchResultCallback.ReportResult (new FileSearchResult (pattern.Pattern, matchString, rank, file, true)); - matchString = FileSearchResult.GetRelProjectPath (file); - if (MatchName (savedMatches, matcher, matchString, out rank)) - searchResultCallback.ReportResult (new FileSearchResult (pattern.Pattern, matchString, rank, file, true)); - + int rank1; + int rank2; + var match1 = MatchName (savedMatches, matcher, file.Item1, out rank1); + var match2 = MatchName (savedMatches, matcher, file.Item2, out rank2); + if (match1 && match2) { + if (rank1 > rank2 || (rank1 == rank2 && String.CompareOrdinal (file.Item1, file.Item2) > 0)) { + searchResultCallback.ReportResult (new FileSearchResult (pattern.Pattern, file.Item1, rank1, file.Item3)); + } else { + searchResultCallback.ReportResult (new FileSearchResult (pattern.Pattern, file.Item2, rank2, file.Item3)); + } + } else if (match1) { + searchResultCallback.ReportResult (new FileSearchResult (pattern.Pattern, file.Item1, rank1, file.Item3)); + } else if (match2) { + searchResultCallback.ReportResult (new FileSearchResult (pattern.Pattern, file.Item2, rank2, file.Item3)); + } } - }, token); + }, token); } static bool MatchName (Dictionary<string, MatchResult> savedMatches, StringMatcher matcher, string name, out int matchRank) @@ -110,7 +137,7 @@ namespace MonoDevelop.Components.MainToolbar MatchResult savedMatch; if (!savedMatches.TryGetValue (name, out savedMatch)) { bool doesMatch = matcher.CalcMatchRank (name, out matchRank); - savedMatches[name] = savedMatch = new MatchResult (doesMatch, matchRank); + savedMatches [name] = savedMatch = new MatchResult (doesMatch, matchRank); } matchRank = savedMatch.Rank; |