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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/SearchResult.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
// NavigateToDialog.cs
 
3
//  
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.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
 
 
27
using System;
 
28
using System.Text;
 
29
using Gdk;
 
30
using Gtk;
 
31
using MonoDevelop.Core;
 
32
using MonoDevelop.Core.Text;
 
33
using MonoDevelop.Projects;
 
34
using ICSharpCode.NRefactory.TypeSystem;
 
35
using MonoDevelop.Ide.TypeSystem;
 
36
using MonoDevelop.Components.Commands;
 
37
using MonoDevelop.Ide.CodeCompletion;
 
38
using MonoDevelop.Ide;
 
39
 
 
40
namespace MonoDevelop.Components.MainToolbar
 
41
{
 
42
        public enum SearchResultType
 
43
        {
 
44
                File,
 
45
                Type,
 
46
                Member,
 
47
                Command
 
48
        }
 
49
 
 
50
        abstract class SearchResult
 
51
        {
 
52
                protected string match;
 
53
                
 
54
                public virtual string GetMarkupText (Widget widget)
 
55
                {
 
56
                        return HighlightMatch (widget, PlainText, match);
 
57
                }
 
58
 
 
59
                public virtual string GetDescriptionMarkupText (Widget widget)
 
60
                {
 
61
                        return AmbienceService.EscapeText (Description);
 
62
                }
 
63
 
 
64
 
 
65
                public abstract SearchResultType SearchResultType { get; }
 
66
                public abstract string PlainText  { get; }
 
67
 
 
68
                public int Rank { get; private set; }
 
69
 
 
70
                public virtual int Row { get { return -1; } }
 
71
                public virtual int Column { get { return -1; } }
 
72
                
 
73
                public abstract string File { get; }
 
74
                public abstract Gdk.Pixbuf Icon { get; }
 
75
                
 
76
                public abstract string Description { get; }
 
77
                public string MatchedString { get; private set;}
 
78
 
 
79
                public abstract TooltipInformation TooltipInformation { get; }
 
80
 
 
81
                public SearchResult (string match, string matchedString, int rank)
 
82
                {
 
83
                        this.match = match;
 
84
                        this.MatchedString = matchedString;
 
85
                        Rank = rank;
 
86
                }
 
87
                
 
88
                protected static string HighlightMatch (Widget widget, string text, string toMatch)
 
89
                {
 
90
                        var lane = StringMatcher.GetMatcher (toMatch, true).GetMatch (text);
 
91
                        StringBuilder result = new StringBuilder ();
 
92
                        if (lane != null) {
 
93
                                int lastPos = 0;
 
94
                                for (int n=0; n < lane.Length; n++) {
 
95
                                        int pos = lane[n];
 
96
                                        if (pos - lastPos > 0)
 
97
                                                MarkupUtilities.AppendEscapedString (result, text.Substring (lastPos, pos - lastPos));
 
98
                                        result.Append ("<span foreground=\"#4d4d4d\" font_weight=\"bold\">");
 
99
                                        MarkupUtilities.AppendEscapedString (result, text[pos].ToString ());
 
100
                                        result.Append ("</span>");
 
101
                                        lastPos = pos + 1;
 
102
                                }
 
103
                                if (lastPos < text.Length)
 
104
                                        MarkupUtilities.AppendEscapedString (result, text.Substring (lastPos, text.Length - lastPos));
 
105
                        } else {
 
106
                                MarkupUtilities.AppendEscapedString (result, text);
 
107
                        }
 
108
                        return result.ToString ();
 
109
                }
 
110
 
 
111
                public virtual bool CanActivate {
 
112
                        get { return false; }
 
113
                }
 
114
 
 
115
                public virtual void Activate ()
 
116
                {
 
117
                }
 
118
        }
 
119
        
 
120
        class TypeSearchResult : MemberSearchResult
 
121
        {
 
122
                ITypeDefinition type;
 
123
                        
 
124
                public override SearchResultType SearchResultType { get { return SearchResultType.Type; } }
 
125
 
 
126
                public override string File {
 
127
                        get { return type.Region.FileName; }
 
128
                }
 
129
                
 
130
                public override Gdk.Pixbuf Icon {
 
131
                        get {
 
132
                                return ImageService.GetPixbuf (type.GetStockIcon (false), IconSize.Menu);
 
133
                        }
 
134
                }
 
135
                
 
136
                public override int Row {
 
137
                        get { return type.Region.BeginLine; }
 
138
                }
 
139
                
 
140
                public override int Column {
 
141
                        get { return type.Region.BeginColumn; }
 
142
                }
 
143
 
 
144
                public static string GetPlainText (ITypeDefinition type, bool useFullName)
 
145
                {
 
146
                        if (type.TypeParameterCount == 0)
 
147
                                return useFullName ? type.FullName : type.Name;
 
148
                        StringBuilder sb = new StringBuilder (useFullName ? type.FullName : type.Name);
 
149
                        sb.Append ("<");
 
150
                        for (int i = 0; i < type.TypeParameterCount; i++) {
 
151
                                if (i > 0)
 
152
                                        sb.Append (", ");
 
153
                                sb.Append (type.TypeParameters [i].Name);
 
154
                        }
 
155
                        sb.Append (">");
 
156
                        return sb.ToString ();
 
157
                }
 
158
                
 
159
                public override string PlainText {
 
160
                        get {
 
161
                                return GetPlainText (type, false);
 
162
                        }
 
163
                }
 
164
 
 
165
                public override MonoDevelop.Ide.CodeCompletion.TooltipInformation TooltipInformation {
 
166
                        get {
 
167
                                return Ambience.GetTooltip (type);
 
168
                        }
 
169
                }
 
170
 
 
171
                public override string Description {
 
172
                        get {
 
173
                                string loc;
 
174
                                if (type.GetSourceProject () != null) {
 
175
                                        loc = GettextCatalog.GetString ("project {0}", type.GetSourceProject ().Name);
 
176
                                } else {
 
177
                                        loc = GettextCatalog.GetString ("file {0}", type.Region.FileName);
 
178
                                }
 
179
 
 
180
                                switch (type.Kind) {
 
181
                                case TypeKind.Interface:
 
182
                                        return GettextCatalog.GetString ("interface ({0})", loc);
 
183
                                case TypeKind.Struct:
 
184
                                        return GettextCatalog.GetString ("struct ({0})", loc);
 
185
                                case TypeKind.Delegate:
 
186
                                        return GettextCatalog.GetString ("delegate ({0})", loc);
 
187
                                case TypeKind.Enum:
 
188
                                        return GettextCatalog.GetString ("enumeration ({0})", loc);
 
189
                                default:
 
190
                                        return GettextCatalog.GetString ("class ({0})", loc);
 
191
                                }
 
192
                        }
 
193
                }
 
194
                
 
195
                public override string GetMarkupText (Widget widget)
 
196
                {
 
197
                        return HighlightMatch (widget, GetPlainText (type, useFullName), match);
 
198
                }
 
199
                
 
200
                public TypeSearchResult (string match, string matchedString, int rank, ITypeDefinition type, bool useFullName) : base (match, matchedString, rank, null, null, useFullName)
 
201
                {
 
202
                        this.type = type;
 
203
                }
 
204
        }
 
205
        
 
206
        class FileSearchResult: SearchResult
 
207
        {
 
208
                ProjectFile file;
 
209
                bool useFileName;
 
210
 
 
211
                public override SearchResultType SearchResultType { get { return SearchResultType.File; } }
 
212
 
 
213
                public override string PlainText {
 
214
                        get {
 
215
                                if (useFileName)
 
216
                                        return System.IO.Path.GetFileName (file.FilePath);
 
217
                                return GetRelProjectPath (file);
 
218
                        }
 
219
                }
 
220
                 
 
221
                public override string File {
 
222
                        get {
 
223
                                return file.FilePath;
 
224
                        }
 
225
                }
 
226
                
 
227
                public override Gdk.Pixbuf Icon {
 
228
                        get {
 
229
                                return DesktopService.GetPixbufForFile (file.FilePath, IconSize.Menu);
 
230
                        }
 
231
                }
 
232
 
 
233
                public override MonoDevelop.Ide.CodeCompletion.TooltipInformation TooltipInformation {
 
234
                        get {
 
235
                                return null;
 
236
                        }
 
237
                }
 
238
 
 
239
                public override string Description {
 
240
                        get {
 
241
                                if (useFileName)
 
242
                                        return file.Project != null
 
243
                                                ? GettextCatalog.GetString ("file \"{0}\" in project \"{1}\"", GetRelProjectPath (file), file.Project.Name)
 
244
                                                : GettextCatalog.GetString ("file \"{0}\"", GetRelProjectPath (file));
 
245
                                return file.Project != null ? GettextCatalog.GetString ("file in project \"{0}\"", file.Project.Name) : "";
 
246
                        }
 
247
                }
 
248
                
 
249
                public FileSearchResult (string match, string matchedString, int rank, ProjectFile file, bool useFileName)
 
250
                                                        : base (match, matchedString, rank)
 
251
                {
 
252
                        this.file = file;
 
253
                        this.useFileName = useFileName;
 
254
                }
 
255
                
 
256
                internal static string GetRelProjectPath (ProjectFile file)
 
257
                {
 
258
                        if (file.Project != null)
 
259
                                return file.ProjectVirtualPath;
 
260
                        return file.FilePath;
 
261
                }
 
262
        }
 
263
        
 
264
        class MemberSearchResult : SearchResult
 
265
        {
 
266
                protected bool useFullName;
 
267
                protected IUnresolvedMember member;
 
268
                protected ITypeDefinition declaringType;
 
269
 
 
270
                public override SearchResultType SearchResultType { get { return SearchResultType.Member; } }
 
271
 
 
272
                protected virtual OutputFlags Flags {
 
273
                        get {
 
274
                                return OutputFlags.IncludeParameters | OutputFlags.IncludeGenerics
 
275
                                        | (useFullName  ? OutputFlags.UseFullName : OutputFlags.None);
 
276
                        }
 
277
                }
 
278
                
 
279
                public override string PlainText {
 
280
                        get {
 
281
                                return member.Name;
 
282
                        }
 
283
                }
 
284
 
 
285
                public override MonoDevelop.Ide.CodeCompletion.TooltipInformation TooltipInformation {
 
286
                        get {
 
287
                                var ctx = member.DeclaringTypeDefinition.CreateResolveContext (new SimpleTypeResolveContext (declaringType));
 
288
                                return Ambience.GetTooltip (member.Resolve (ctx));
 
289
                        }
 
290
                }
 
291
 
 
292
                public override string File {
 
293
                        get { return member.DeclaringTypeDefinition.Region.FileName; }
 
294
                }
 
295
                
 
296
                public override Gdk.Pixbuf Icon {
 
297
                        get {
 
298
                                return ImageService.GetPixbuf (member.GetStockIcon (false), IconSize.Menu);
 
299
                        }
 
300
                }
 
301
                
 
302
                public override int Row {
 
303
                        get { return member.Region.BeginLine; }
 
304
                }
 
305
                
 
306
                public override int Column {
 
307
                        get { return member.Region.BeginColumn; }
 
308
                }
 
309
                
 
310
                public override string Description {
 
311
                        get {
 
312
                                string loc = GettextCatalog.GetString ("type \"{0}\"", member.DeclaringTypeDefinition.Name);
 
313
 
 
314
                                switch (member.EntityType) {
 
315
                                case EntityType.Field:
 
316
                                        return GettextCatalog.GetString ("field ({0})", loc);
 
317
                                case EntityType.Property:
 
318
                                        return GettextCatalog.GetString ("property ({0})", loc);
 
319
                                case EntityType.Indexer:
 
320
                                        return GettextCatalog.GetString ("indexer ({0})", loc);
 
321
                                case EntityType.Event:
 
322
                                        return GettextCatalog.GetString ("event ({0})", loc);
 
323
                                case EntityType.Method:
 
324
                                        return GettextCatalog.GetString ("method ({0})", loc);
 
325
                                case EntityType.Operator:
 
326
                                        return GettextCatalog.GetString ("operator ({0})", loc);
 
327
                                case EntityType.Constructor:
 
328
                                        return GettextCatalog.GetString ("constructor ({0})", loc);
 
329
                                case EntityType.Destructor:
 
330
                                        return GettextCatalog.GetString ("destrutcor ({0})", loc);
 
331
                                default:
 
332
                                        throw new NotSupportedException (member.EntityType + " is not supported.");
 
333
                                }
 
334
                        }
 
335
                }
 
336
                
 
337
                public MemberSearchResult (string match, string matchedString, int rank, ITypeDefinition declaringType, IUnresolvedMember member, bool useFullName) : base (match, matchedString, rank)
 
338
                {
 
339
                        this.declaringType = declaringType;
 
340
                        this.member = member;
 
341
                        this.useFullName = useFullName;
 
342
                }
 
343
                
 
344
                public override string GetMarkupText (Widget widget)
 
345
                {
 
346
                        if (useFullName)
 
347
                                return HighlightMatch (widget, member.EntityType == EntityType.Constructor ? member.DeclaringTypeDefinition.FullName :  member.FullName, match);
 
348
                        return HighlightMatch (widget, member.EntityType == EntityType.Constructor ? member.DeclaringTypeDefinition.Name : member.Name, match);
 
349
                }
 
350
                
 
351
                internal Ambience Ambience { 
 
352
                        get;
 
353
                        set;
 
354
                }
 
355
        }
 
356
 
 
357
        class CommandResult: SearchResult
 
358
        {
 
359
                Command command;
 
360
                CommandInfo ci;
 
361
                CommandTargetRoute route;
 
362
 
 
363
                public CommandResult (Command cmd, CommandInfo ci, CommandTargetRoute route, string match, string matchedString, int rank): base (match, matchedString, rank)
 
364
                {
 
365
                        this.ci = ci;
 
366
                        command = cmd;
 
367
                        this.route = route;
 
368
                }
 
369
 
 
370
                public override SearchResultType SearchResultType {
 
371
                        get {
 
372
                                return SearchResultType.Command;
 
373
                        }
 
374
                }
 
375
 
 
376
                public override string PlainText {
 
377
                        get {
 
378
                                return MatchedString;
 
379
                        }
 
380
                }
 
381
 
 
382
                public override string File {
 
383
                        get {
 
384
                                return null;
 
385
                        }
 
386
                }
 
387
 
 
388
                public override Pixbuf Icon {
 
389
                        get {
 
390
                                return ImageService.GetPixbuf ("md-command", IconSize.Menu);
 
391
                        }
 
392
                }
 
393
 
 
394
                public override MonoDevelop.Ide.CodeCompletion.TooltipInformation TooltipInformation {
 
395
                        get {
 
396
                                return null;
 
397
                        }
 
398
                }
 
399
 
 
400
                public override string Description {
 
401
                        get {
 
402
                                string desc = "";
 
403
                                if (!string.IsNullOrEmpty (ci.AccelKey))
 
404
                                        desc = KeyBindingManager.BindingToDisplayLabel (ci.AccelKey, false);
 
405
                                if (!string.IsNullOrEmpty (ci.Description)) {
 
406
                                        if (desc.Length > 0)
 
407
                                                desc += " - ";
 
408
                                        desc += ci.Description;
 
409
                                }
 
410
                                else if (desc.Length == 0) {
 
411
                                        desc = "Command";
 
412
                                }
 
413
                                if (!string.IsNullOrEmpty (command.Category))
 
414
                                        desc += " (" + command.Category + ")";
 
415
                                return desc;
 
416
                        }
 
417
                }
 
418
                
 
419
                public override string GetMarkupText (Widget widget)
 
420
                {
 
421
                        return HighlightMatch (widget, MatchedString, match);
 
422
                }
 
423
 
 
424
                public override bool CanActivate {
 
425
                        get {
 
426
                                return true;
 
427
                        }
 
428
                }
 
429
 
 
430
                public override void Activate ()
 
431
                {
 
432
                        IdeApp.CommandService.DispatchCommand (command.Id, null, route.InitialTarget, CommandSource.MainToolbar);
 
433
                }
 
434
        }
 
435
}