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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Completion/ICompletionContextProvider.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
// IMemberProvider.cs
 
3
//  
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin Inc.
 
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.Collections.Generic;
 
28
using ICSharpCode.NRefactory.TypeSystem;
 
29
using ICSharpCode.NRefactory.Editor;
 
30
using ICSharpCode.NRefactory.CSharp.TypeSystem;
 
31
using System.Linq;
 
32
using ICSharpCode.NRefactory.CSharp.Resolver;
 
33
 
 
34
namespace ICSharpCode.NRefactory.CSharp.Completion
 
35
{
 
36
        public interface ICompletionContextProvider
 
37
        {
 
38
                IList<string> ConditionalSymbols {
 
39
                        get;
 
40
                }
 
41
 
 
42
                void GetCurrentMembers (int offset, out IUnresolvedTypeDefinition currentType, out IUnresolvedMember currentMember);
 
43
 
 
44
                Tuple<string, TextLocation> GetMemberTextToCaret(int caretOffset, IUnresolvedTypeDefinition currentType, IUnresolvedMember currentMember);
 
45
 
 
46
                CSharpAstResolver GetResolver (CSharpResolver resolver, AstNode rootNode);
 
47
        }
 
48
 
 
49
        public class DefaultCompletionContextProvider : ICompletionContextProvider
 
50
        {
 
51
                readonly IDocument document;
 
52
                readonly CSharpUnresolvedFile unresolvedFile;
 
53
                readonly List<string> symbols = new List<string> ();
 
54
 
 
55
                public IList<string> ConditionalSymbols {
 
56
                        get {
 
57
                                return symbols;
 
58
                        }
 
59
                }
 
60
 
 
61
                public DefaultCompletionContextProvider (IDocument document, CSharpUnresolvedFile unresolvedFile)
 
62
                {
 
63
                        if (document == null)
 
64
                                throw new ArgumentNullException("document");
 
65
                        if (unresolvedFile == null)
 
66
                                throw new ArgumentNullException("unresolvedFile");
 
67
                        this.document = document;
 
68
                        this.unresolvedFile = unresolvedFile;
 
69
                }
 
70
 
 
71
                public void AddSymbol (string sym)
 
72
                {
 
73
                        symbols.Add (sym);
 
74
                }
 
75
                public void GetCurrentMembers(int offset, out IUnresolvedTypeDefinition currentType, out IUnresolvedMember currentMember)
 
76
                {
 
77
                        //var document = engine.document;
 
78
                        var location = document.GetLocation(offset);
 
79
                        
 
80
                        currentType = null;
 
81
                        
 
82
                        foreach (var type in unresolvedFile.TopLevelTypeDefinitions) {
 
83
                                if (type.Region.Begin < location)
 
84
                                        currentType = type;
 
85
                        }
 
86
                        currentType = FindInnerType (currentType, location);
 
87
                        
 
88
                        // location is beyond last reported end region, now we need to check, if the end region changed
 
89
                        if (currentType != null && currentType.Region.End < location) {
 
90
                                if (!IsInsideType (currentType, location))
 
91
                                        currentType = null;
 
92
                        }
 
93
                        currentMember = null;
 
94
                        if (currentType != null) {
 
95
                                foreach (var member in currentType.Members) {
 
96
                                        if (member.Region.Begin < location && (currentMember == null || currentMember.Region.Begin < member.Region.Begin))
 
97
                                                currentMember = member;
 
98
                                }
 
99
                        }
 
100
                        
 
101
                        // location is beyond last reported end region, now we need to check, if the end region changed
 
102
                        // NOTE: Enums are a special case, there the "last" field needs to be treated as current member
 
103
                        if (currentMember != null && currentMember.Region.End < location && currentType.Kind != TypeKind.Enum) {
 
104
                                if (!IsInsideType (currentMember, location))
 
105
                                        currentMember = null;
 
106
                        }/*
 
107
                        var stack = GetBracketStack (engine.GetMemberTextToCaret ().Item1);
 
108
                        if (stack.Count == 0)
 
109
                                currentMember = null;*/
 
110
                }
 
111
 
 
112
                IUnresolvedTypeDefinition FindInnerType (IUnresolvedTypeDefinition parent, TextLocation location)
 
113
                {
 
114
                        if (parent == null)
 
115
                                return null;
 
116
                        var currentType = parent;
 
117
                        foreach (var type in parent.NestedTypes) {
 
118
                                if (type.Region.Begin < location  && location < type.Region.End)
 
119
                                        currentType = FindInnerType (type, location);
 
120
                        }
 
121
                        
 
122
                        return currentType;
 
123
                }
 
124
                
 
125
                bool IsInsideType (IUnresolvedEntity currentType, TextLocation location)
 
126
                {
 
127
                        int startOffset = document.GetOffset (currentType.Region.Begin);
 
128
                        int endOffset = document.GetOffset (location);
 
129
                        //bool foundEndBracket = false;
 
130
                
 
131
                        var bracketStack = new Stack<char> ();
 
132
                
 
133
                        bool isInString = false, isInChar = false;
 
134
                        bool isInLineComment = false, isInBlockComment = false;
 
135
                        
 
136
                        for (int i = startOffset; i < endOffset; i++) {
 
137
                                char ch = document.GetCharAt (i);
 
138
                                switch (ch) {
 
139
                                        case '(':
 
140
                                        case '[':
 
141
                                        case '{':
 
142
                                                if (!isInString && !isInChar && !isInLineComment && !isInBlockComment)
 
143
                                                        bracketStack.Push (ch);
 
144
                                                break;
 
145
                                        case ')':
 
146
                                        case ']':
 
147
                                        case '}':
 
148
                                                if (!isInString && !isInChar && !isInLineComment && !isInBlockComment)
 
149
                                                if (bracketStack.Count > 0)
 
150
                                                        bracketStack.Pop ();
 
151
                                                break;
 
152
                                        case '\r':
 
153
                                        case '\n':
 
154
                                                isInLineComment = false;
 
155
                                                break;
 
156
                                        case '/':
 
157
                                                if (isInBlockComment) {
 
158
                                                        if (i > 0 && document.GetCharAt (i - 1) == '*') 
 
159
                                                                isInBlockComment = false;
 
160
                                                } else if (!isInString && !isInChar && i + 1 < document.TextLength) {
 
161
                                                        char nextChar = document.GetCharAt (i + 1);
 
162
                                                        if (nextChar == '/')
 
163
                                                                isInLineComment = true;
 
164
                                                        if (!isInLineComment && nextChar == '*')
 
165
                                                                isInBlockComment = true;
 
166
                                                }
 
167
                                                break;
 
168
                                        case '"':
 
169
                                                if (!(isInChar || isInLineComment || isInBlockComment)) 
 
170
                                                        isInString = !isInString;
 
171
                                                break;
 
172
                                        case '\'':
 
173
                                                if (!(isInString || isInLineComment || isInBlockComment)) 
 
174
                                                        isInChar = !isInChar;
 
175
                                                break;
 
176
                                        default :
 
177
                                                break;
 
178
                                        }
 
179
                                }
 
180
                        return bracketStack.Any (t => t == '{');
 
181
                }
 
182
        
 
183
                public Tuple<string, TextLocation> GetMemberTextToCaret(int caretOffset, IUnresolvedTypeDefinition currentType, IUnresolvedMember currentMember)
 
184
                {
 
185
                        int startOffset;
 
186
                        if (currentMember != null && currentType != null && currentType.Kind != TypeKind.Enum) {
 
187
                                startOffset = document.GetOffset(currentMember.Region.Begin);
 
188
                        } else if (currentType != null) {
 
189
                                startOffset = document.GetOffset(currentType.Region.Begin);
 
190
                        } else {
 
191
                                startOffset = 0;
 
192
                        }
 
193
                        while (startOffset > 0) {
 
194
                                char ch = document.GetCharAt(startOffset - 1);
 
195
                                if (ch != ' ' && ch != '\t') {
 
196
                                        break;
 
197
                                }
 
198
                                --startOffset;
 
199
                        }
 
200
 
 
201
                        return Tuple.Create (document.GetText (startOffset, caretOffset - startOffset), document.GetLocation (startOffset));
 
202
                }
 
203
 
 
204
 
 
205
                public CSharpAstResolver GetResolver (CSharpResolver resolver, AstNode rootNode)
 
206
                {
 
207
                        return new CSharpAstResolver (resolver, rootNode, unresolvedFile);
 
208
                }
 
209
 
 
210
 
 
211
        }
 
212
}
 
213