protected bool ProvideContextCompletion(ITextEditor editor, IReturnType expected, char charTyped)
		{
			if (expected == null) return false;
			IClass c = expected.GetUnderlyingClass();
			if (c == null) return false;
			if (c.ClassType == ClassType.Enum) {
				CtrlSpaceCompletionItemProvider cdp = new NRefactoryCtrlSpaceCompletionItemProvider(languageProperties);
				var ctrlSpaceList = cdp.GenerateCompletionList(editor);
				if (ctrlSpaceList == null) return false;
				ContextCompletionItemList contextList = new ContextCompletionItemList();
				contextList.Items.AddRange(ctrlSpaceList.Items);
				contextList.activationKey = charTyped;
				foreach (CodeCompletionItem item in contextList.Items.OfType<CodeCompletionItem>()) {
					IClass itemClass = item.Entity as IClass;
					if (itemClass != null && c.FullyQualifiedName == itemClass.FullyQualifiedName && c.TypeParameters.Count == itemClass.TypeParameters.Count) {
						contextList.SuggestedItem = item;
						break;
					}
				}
				if (contextList.SuggestedItem != null) {
					if (charTyped != ' ') contextList.InsertSpace = true;
					editor.ShowCompletionWindow(contextList);
					return true;
				}
			}
			return false;
		}
		public override bool CtrlSpace(ITextEditor editor)
		{
			NRefactoryCtrlSpaceCompletionItemProvider provider = new NRefactoryCtrlSpaceCompletionItemProvider(languageProperties);
			provider.AllowCompleteExistingExpression = true;
			// on Ctrl+Space, include items (e.g. types / extension methods) from all namespaces, regardless of imports
			provider.ShowItemsFromAllNamespaces = true;
			provider.ShowCompletion(editor);
			return true;
		}
Пример #3
0
        public override bool CtrlSpace(ITextEditor editor)
        {
            NRefactoryCtrlSpaceCompletionItemProvider provider = new NRefactoryCtrlSpaceCompletionItemProvider(languageProperties);

            provider.AllowCompleteExistingExpression = true;
            // on Ctrl+Space, include items (e.g. types / extension methods) from all namespaces, regardless of imports
            provider.ShowItemsFromAllNamespaces = true;
            provider.ShowCompletion(editor);
            return(true);
        }
