~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.Refactoring/MonoDevelop.Refactoring/ReferencesFinder.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ReferencesFinder.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
 
 
28
/*
 
29
 
 
30
 This is a new approach for speeding up find all reference - it does a full text search & then it resolves the found expression.
 
31
 Unfortunately this isn't as exact as the current approach - but this should be used if the resolver is more exact & capable of
 
32
 'looking around'.
 
33
 
 
34
using System;
 
35
using System.Collections.Generic;
 
36
using Mono.TextEditor;
 
37
using MonoDevelop.Projects.Dom;
 
38
using MonoDevelop.Core;
 
39
using MonoDevelop.Projects.Dom.Parser;
 
40
using MonoDevelop.Ide;
 
41
 
 
42
namespace MonoDevelop.Refactoring.References
 
43
{
 
44
        public class MemberReference
 
45
        {
 
46
                public FilePath FileName {
 
47
                        get;
 
48
                        set;
 
49
                }
 
50
                
 
51
                public ITextEditorDataProvider DataProvider {
 
52
                        get;
 
53
                        set;
 
54
                }
 
55
                
 
56
                public DocumentLocation Location {
 
57
                        get;
 
58
                        set;
 
59
                }
 
60
                
 
61
                public int Offset {
 
62
                        get;
 
63
                        set;
 
64
                }
 
65
                
 
66
                public int Length {
 
67
                        get;
 
68
                        set;
 
69
                }
 
70
                
 
71
                public MemberReference (FilePath fileName, ITextEditorDataProvider dataProvider, DocumentLocation location, int offset, int length)
 
72
                {
 
73
                        this.FileName = fileName;
 
74
                        this.DataProvider = dataProvider;
 
75
                        this.Location = location;
 
76
                        this.Offset = offset;
 
77
                        this.Length = length;
 
78
                }
 
79
 
 
80
        }
 
81
        
 
82
        public enum RefactoryScope
 
83
        {
 
84
                File,
 
85
                Project,
 
86
                Solution,
 
87
                DeclaringType
 
88
        }
 
89
        
 
90
        public static class ReferencesFinder
 
91
        {
 
92
                public static IEnumerable<MemberReference> GetReferences (IMember member)
 
93
                {
 
94
                        IType type = member is IType ? (IType)member : member.DeclaringType;
 
95
                        if (type == null) {
 
96
                                LoggingService.LogError ("Declaring type of '" + member + "' not found.");
 
97
                                yield break;
 
98
                        }
 
99
                        var matcher = GetReferenceMatcher (member);
 
100
                        foreach (var fileInfo in GetFileNames (member)) {
 
101
                                var textFile = TextFileProvider.Instance.GetEditableTextFile (fileInfo.FileName);
 
102
                                if (textFile.Text == null) // file not found
 
103
                                        continue;
 
104
                                var expressionFinder = ProjectDomService.GetExpressionFinder (fileInfo.FileName);
 
105
                                
 
106
                                foreach (var match in matcher.Search (textFile.Text)) {
 
107
                                        var data = (textFile as ITextEditorDataProvider).GetTextEditorData ();
 
108
                                        ExpressionResult expr = expressionFinder.FindFullExpression (data, match.Offset);
 
109
                                        if (expr.Expression == null) 
 
110
                                                continue;
 
111
                                        
 
112
                                        var resolver = ProjectDomService.GetParser (fileInfo.FileName).CreateResolver (fileInfo.Dom, data, fileInfo.FileName);
 
113
                                        var location = data.Document.OffsetToLocation (match.Offset);
 
114
                                        ResolveResult resolveResult = resolver.Resolve (expr, new DomLocation (location.Line, location.Column));
 
115
                                        
 
116
                                        // TODO: Add IsReferenceTo in the resolve results - could could be taken from FindMemberAstVisitor
 
117
                                        if (resolveResult != null && resolveResult.IsReferenceTo (member))
 
118
                                                yield return new MemberReference (fileInfo.FileName, (textFile as ITextEditorDataProvider), location, match.Position, match.Length);
 
119
                                }
 
120
                        }
 
121
                }
 
122
                
 
123
                public static  IReferenceMatcher GetReferenceMatcher (IMember member)
 
124
                {
 
125
                        IMethod method = member as IMethod;
 
126
                        if (method != null) {
 
127
                                if (method.IsConstructor) {
 
128
                                        return new AggregatedReferenceMatcher (
 
129
                                                new WholeWordMatcher (member.DeclaringType.Name),
 
130
                                                new WholeWordMatcher ("this"),
 
131
                                                new WholeWordMatcher ("base")
 
132
                                        );
 
133
                                }
 
134
                                if (method.IsFinalizer)
 
135
                                        return new WholeWordMatcher (member.DeclaringType.Name);
 
136
                        }
 
137
                        
 
138
                        IProperty property = member as IProperty;
 
139
                        if (property != null && property.IsIndexer) 
 
140
                                return new IndexBeforeReferenceMatcher ("[");
 
141
                                
 
142
                        return new WholeWordMatcher (member.Name);
 
143
                }
 
144
                
 
145
                
 
146
                
 
147
                
 
148
                static RefactoryScope GetScope (IMember member)
 
149
                {
 
150
                        if (member.DeclaringType != null && member.DeclaringType.ClassType == ClassType.Interface)
 
151
                                return GetScope (member.DeclaringType);
 
152
                        
 
153
                        if (member.IsPublic)
 
154
                                return RefactoryScope.Solution;
 
155
                        
 
156
                        if (member.IsProtected || member.IsInternal || member.DeclaringType == null)
 
157
                                return RefactoryScope.Project;
 
158
                        return RefactoryScope.DeclaringType;
 
159
                }
 
160
        }
 
161
        
 
162
        public struct ReferenceMatch
 
163
        {
 
164
                public readonly int Offset;
 
165
                public readonly int Length;
 
166
 
 
167
                public ReferenceMatch (int offset, int length)
 
168
                {
 
169
                        this.Offset = offset;
 
170
                        this.Length = length;
 
171
                }
 
172
        }
 
173
 
 
174
        public interface IReferenceMatcher
 
175
        {
 
176
                IEnumerable<ReferenceMatch> Search (string inputText);
 
177
        }
 
178
        
 
179
        class WholeWordMatcher : IReferenceMatcher
 
180
        {
 
181
                string pattern;
 
182
 
 
183
                public WholeWordMatcher (string pattern)
 
184
                {
 
185
                        this.pattern = pattern;
 
186
                }
 
187
 
 
188
                public IEnumerable<ReferenceMatch> Search (string inputText)
 
189
                {
 
190
                        if (pattern.Length == 0)
 
191
                                yield break;
 
192
                        int pos = -1;
 
193
                        while ((pos = inputText.IndexOf (pattern, pos + 1)) >= 0) {
 
194
                                if (pos > 0 && char.IsLetterOrDigit (inputText, pos - 1) ||
 
195
                                        pos < inputText.Length - pattern.Length - 1 && char.IsLetterOrDigit (inputText, pos + pattern.Length))
 
196
                                        continue;
 
197
                                yield return new ReferenceMatch (pos, pattern.Length);
 
198
                        }
 
199
                }
 
200
        }
 
201
        
 
202
        class AggregatedReferenceMatcher : IReferenceMatcher
 
203
        {
 
204
                IEnumerable<IReferenceMatcher> referenceMatcher;
 
205
                
 
206
                public AggregatedReferenceMatcher (params IReferenceMatcher[] matcher)
 
207
                {
 
208
                        this.referenceMatcher = matcher;
 
209
                }
 
210
                
 
211
                public AggregatedReferenceMatcher (IEnumerable<IReferenceMatcher> matcher)
 
212
                {
 
213
                        this.referenceMatcher = matcher;
 
214
                }
 
215
                
 
216
                public IEnumerable<ReferenceMatch> Search (string inputText)
 
217
                {
 
218
                        foreach (IReferenceMatcher matcher in referenceMatcher) {
 
219
                                foreach (ReferenceMatch match in matcher.Search (inputText)) {
 
220
                                        yield return match;
 
221
                                }
 
222
                        }
 
223
                }
 
224
        }
 
225
        
 
226
        class IndexBeforeReferenceMatcher : IReferenceMatcher
 
227
        {
 
228
                string tag;
 
229
                
 
230
                public IndexBeforeReferenceMatcher (string tag)
 
231
                {
 
232
                        this.tag = tag;
 
233
                }
 
234
                
 
235
                public IEnumerable<ReferenceMatch> Search (string inputText)
 
236
                {
 
237
                        int pos = -1;
 
238
                        while ((pos = inputText.IndexOf (tag, pos + 1)) >= 0) {
 
239
                                yield return new ReferenceMatch (pos - 1, tag.Length);
 
240
                        }
 
241
                }
 
242
        }
 
243
}*/
 
 
b'\\ No newline at end of file'