~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/ProjectSearchCategory.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ProjectSearchCategory.cs
 
3
//  
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Threading;
 
28
using System.Threading.Tasks;
 
29
using MonoDevelop.Core;
 
30
using System.Collections.Generic;
 
31
using MonoDevelop.Core.Instrumentation;
 
32
using MonoDevelop.Projects;
 
33
using MonoDevelop.Ide.Gui;
 
34
using MonoDevelop.Ide;
 
35
using ICSharpCode.NRefactory.TypeSystem;
 
36
using MonoDevelop.Ide.TypeSystem;
 
37
using MonoDevelop.Core.Text;
 
38
using Gtk;
 
39
using System.Linq;
 
40
 
 
41
namespace MonoDevelop.Components.MainToolbar
 
42
{
 
43
        class ProjectSearchCategory : SearchCategory
 
44
        {
 
45
                SearchPopupWindow widget;
 
46
 
 
47
                public ProjectSearchCategory (SearchPopupWindow widget) : base (GettextCatalog.GetString("Solution"))
 
48
                {
 
49
                        this.widget = widget;
 
50
                        this.lastResult = new WorkerResult (widget);
 
51
                }
 
52
 
 
53
                static TimerCounter getMembersTimer = InstrumentationService.CreateTimerCounter ("Time to get all members", "NavigateToDialog");
 
54
 
 
55
 
 
56
                static TimerCounter getTypesTimer = InstrumentationService.CreateTimerCounter ("Time to get all types", "NavigateToDialog");
 
57
 
 
58
                IEnumerable<ITypeDefinition> types {
 
59
                        get {
 
60
                                getTypesTimer.BeginTiming ();
 
61
                                try {
 
62
                                        foreach (Document doc in IdeApp.Workbench.Documents) {
 
63
                                                // We only want to check it here if it's not part
 
64
                                                // of the open combine. Otherwise, it will get
 
65
                                                // checked down below.
 
66
                                                if (doc.Project == null && doc.IsFile) {
 
67
                                                        var info = doc.ParsedDocument;
 
68
                                                        if (info != null) {
 
69
                                                                var ctx = doc.Compilation;
 
70
                                                                foreach (var type in ctx.MainAssembly.GetAllTypeDefinitions ()) {
 
71
                                                                        yield return type;
 
72
                                                                }
 
73
                                                        }
 
74
                                                }
 
75
                                        }
 
76
                                        
 
77
                                        var projects = IdeApp.Workspace.GetAllProjects ();
 
78
                                        
 
79
                                        foreach (Project p in projects) {
 
80
                                                var pctx = TypeSystemService.GetCompilation (p);
 
81
                                                foreach (var type in pctx.MainAssembly.GetAllTypeDefinitions ())
 
82
                                                        yield return type;
 
83
                                        }
 
84
                                } finally {
 
85
                                        getTypesTimer.EndTiming ();
 
86
                                }
 
87
                        }
 
88
                }
 
89
 
 
90
                WorkerResult lastResult;
 
91
                string[] typeTags = new [] { "type", "c", "s", "i", "e", "d"};
 
92
                string[] memberTags = new [] { "member", "m", "p", "f", "evt"};
 
93
 
 
94
                public override bool IsValidTag (string tag)
 
95
                {
 
96
                        return typeTags.Any (t => t == tag) || memberTags.Any (t => t == tag);
 
97
                }
 
98
 
 
99
                public override Task<ISearchDataSource> GetResults (SearchPopupSearchPattern searchPattern, int resultsCount, CancellationToken token)
 
100
                {
 
101
                        return Task.Factory.StartNew (delegate {
 
102
                                if (searchPattern.Tag != null && !(typeTags.Contains (searchPattern.Tag) || memberTags.Contains (searchPattern.Tag)) || searchPattern.HasLineNumber)
 
103
                                        return null;
 
104
                                try {
 
105
                                        var newResult = new WorkerResult (widget);
 
106
                                        newResult.pattern = searchPattern.Pattern;
 
107
                                        newResult.IncludeFiles = true;
 
108
                                        newResult.Tag = searchPattern.Tag;
 
109
                                        newResult.IncludeTypes = searchPattern.Tag == null || typeTags.Contains (searchPattern.Tag) ;
 
110
                                        newResult.IncludeMembers = searchPattern.Tag == null || memberTags.Contains (searchPattern.Tag);
 
111
                                        var firstType = types.FirstOrDefault ();
 
112
                                        newResult.ambience = firstType != null ? AmbienceService.GetAmbienceForFile (firstType.Region.FileName) : AmbienceService.DefaultAmbience;
 
113
                                        
 
114
                                        string toMatch = searchPattern.Pattern;
 
115
                                        newResult.matcher = StringMatcher.GetMatcher (toMatch, false);
 
116
                                        newResult.FullSearch = toMatch.IndexOf ('.') > 0;
 
117
                                        var oldLastResult = lastResult;
 
118
                                        if (newResult.FullSearch && oldLastResult != null && !oldLastResult.FullSearch)
 
119
                                                oldLastResult = new WorkerResult (widget);
 
120
//                                      var now = DateTime.Now;
 
121
 
 
122
                                        AllResults (oldLastResult, newResult, token);
 
123
                                        newResult.results.SortUpToN (new DataItemComparer (token), resultsCount);
 
124
                                        lastResult = newResult;
 
125
//                                      Console.WriteLine ((now - DateTime.Now).TotalMilliseconds);
 
126
                                        return (ISearchDataSource)newResult.results;
 
127
                                } catch {
 
128
                                        token.ThrowIfCancellationRequested ();
 
129
                                        throw;
 
130
                                }
 
131
                        }, token);
 
132
                }
 
133
 
 
134
                void AllResults (WorkerResult lastResult, WorkerResult newResult, CancellationToken token)
 
135
                {
 
136
                        if (newResult.isGotoFilePattern)
 
137
                                return;
 
138
                        uint x = 0;
 
139
                        // Search Types
 
140
                        if (newResult.IncludeTypes && (newResult.Tag == null || typeTags.Any (t => t == newResult.Tag))) {
 
141
                                newResult.filteredTypes = new List<ITypeDefinition> ();
 
142
                                bool startsWithLastFilter = lastResult.pattern != null && newResult.pattern.StartsWith (lastResult.pattern, StringComparison.Ordinal) && lastResult.filteredTypes != null;
 
143
                                var allTypes = startsWithLastFilter ? lastResult.filteredTypes : types;
 
144
                                foreach (var type in allTypes) {
 
145
                                        if (unchecked(x++) % 100 == 0 && token.IsCancellationRequested)
 
146
                                                return;
 
147
 
 
148
                                        if (newResult.Tag != null) {
 
149
                                                if (newResult.Tag == "c" && type.Kind != TypeKind.Class)
 
150
                                                        continue;
 
151
                                                if (newResult.Tag == "s" && type.Kind != TypeKind.Struct)
 
152
                                                        continue;
 
153
                                                if (newResult.Tag == "i" && type.Kind != TypeKind.Interface)
 
154
                                                        continue;
 
155
                                                if (newResult.Tag == "e" && type.Kind != TypeKind.Enum)
 
156
                                                        continue;
 
157
                                                if (newResult.Tag == "d" && type.Kind != TypeKind.Delegate)
 
158
                                                        continue;
 
159
                                        }
 
160
                                        SearchResult curResult = newResult.CheckType (type);
 
161
                                        if (curResult != null) {
 
162
                                                newResult.filteredTypes.Add (type);
 
163
                                                newResult.results.AddResult (curResult);
 
164
                                        }
 
165
                                }
 
166
                        }
 
167
                        
 
168
                        // Search members
 
169
                        if (newResult.IncludeMembers && (newResult.Tag == null || memberTags.Any (t => t == newResult.Tag))) {
 
170
                                newResult.filteredMembers = new List<Tuple<ITypeDefinition, IUnresolvedMember>> ();
 
171
                                bool startsWithLastFilter = lastResult.pattern != null && newResult.pattern.StartsWith (lastResult.pattern, StringComparison.Ordinal) && lastResult.filteredMembers != null;
 
172
                                if (startsWithLastFilter) {
 
173
                                        foreach (var t in lastResult.filteredMembers) {
 
174
                                                if (unchecked(x++) % 100 == 0 && token.IsCancellationRequested)
 
175
                                                        return;
 
176
                                                var member = t.Item2;
 
177
                                                if (newResult.Tag != null) {
 
178
                                                        if (newResult.Tag == "m" && member.EntityType != EntityType.Method)
 
179
                                                                continue;
 
180
                                                        if (newResult.Tag == "p" && member.EntityType != EntityType.Property)
 
181
                                                                continue;
 
182
                                                        if (newResult.Tag == "f" && member.EntityType != EntityType.Field)
 
183
                                                                continue;
 
184
                                                        if (newResult.Tag == "evt" && member.EntityType != EntityType.Event)
 
185
                                                                continue;
 
186
                                                }
 
187
                                                SearchResult curResult = newResult.CheckMember (t.Item1, member);
 
188
                                                if (curResult != null) {
 
189
                                                        newResult.filteredMembers.Add (t);
 
190
                                                        newResult.results.AddResult (curResult);
 
191
                                                }
 
192
                                        }
 
193
                                } else {
 
194
                                        Func<IUnresolvedMember, bool> mPred = member => {
 
195
                                                if (newResult.Tag != null) {
 
196
                                                        if (newResult.Tag == "m" && member.EntityType != EntityType.Method)
 
197
                                                                return false;
 
198
                                                        if (newResult.Tag == "p" && member.EntityType != EntityType.Property)
 
199
                                                                return false;
 
200
                                                        if (newResult.Tag == "f" && member.EntityType != EntityType.Field)
 
201
                                                                return false;
 
202
                                                        if (newResult.Tag == "evt" && member.EntityType != EntityType.Event)
 
203
                                                                return false;
 
204
                                                }
 
205
                                                return newResult.IsMatchingMember (member);
 
206
                                        };
 
207
 
 
208
                                        getMembersTimer.BeginTiming ();
 
209
                                        try {
 
210
                                                foreach (var type in types) {
 
211
                                                        if (type.Kind == TypeKind.Delegate)
 
212
                                                                continue;
 
213
                                                        foreach (var p in type.Parts) {
 
214
                                                                foreach (var member in p.Members.Where (mPred)) {
 
215
                                                                        if (unchecked(x++) % 100 == 0 && token.IsCancellationRequested)
 
216
                                                                                return;
 
217
                                                                        SearchResult curResult = newResult.CheckMember (type, member);
 
218
                                                                        if (curResult != null) {
 
219
                                                                                newResult.filteredMembers.Add (Tuple.Create (type, member));
 
220
                                                                                newResult.results.AddResult (curResult);
 
221
                                                                        }
 
222
                                                                }
 
223
                                                        }
 
224
                                                }
 
225
                                        } finally {
 
226
                                                getMembersTimer.EndTiming ();
 
227
                                        }
 
228
                                }
 
229
                        }
 
230
                }
 
231
                
 
232
                class WorkerResult
 
233
                {
 
234
                        public string Tag {
 
235
                                get;
 
236
                                set;
 
237
                        }
 
238
 
 
239
                        public List<ProjectFile> filteredFiles;
 
240
                        public List<ITypeDefinition> filteredTypes;
 
241
                        public List<Tuple<ITypeDefinition, IUnresolvedMember>> filteredMembers;
 
242
                        string pattern2;
 
243
                        char firstChar;
 
244
                        char[] firstChars;
 
245
                        public string pattern {
 
246
                                get {
 
247
                                        return pattern2;
 
248
                                }
 
249
                                set {
 
250
                                        pattern2 = value;
 
251
                                        if (pattern2.Length == 1) {
 
252
                                                firstChar = pattern2[0];
 
253
                                                firstChars = new [] { char.ToUpper (firstChar), char.ToLower (firstChar) };
 
254
                                        } else {
 
255
                                                firstChars = null;
 
256
                                        }
 
257
                                }
 
258
                        }
 
259
                        public bool isGotoFilePattern;
 
260
                        public ResultsDataSource results;
 
261
                        public bool FullSearch;
 
262
                        public bool IncludeFiles, IncludeTypes, IncludeMembers;
 
263
                        public Ambience ambience;
 
264
                        public StringMatcher matcher;
 
265
                        
 
266
                        public WorkerResult (Widget widget)
 
267
                        {
 
268
                                results = new ResultsDataSource (widget);
 
269
                        }
 
270
                        
 
271
                        internal SearchResult CheckFile (ProjectFile file)
 
272
                        {
 
273
                                int rank;
 
274
                                string matchString = System.IO.Path.GetFileName (file.FilePath);
 
275
                                if (MatchName (matchString, out rank)) 
 
276
                                        return new FileSearchResult (pattern, matchString, rank, file, true);
 
277
                                
 
278
                                if (!FullSearch)
 
279
                                        return null;
 
280
                                matchString = FileSearchResult.GetRelProjectPath (file);
 
281
                                if (MatchName (matchString, out rank)) 
 
282
                                        return new FileSearchResult (pattern, matchString, rank, file, false);
 
283
                                
 
284
                                return null;
 
285
                        }
 
286
                        
 
287
                        internal SearchResult CheckType (ITypeDefinition type)
 
288
                        {
 
289
                                int rank;
 
290
                                if (MatchName (TypeSearchResult.GetPlainText (type, false), out rank))
 
291
                                        return new TypeSearchResult (pattern, TypeSearchResult.GetPlainText (type, false), rank, type, false) { Ambience = ambience };
 
292
                                if (!FullSearch)
 
293
                                        return null;
 
294
                                if (MatchName (TypeSearchResult.GetPlainText (type, true), out rank))
 
295
                                        return new TypeSearchResult (pattern, TypeSearchResult.GetPlainText (type, true), rank, type, true) { Ambience = ambience };
 
296
                                return null;
 
297
                        }
 
298
                        
 
299
                        internal SearchResult CheckMember (ITypeDefinition declaringType, IUnresolvedMember member)
 
300
                        {
 
301
                                int rank;
 
302
                                bool useDeclaringTypeName = member is IUnresolvedMethod && (((IUnresolvedMethod)member).IsConstructor || ((IUnresolvedMethod)member).IsDestructor);
 
303
                                string memberName = useDeclaringTypeName ? member.DeclaringTypeDefinition.Name : member.Name;
 
304
                                if (MatchName (memberName, out rank))
 
305
                                        return new MemberSearchResult (pattern, memberName, rank, declaringType, member, false) { Ambience = ambience };
 
306
                                return null;
 
307
                        }
 
308
 
 
309
                        internal bool IsMatchingMember (IUnresolvedMember member)
 
310
                        {
 
311
                                int rank;
 
312
                                bool useDeclaringTypeName = member is IUnresolvedMethod && (((IUnresolvedMethod)member).IsConstructor || ((IUnresolvedMethod)member).IsDestructor);
 
313
                                string memberName = useDeclaringTypeName ? member.DeclaringTypeDefinition.Name : member.Name;
 
314
                                return MatchName (memberName, out rank);
 
315
                        }
 
316
 
 
317
                        Dictionary<string, MatchResult> savedMatches = new Dictionary<string, MatchResult> (StringComparer.Ordinal);
 
318
 
 
319
                        bool MatchName (string name, out int matchRank)
 
320
                        {
 
321
                                if (name == null) {
 
322
                                        matchRank = -1;
 
323
                                        return false;
 
324
                                }
 
325
 
 
326
                                bool doesMatch;
 
327
                                if (firstChars != null) {
 
328
                                        int idx = name.IndexOfAny (firstChars);
 
329
                                        doesMatch = idx >= 0;
 
330
                                        if (doesMatch) {
 
331
                                                matchRank = int.MaxValue - (name.Length - 1) * 10 - idx;
 
332
                                                if (name[idx] != firstChar)
 
333
                                                        matchRank /= 2;
 
334
                                                return true;
 
335
                                        } else {
 
336
                                                matchRank = -1;
 
337
                                        }
 
338
                                        return false;
 
339
                                }
 
340
                                MatchResult savedMatch;
 
341
                                if (!savedMatches.TryGetValue (name, out savedMatch)) {
 
342
                                        doesMatch = matcher.CalcMatchRank (name, out matchRank);
 
343
                                        savedMatches [name] = savedMatch = new MatchResult (doesMatch, matchRank);
 
344
                                }
 
345
                                
 
346
                                matchRank = savedMatch.Rank;
 
347
                                return savedMatch.Match;
 
348
                        }
 
349
                }
 
350
        }
 
351
}