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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/ResultsDataSource.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
using System;
 
27
using System.Text;
 
28
using System.Collections;
 
29
using System.Collections.Generic;
 
30
using System.Collections.ObjectModel;
 
31
using System.Threading;
 
32
using Gdk;
 
33
using Gtk;
 
34
using MonoDevelop.Projects;
 
35
using MonoDevelop.Components;
 
36
using MonoDevelop.Core;
 
37
using MonoDevelop.Core.Instrumentation;
 
38
using MonoDevelop.Ide.Gui;
 
39
using MonoDevelop.Ide.CodeCompletion;
 
40
using MonoDevelop.Components.MainToolbar;
 
41
using ICSharpCode.NRefactory.TypeSystem;
 
42
 
 
43
namespace MonoDevelop.Components.MainToolbar
 
44
{
 
45
        class ResultsDataSource: List<SearchResult>, ISearchDataSource
 
46
        {
 
47
 
 
48
                Gtk.Widget widget;
 
49
                SearchResult bestResult;
 
50
                int bestRank = int.MinValue;
 
51
 
 
52
                public ResultsDataSource (Gtk.Widget widget)
 
53
                {
 
54
                        this.widget = widget;
 
55
                }
 
56
 
 
57
                public void SortUpToN (MonoDevelop.Components.MainToolbar.SearchCategory.DataItemComparer comparison, int n)
 
58
                {
 
59
                        // use built-in sorting for small lists since the fast algorithm is only correct for lists larger than n * 2
 
60
                        if (Count < n * 2) {
 
61
                                this.Sort (comparison);
 
62
                                return;
 
63
                        }
 
64
                        int offset = 0;
 
65
                        // build binary heap from all items
 
66
                        for (int i = 0; i < Count; i++) {
 
67
                                int index = i;
 
68
                                var item = this [offset + i]; // use next item
 
69
                                
 
70
                                // and move it on top, if greater than parent
 
71
                                while (index > 0 &&
 
72
                                       comparison.Compare (this[offset + (index - 1) / 2], item) > 0) {
 
73
                                        int top = (index - 1) / 2;
 
74
                                        this [offset + index] = this [offset + top];
 
75
                                        index = top;
 
76
                                }
 
77
                                this [offset + index] = item;
 
78
                        }
 
79
                        
 
80
                        var bound = Math.Max (0, Count - 1 - n);
 
81
                        for (int i = Count - 1; i > bound; i--) {
 
82
                                // delete max and place it as last
 
83
                                var last = this [offset + i];
 
84
                                this [offset + i] = this [offset];
 
85
 
 
86
                                int index = 0;
 
87
                                // the last one positioned in the heap
 
88
                                while (index * 2 + 1 < i) {
 
89
                                        int left = index * 2 + 1, right = left + 1;
 
90
                                        
 
91
                                        if (right < i && comparison.Compare (this [offset + left], this [offset + right]) > 0) {
 
92
                                                if (comparison.Compare (last, this [offset + right]) < 0)
 
93
                                                        break;
 
94
                                                
 
95
                                                this [offset + index] = this [offset + right];
 
96
                                                index = right;
 
97
                                        } else {
 
98
                                                if (comparison.Compare (last, this [offset + left]) < 0)
 
99
                                                        break;
 
100
                                                
 
101
                                                this [offset + index] = this [offset + left];
 
102
                                                index = left;
 
103
                                        }
 
104
                                }
 
105
                                this [offset + index] = last;
 
106
                        }
 
107
 
 
108
                        // switch the lasts elements with the first ones (the last n elements are sorted)
 
109
                        for (int i = 0; i < n; i++) {
 
110
                                var tmp = this [Count - 1 - i];
 
111
                                this [Count - 1 - i] = this [i];
 
112
                                this [i] = tmp;
 
113
                        }
 
114
                }
 
115
 
 
116
                #region ISearchDataSource implementation
 
117
 
 
118
                Gdk.Pixbuf ISearchDataSource.GetIcon (int item)
 
119
                {
 
120
                        return this [item].Icon;
 
121
                }
 
122
 
 
123
                string ISearchDataSource.GetMarkup (int item, bool isSelected)
 
124
                {
 
125
                        if (isSelected)
 
126
                                return GLib.Markup.EscapeText (this [item].PlainText);
 
127
                        return this [item].GetMarkupText (widget);
 
128
                }
 
129
 
 
130
                string ISearchDataSource.GetDescriptionMarkup (int item, bool isSelected)
 
131
                {
 
132
                        if (isSelected)
 
133
                                return GLib.Markup.EscapeText (this [item].Description);
 
134
                        return this [item].GetDescriptionMarkupText (widget);
 
135
                }
 
136
 
 
137
                ICSharpCode.NRefactory.TypeSystem.DomRegion ISearchDataSource.GetRegion (int item)
 
138
                {
 
139
                        var result = this [item];
 
140
                        return new DomRegion (result.File, result.Row, result.Column, result.Row, result.Column);
 
141
                }
 
142
 
 
143
                bool ISearchDataSource.CanActivate (int item)
 
144
                {
 
145
                        var result = this [item];
 
146
                        return result.CanActivate;
 
147
                }
 
148
 
 
149
                void ISearchDataSource.Activate (int item)
 
150
                {
 
151
                        var result = this [item];
 
152
                        result.Activate ();
 
153
                }
 
154
                
 
155
                double ISearchDataSource.GetWeight (int item)
 
156
                {
 
157
                        return this [item].Rank;
 
158
                }
 
159
 
 
160
                int ISearchDataSource.ItemCount {
 
161
                        get {
 
162
                                return this.Count;
 
163
                        }
 
164
                }
 
165
 
 
166
                TooltipInformation ISearchDataSource.GetTooltip (int item)
 
167
                {
 
168
                        return this [item].TooltipInformation;
 
169
                }
 
170
                #endregion
 
171
 
 
172
                public SearchResult BestResult {
 
173
                        get {
 
174
                                return bestResult;
 
175
                        }
 
176
                }
 
177
 
 
178
                public void AddResult (SearchResult res)
 
179
                {
 
180
                        Add (res);
 
181
 
 
182
                        if (res.Rank > bestRank) {
 
183
                                bestResult = res;
 
184
                                bestRank = res.Rank;
 
185
                        }
 
186
                }
 
187
        }
 
188
}