IEnumerable<ICompletionData> HandleKeywordCompletion(int wordStart, string word)
{
if (IsInsideCommentStringOrDirective()) {
if (IsInPreprocessorDirective()) {
if (word == "if" || word == "elif") {
if (wordStart > 0 && document.GetCharAt(wordStart - 1) == '#') {
return factory.CreatePreProcessorDefinesCompletionData();
}
}
}
return null;
}
switch (word) {
case "namespace":
return null;
case "using":
if (currentType != null) {
return null;
}
var wrapper = new CompletionDataWrapper(this);
AddTypesAndNamespaces(wrapper, GetState(), null, t => null);
return wrapper.Result;
case "case":
return CreateCaseCompletionData(location);
// case ",":
// case ":":
// if (result.ExpressionContext == ExpressionContext.InheritableType) {
// IType cls = NRefactoryResolver.GetTypeAtCursor (Document.CompilationUnit, Document.FileName, new TextLocation (completionContext.TriggerLine, completionContext.TriggerLineOffset));
// CompletionDataList completionList = new ProjectDomCompletionDataList ();
// List<string > namespaceList = GetUsedNamespaces ();
// var col = new CSharpTextEditorCompletion.CompletionDataCollector (this, dom, completionList, Document.CompilationUnit, null, location);
// bool isInterface = false;
// HashSet<string > baseTypeNames = new HashSet<string> ();
// if (cls != null) {
// baseTypeNames.Add (cls.Name);
// if (cls.ClassType == ClassType.Struct)
// isInterface = true;
// }
// int tokenIndex = offset;
//
// // Search base types " : [Type1, ... ,TypeN,] <Caret>"
// string token = null;
// do {
// token = GetPreviousToken (ref tokenIndex, false);
// if (string.IsNullOrEmpty (token))
// break;
// token = token.Trim ();
// if (Char.IsLetterOrDigit (token [0]) || token [0] == '_') {
// IType baseType = dom.SearchType (Document.CompilationUnit, cls, result.Region.Start, token);
// if (baseType != null) {
// if (baseType.ClassType != ClassType.Interface)
// isInterface = true;
// baseTypeNames.Add (baseType.Name);
// }
// }
// } while (token != ":");
// foreach (object o in dom.GetNamespaceContents (namespaceList, true, true)) {
// IType type = o as IType;
// if (type != null && (type.IsStatic || type.IsSealed || baseTypeNames.Contains (type.Name) || isInterface && type.ClassType != ClassType.Interface)) {
// continue;
// }
// if (o is Namespace && !namespaceList.Any (ns => ns.StartsWith (((Namespace)o).FullName)))
// continue;
// col.Add (o);
// }
// // Add inner classes
// Stack<IType > innerStack = new Stack<IType> ();
// innerStack.Push (cls);
// while (innerStack.Count > 0) {
// IType curType = innerStack.Pop ();
// if (curType == null)
// continue;
// foreach (IType innerType in curType.InnerTypes) {
// if (innerType != cls)
// // don't add the calling class as possible base type
// col.Add (innerType);
// }
// if (curType.DeclaringType != null)
// innerStack.Push (curType.DeclaringType);
// }
// return completionList;
// }
// break;
case "is":
case "as":
if (currentType == null) {
return null;
}
IType isAsType = null;
var isAsExpression = GetExpressionAt(wordStart);
if (isAsExpression != null) {
var parent = isAsExpression.Node.Parent;
if (parent is VariableInitializer) {
parent = parent.Parent;
}
if (parent is VariableDeclarationStatement) {
var resolved = ResolveExpression(parent);
if (resolved != null) {
isAsType = resolved.Item1.Type;
}
}
}
var isAsWrapper = new CompletionDataWrapper(this);
var def = isAsType != null ? isAsType.GetDefinition() : null;
AddTypesAndNamespaces(
isAsWrapper,
GetState(),
null,
t => t.GetDefinition() == null || def == null || t.GetDefinition().IsDerivedFrom(def) ? t : null,
m => false);
return isAsWrapper.Result;
// {
// CompletionDataList completionList = new ProjectDomCompletionDataList ();
// ExpressionResult expressionResult = FindExpression (dom, completionContext, wordStart - document.Caret.Offset);
// NRefactoryResolver resolver = CreateResolver ();
// ResolveResult resolveResult = resolver.Resolve (expressionResult, new TextLocation (completionContext.TriggerLine, completionContext.TriggerLineOffset));
// if (resolveResult != null && resolveResult.ResolvedType != null) {
// CompletionDataCollector col = new CompletionDataCollector (this, dom, completionList, Document.CompilationUnit, resolver.CallingType, location);
// IType foundType = null;
// if (word == "as") {
// ExpressionContext exactContext = new NewCSharpExpressionFinder (dom).FindExactContextForAsCompletion (document, Document.CompilationUnit, Document.FileName, resolver.CallingType);
// if (exactContext is ExpressionContext.TypeExpressionContext) {
// foundType = resolver.SearchType (((ExpressionContext.TypeExpressionContext)exactContext).Type);
// AddAsCompletionData (col, foundType);
// }
// }
//
// if (foundType == null)
// foundType = resolver.SearchType (resolveResult.ResolvedType);
//
// if (foundType != null) {
// if (foundType.ClassType == ClassType.Interface)
// foundType = resolver.SearchType (DomReturnType.Object);
//
// foreach (IType type in dom.GetSubclasses (foundType)) {
// if (type.IsSpecialName || type.Name.StartsWith ("<"))
// continue;
// AddAsCompletionData (col, type);
// }
// }
// List<string > namespaceList = GetUsedNamespaces ();
// foreach (object o in dom.GetNamespaceContents (namespaceList, true, true)) {
// if (o is IType) {
// IType type = (IType)o;
// if (type.ClassType != ClassType.Interface || type.IsSpecialName || type.Name.StartsWith ("<"))
// continue;
// // if (foundType != null && !dom.GetInheritanceTree (foundType).Any (x => x.FullName == type.FullName))
// // continue;
// AddAsCompletionData (col, type);
// continue;
// }
// if (o is Namespace)
// continue;
// col.Add (o);
// }
// return completionList;
// }
// result.ExpressionContext = ExpressionContext.TypeName;
// return CreateCtrlSpaceCompletionData (completionContext, result);
// }
case "override":
// Look for modifiers, in order to find the beginning of the declaration
int firstMod = wordStart;
int i = wordStart;
for (int n = 0; n < 3; n++) {
string mod = GetPreviousToken(ref i, true);
if (mod == "public" || mod == "protected" || mod == "private" || mod == "internal" || mod == "sealed") {
firstMod = i;
} else if (mod == "static") {
// static methods are not overridable
return null;
} else {
break;
}
}
if (!IsLineEmptyUpToEol()) {
return null;
}
if (currentType != null && (currentType.Kind == TypeKind.Class || currentType.Kind == TypeKind.Struct)) {
string modifiers = document.GetText(firstMod, wordStart - firstMod);
return GetOverrideCompletionData(currentType, modifiers);
}
return null;
case "partial":
// Look for modifiers, in order to find the beginning of the declaration
firstMod = wordStart;
i = wordStart;
for (int n = 0; n < 3; n++) {
string mod = GetPreviousToken(ref i, true);
if (mod == "public" || mod == "protected" || mod == "private" || mod == "internal" || mod == "sealed") {
firstMod = i;
} else if (mod == "static") {
// static methods are not overridable
return null;
} else {
break;
}
}
if (!IsLineEmptyUpToEol()) {
return null;
}
var state = GetState();
if (state.CurrentTypeDefinition != null && (state.CurrentTypeDefinition.Kind == TypeKind.Class || state.CurrentTypeDefinition.Kind == TypeKind.Struct)) {
string modifiers = document.GetText(firstMod, wordStart - firstMod);
return GetPartialCompletionData(state.CurrentTypeDefinition, modifiers);
}
return null;
case "public":
case "protected":
case "private":
case "internal":
case "sealed":
case "static":
var accessorContext = HandleAccessorContext();
if (accessorContext != null) {
return accessorContext;
}
wrapper = new CompletionDataWrapper(this);
state = GetState();
if (currentType != null) {
AddTypesAndNamespaces(wrapper, state, null, null, m => false);
AddKeywords(wrapper, primitiveTypesKeywords);
}
AddKeywords(wrapper, typeLevelKeywords);
return wrapper.Result;
case "new":
int j = offset - 4;
// string token = GetPreviousToken (ref j, true);
IType hintType = null;
var expressionOrVariableDeclaration = GetNewExpressionAt(j);
if (expressionOrVariableDeclaration == null)
return null;
var astResolver = CompletionContextProvider.GetResolver(GetState(), expressionOrVariableDeclaration.Unit);
hintType = CreateFieldAction.GetValidTypes(
astResolver,
expressionOrVariableDeclaration.Node as Expression
)
.FirstOrDefault();
return CreateTypeCompletionData(hintType);
case "yield":
var yieldDataList = new CompletionDataWrapper(this);
DefaultCompletionString = "return";
yieldDataList.AddCustom("break");
yieldDataList.AddCustom("return");
return yieldDataList.Result;
case "in":
var inList = new CompletionDataWrapper(this);
var expr = GetExpressionAtCursor();
var rr = ResolveExpression(expr);
AddContextCompletion(
inList,
rr != null ? rr.Item2 : GetState(),
expr.Node
);
return inList.Result;
}
return null;
}