5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc.
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
27
using System.Collections.Generic;
28
using ICSharpCode.NRefactory.TypeSystem;
29
using ICSharpCode.NRefactory.Editor;
30
using ICSharpCode.NRefactory.CSharp.TypeSystem;
32
using ICSharpCode.NRefactory.CSharp.Resolver;
34
namespace ICSharpCode.NRefactory.CSharp.Completion
36
public interface ICompletionContextProvider
38
IList<string> ConditionalSymbols {
42
void GetCurrentMembers (int offset, out IUnresolvedTypeDefinition currentType, out IUnresolvedMember currentMember);
44
Tuple<string, TextLocation> GetMemberTextToCaret(int caretOffset, IUnresolvedTypeDefinition currentType, IUnresolvedMember currentMember);
46
CSharpAstResolver GetResolver (CSharpResolver resolver, AstNode rootNode);
49
public class DefaultCompletionContextProvider : ICompletionContextProvider
51
readonly IDocument document;
52
readonly CSharpUnresolvedFile unresolvedFile;
53
readonly List<string> symbols = new List<string> ();
55
public IList<string> ConditionalSymbols {
61
public DefaultCompletionContextProvider (IDocument document, CSharpUnresolvedFile unresolvedFile)
64
throw new ArgumentNullException("document");
65
if (unresolvedFile == null)
66
throw new ArgumentNullException("unresolvedFile");
67
this.document = document;
68
this.unresolvedFile = unresolvedFile;
71
public void AddSymbol (string sym)
75
public void GetCurrentMembers(int offset, out IUnresolvedTypeDefinition currentType, out IUnresolvedMember currentMember)
77
//var document = engine.document;
78
var location = document.GetLocation(offset);
82
foreach (var type in unresolvedFile.TopLevelTypeDefinitions) {
83
if (type.Region.Begin < location)
86
currentType = FindInnerType (currentType, location);
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))
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;
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;
107
var stack = GetBracketStack (engine.GetMemberTextToCaret ().Item1);
108
if (stack.Count == 0)
109
currentMember = null;*/
112
IUnresolvedTypeDefinition FindInnerType (IUnresolvedTypeDefinition parent, TextLocation location)
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);
125
bool IsInsideType (IUnresolvedEntity currentType, TextLocation location)
127
int startOffset = document.GetOffset (currentType.Region.Begin);
128
int endOffset = document.GetOffset (location);
129
//bool foundEndBracket = false;
131
var bracketStack = new Stack<char> ();
133
bool isInString = false, isInChar = false;
134
bool isInLineComment = false, isInBlockComment = false;
136
for (int i = startOffset; i < endOffset; i++) {
137
char ch = document.GetCharAt (i);
142
if (!isInString && !isInChar && !isInLineComment && !isInBlockComment)
143
bracketStack.Push (ch);
148
if (!isInString && !isInChar && !isInLineComment && !isInBlockComment)
149
if (bracketStack.Count > 0)
154
isInLineComment = false;
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);
163
isInLineComment = true;
164
if (!isInLineComment && nextChar == '*')
165
isInBlockComment = true;
169
if (!(isInChar || isInLineComment || isInBlockComment))
170
isInString = !isInString;
173
if (!(isInString || isInLineComment || isInBlockComment))
174
isInChar = !isInChar;
180
return bracketStack.Any (t => t == '{');
183
public Tuple<string, TextLocation> GetMemberTextToCaret(int caretOffset, IUnresolvedTypeDefinition currentType, IUnresolvedMember currentMember)
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);
193
while (startOffset > 0) {
194
char ch = document.GetCharAt(startOffset - 1);
195
if (ch != ' ' && ch != '\t') {
201
return Tuple.Create (document.GetText (startOffset, caretOffset - startOffset), document.GetLocation (startOffset));
205
public CSharpAstResolver GetResolver (CSharpResolver resolver, AstNode rootNode)
207
return new CSharpAstResolver (resolver, rootNode, unresolvedFile);