Пример #4
0
        // TODO : remove this code duplication!
        // see NRefactoryCodeCompletionBinding
        bool ProvideContextCompletion(ITextEditor editor, IReturnType expected, char charTyped)
        {
            if (expected == null)
            {
                return(false);
            }
            IClass c = expected.GetUnderlyingClass();

            if (c == null)
            {
                return(false);
            }
            if (c.ClassType == ClassType.Enum)
            {
                CtrlSpaceCompletionItemProvider cdp = new NRefactoryCtrlSpaceCompletionItemProvider(languageProperties);
                var ctrlSpaceList = cdp.GenerateCompletionList(editor);
                if (ctrlSpaceList == null)
                {
                    return(false);
                }
                ContextCompletionItemList contextList = new ContextCompletionItemList();
                contextList.Items.AddRange(ctrlSpaceList.Items);
                contextList.activationKey = charTyped;
                foreach (CodeCompletionItem item in contextList.Items.OfType <CodeCompletionItem>())
                {
                    IClass itemClass = item.Entity as IClass;
                    if (itemClass != null && c.FullyQualifiedName == itemClass.FullyQualifiedName && c.TypeParameters.Count == itemClass.TypeParameters.Count)
                    {
                        contextList.SuggestedItem = item;
                        break;
                    }
                }
                if (contextList.SuggestedItem != null)
                {
                    if (charTyped != ' ')
                    {
                        contextList.InsertSpace = true;
                    }
                    editor.ShowCompletionWindow(contextList);
                    return(true);
                }
            }
            return(false);
        }
		public override CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch)
		{
			CSharpExpressionFinder ef = CreateExpressionFinder(editor.FileName);
			int cursor = editor.Caret.Offset;
			if (ch == '[') {
				var line = editor.Document.GetLineForOffset(cursor);
				/* TODO: AVALONEDIT Reimplement this
				if (TextUtilities.FindPrevWordStart(editor.ActiveTextAreaControl.Document, cursor) <= line.Offset) {
					// [ is first character on the line
					// -> Attribute completion
					editor.ShowCompletionWindow(new AttributesDataProvider(ParserService.CurrentProjectContent), ch);
					return true;
				}*/
			} else if (ch == ',' && CodeCompletionOptions.InsightRefreshOnComma && CodeCompletionOptions.InsightEnabled) {
				IInsightWindow insightWindow;
				if (insightHandler.InsightRefreshOnComma(editor, ch, out insightWindow)) {
					insightHandler.HighlightParameter(insightWindow, -1); // disable highlighting
					return CodeCompletionKeyPressResult.Completed;
				}
			} else if(ch == '=') {
				var curLine = editor.Document.GetLineForOffset(cursor);
				string documentText = editor.Document.Text;
				int position = editor.Caret.Offset - 2;
				
				if (position > 0 && (documentText[position + 1] == '+')) {
					ExpressionResult result = ef.FindFullExpression(documentText, position);
					
					if(result.Expression != null) {
						ResolveResult resolveResult = ParserService.Resolve(result, editor.Caret.Line, editor.Caret.Column, editor.FileName, documentText);
						if (resolveResult != null && resolveResult.ResolvedType != null) {
							IClass underlyingClass = resolveResult.ResolvedType.GetUnderlyingClass();
							if (underlyingClass != null && underlyingClass.IsTypeInInheritanceTree(ParserService.CurrentProjectContent.GetClass("System.MulticastDelegate", 0))) {
								EventHandlerCompletionItemProvider eventHandlerProvider = new EventHandlerCompletionItemProvider(result.Expression, resolveResult);
								eventHandlerProvider.ShowCompletion(editor);
								return CodeCompletionKeyPressResult.Completed;
							}
						}
					}
				} else if (position > 0) {
					ExpressionResult result = ef.FindFullExpression(documentText, position);
					
					if(result.Expression != null) {
						ResolveResult resolveResult = ParserService.Resolve(result, editor.Caret.Line, editor.Caret.Column, editor.FileName, documentText);
						if (resolveResult != null && resolveResult.ResolvedType != null) {
							if (ProvideContextCompletion(editor, resolveResult.ResolvedType, ch)) {
								return CodeCompletionKeyPressResult.Completed;
							}
						}
					}
				}
			} else if (ch == '.') {
				new CSharpCodeCompletionDataProvider().ShowCompletion(editor);
				return CodeCompletionKeyPressResult.Completed;
			} else if (ch == '>') {
				if (IsInComment(editor)) return CodeCompletionKeyPressResult.None;
				char prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' ';
				if (prevChar == '-') {
					new PointerArrowCompletionDataProvider().ShowCompletion(editor);
					
					return CodeCompletionKeyPressResult.Completed;
				}
			}
			
			if (char.IsLetter(ch) && CodeCompletionOptions.CompleteWhenTyping) {
				if (editor.SelectionLength > 0) {
					// allow code completion when overwriting an identifier
					int endOffset = editor.SelectionStart + editor.SelectionLength;
					// but block code completion when overwriting only part of an identifier
					if (endOffset < editor.Document.TextLength && char.IsLetterOrDigit(editor.Document.GetCharAt(endOffset)))
						return CodeCompletionKeyPressResult.None;
					
					editor.Document.Remove(editor.SelectionStart, editor.SelectionLength);
					// Read caret position again after removal - this is required because the document might change in other
					// locations, too (e.g. bound elements in snippets).
					cursor = editor.Caret.Offset;
				}
				char prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' ';
				bool afterUnderscore = prevChar == '_';
				if (afterUnderscore) {
					cursor--;
					prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' ';
				}
				if (!char.IsLetterOrDigit(prevChar) && prevChar != '.' && !IsInComment(editor)) {
					ExpressionResult result = ef.FindExpression(editor.Document.Text, cursor);
					LoggingService.Debug("CC: Beginning to type a word, result=" + result);
					if (result.Context != ExpressionContext.IdentifierExpected) {
						var ctrlSpaceProvider = new NRefactoryCtrlSpaceCompletionItemProvider(LanguageProperties.CSharp, result.Context);
						ctrlSpaceProvider.ShowTemplates = true;
						ctrlSpaceProvider.AllowCompleteExistingExpression = afterUnderscore;
						ctrlSpaceProvider.ShowCompletion(editor);
						return CodeCompletionKeyPressResult.CompletedIncludeKeyInCompletion;
					}
				}
			}
			
			return base.HandleKeyPress(editor, ch);
		}