Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileSearchCategory.cs « MonoDevelop.Components.MainToolbar « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 76e923f2acd6e801f865e6f50f0c19590be59207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// FileSearchCategory.cs
//
// Author:
//       Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 mkrueger
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Threading;
using System.Threading.Tasks;
using MonoDevelop.Core;
using System.Collections.Generic;
using MonoDevelop.Core.Instrumentation;
using MonoDevelop.Projects;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Ide;
using MonoDevelop.Ide.TypeSystem;
using MonoDevelop.Core.Text;
using Gtk;
using System.Linq;
using Microsoft.CodeAnalysis;

namespace MonoDevelop.Components.MainToolbar
{
	class FileSearchCategory : SearchCategory
	{
		public FileSearchCategory () : base (GettextCatalog.GetString ("Files"))
		{
			sortOrder = HighPriorityResultsOrder;
		}

		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.Owner == 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 ();

			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" };

		public override string [] Tags {
			get {
				return validTags;
			}
		}

		public override bool IsValidTag (string tag)
		{
			return validTags.Any (t => t == tag);
		}

		static List<Tuple<string, string, ProjectFile>> allFilesCache;
		static SemaphoreSlim allFilesLock = new SemaphoreSlim (1, 1);

		public override Task GetResults (ISearchResultCallback searchResultCallback, SearchPopupSearchPattern pattern, CancellationToken token)
		{
			return Task.Run (async delegate {
				List<Tuple<string, string, ProjectFile>> files;
				//This lock is here in case user quickly types 5 letters which triggers 5 threads
				//we don't want to use all CPU doing same thing, instead 1st one will create cache, others will wait here
				//and then all will use cached version...
				bool locked = false;
				try {
					locked = await allFilesLock.WaitAsync (System.Threading.Timeout.Infinite, token).ConfigureAwait (false);
					files = allFilesCache = allFilesCache ?? GenerateAllFiles ();
					if (token.IsCancellationRequested)
						return;
				} finally {
					if (locked)
						allFilesLock.Release ();
				}

				var matcher = StringMatcher.GetMatcher (pattern.Pattern, false);
				var savedMatches = new Dictionary<string, MatchResult> (files.Count * 2);
				foreach (var file in files) {
					if (token.IsCancellationRequested)
						break;
					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);
		}

		static bool MatchName (Dictionary<string, MatchResult> savedMatches, StringMatcher matcher, string name, out int matchRank)
		{
			if (name == null) {
				matchRank = -1;
				return false;
			}
			MatchResult savedMatch;
			if (!savedMatches.TryGetValue (name, out savedMatch)) {
				bool doesMatch = matcher.CalcMatchRank (name, out matchRank);
				savedMatches [name] = savedMatch = new MatchResult (doesMatch, matchRank);
			}

			matchRank = savedMatch.Rank;
			return savedMatch.Match;
		}

	}
}