public override bool HandleKeyPress(SharpDevelopTextAreaControl editor, char ch)
		{
			CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.FileName);
			int cursor = editor.ActiveTextAreaControl.Caret.Offset;
			ExpressionContext context = null;
			if (ch == '(') {
				if (CodeCompletionOptions.KeywordCompletionEnabled) {
					switch (editor.GetWordBeforeCaret().Trim()) {
						case "for":
						case "lock":
							context = ExpressionContext.Default;
							break;
						case "using":
							context = ExpressionContext.TypeDerivingFrom(ParserService.CurrentProjectContent.GetClass("System.IDisposable"), false);
							break;
						case "catch":
							context = ExpressionContext.TypeDerivingFrom(ParserService.CurrentProjectContent.GetClass("System.Exception"), false);
							break;
						case "foreach":
						case "typeof":
						case "sizeof":
						case "default":
							context = ExpressionContext.Type;
							break;
					}
				}
				if (context != null) {
					if (IsInComment(editor)) return false;
					editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ch);
					return true;
				} else if (EnableMethodInsight && CodeCompletionOptions.InsightEnabled) {
					editor.ShowInsightWindow(new MethodInsightDataProvider());
					return true;
				}
				return false;
			} else if (ch == '[') {
				LineSegment line = editor.Document.GetLineSegmentForOffset(cursor);
				if (TextUtilities.FindPrevWordStart(editor.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) {
				if (InsightRefreshOnComma(editor, ch))
					return true;
			} else if(ch == '=') {
				LineSegment curLine = editor.Document.GetLineSegmentForOffset(cursor);
				string documentText = editor.Text;
				int position = editor.ActiveTextAreaControl.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.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, editor.FileName, documentText);
						if (resolveResult != null && resolveResult.ResolvedType != null) {
							IClass underlyingClass = resolveResult.ResolvedType.GetUnderlyingClass();
							if (underlyingClass != null && underlyingClass.IsTypeInInheritanceTree(ParserService.CurrentProjectContent.GetClass("System.MulticastDelegate"))) {
								EventHandlerCompletitionDataProvider eventHandlerProvider = new EventHandlerCompletitionDataProvider(result.Expression, resolveResult);
								eventHandlerProvider.InsertSpace = true;
								editor.ShowCompletionWindow(eventHandlerProvider, ch);
							}
						}
					}
				} else if (position > 0) {
					ExpressionResult result = ef.FindFullExpression(documentText, position);
					
					if(result.Expression != null) {
						ResolveResult resolveResult = ParserService.Resolve(result, editor.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, editor.FileName, documentText);
						if (resolveResult != null && resolveResult.ResolvedType != null) {
							if (ProvideContextCompletion(editor, resolveResult.ResolvedType, ch)) {
								return true;
							}
						}
					}
				}
			} else if (ch == ';') {
				LineSegment curLine = editor.Document.GetLineSegmentForOffset(cursor);
				// don't return true when inference succeeds, otherwise the ';' won't be added to the document.
				TryDeclarationTypeInference(editor, curLine);
			}
			
			return base.HandleKeyPress(editor, ch);
		}
Beispiel #2
0
        public override bool HandleKeyPress(SharpDevelopTextAreaControl editor, char ch)
        {
            CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.FileName);
            int cursor = editor.ActiveTextAreaControl.Caret.Offset;
            ExpressionContext context = null;

            if (ch == '(')
            {
                if (CodeCompletionOptions.KeywordCompletionEnabled)
                {
                    switch (editor.GetWordBeforeCaret().Trim())
                    {
                    case "for":
                    case "lock":
                        context = ExpressionContext.Default;
                        break;

                    case "using":
                        context = ExpressionContext.TypeDerivingFrom(ParserService.CurrentProjectContent.GetClass("System.IDisposable"), false);
                        break;

                    case "catch":
                        context = ExpressionContext.TypeDerivingFrom(ParserService.CurrentProjectContent.GetClass("System.Exception"), false);
                        break;

                    case "foreach":
                    case "typeof":
                    case "sizeof":
                    case "default":
                        context = ExpressionContext.Type;
                        break;
                    }
                }
                if (context != null)
                {
                    if (IsInComment(editor))
                    {
                        return(false);
                    }
                    editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ch);
                    return(true);
                }
                else if (EnableMethodInsight && CodeCompletionOptions.InsightEnabled)
                {
                    editor.ShowInsightWindow(new MethodInsightDataProvider());
                    return(true);
                }
                return(false);
            }
            else if (ch == '[')
            {
                LineSegment line = editor.Document.GetLineSegmentForOffset(cursor);
                if (TextUtilities.FindPrevWordStart(editor.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)
            {
                if (InsightRefreshOnComma(editor, ch))
                {
                    return(true);
                }
            }
            else if (ch == '=')
            {
                LineSegment curLine      = editor.Document.GetLineSegmentForOffset(cursor);
                string      documentText = editor.Text;
                int         position     = editor.ActiveTextAreaControl.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.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, editor.FileName, documentText);
                        if (resolveResult != null && resolveResult.ResolvedType != null)
                        {
                            IClass underlyingClass = resolveResult.ResolvedType.GetUnderlyingClass();
                            if (underlyingClass != null && underlyingClass.IsTypeInInheritanceTree(ParserService.CurrentProjectContent.GetClass("System.MulticastDelegate")))
                            {
                                EventHandlerCompletitionDataProvider eventHandlerProvider = new EventHandlerCompletitionDataProvider(result.Expression, resolveResult);
                                eventHandlerProvider.InsertSpace = true;
                                editor.ShowCompletionWindow(eventHandlerProvider, ch);
                            }
                        }
                    }
                }
                else if (position > 0)
                {
                    ExpressionResult result = ef.FindFullExpression(documentText, position);

                    if (result.Expression != null)
                    {
                        ResolveResult resolveResult = ParserService.Resolve(result, editor.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, editor.FileName, documentText);
                        if (resolveResult != null && resolveResult.ResolvedType != null)
                        {
                            if (ProvideContextCompletion(editor, resolveResult.ResolvedType, ch))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            else if (ch == ';')
            {
                LineSegment curLine = editor.Document.GetLineSegmentForOffset(cursor);
                // don't return true when inference succeeds, otherwise the ';' won't be added to the document.
                TryDeclarationTypeInference(editor, curLine);
            }

            return(base.HandleKeyPress(editor, ch));
        }
        public override bool HandleKeyPress(SharpDevelopTextAreaControl editor, char ch)
        {
            CSharpExpressionFinder ef = CreateExpressionFinder(editor.FileName);
            int cursor = editor.ActiveTextAreaControl.Caret.Offset;
            ExpressionContext context = null;

            if (ch == '(')
            {
                if (context != null)
                {
                    if (IsInComment(editor))
                    {
                        return(false);
                    }
                    editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ch);
                    return(true);
                }
                else if (EnableMethodInsight && CodeCompletionOptions.InsightEnabled)
                {
                    editor.ShowInsightWindow(new MethodInsightDataProvider());
                    return(true);
                }
                return(false);
            }
            else if (ch == '[')
            {
                LineSegment line = editor.Document.GetLineSegmentForOffset(cursor);
                if (TextUtilities.FindPrevWordStart(editor.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)
            {
                if (InsightRefreshOnComma(editor, ch))
                {
                    return(true);
                }
            }
            else if (ch == '=')
            {
                LineSegment curLine      = editor.Document.GetLineSegmentForOffset(cursor);
                string      documentText = editor.Text;
                int         position     = editor.ActiveTextAreaControl.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.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, 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)))
                            {
                                EventHandlerCompletitionDataProvider eventHandlerProvider = new EventHandlerCompletitionDataProvider(result.Expression, resolveResult);
                                eventHandlerProvider.InsertSpace = true;
                                editor.ShowCompletionWindow(eventHandlerProvider, ch);
                            }
                        }
                    }
                }
                else if (position > 0)
                {
                    ExpressionResult result = ef.FindFullExpression(documentText, position);

                    if (result.Expression != null)
                    {
                        ResolveResult resolveResult = ParserService.Resolve(result, editor.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, editor.FileName, documentText);
                        if (resolveResult != null && resolveResult.ResolvedType != null)
                        {
                            if (ProvideContextCompletion(editor, resolveResult.ResolvedType, ch))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            else if (ch == '.')
            {
                editor.ShowCompletionWindow(new CSharpCodeCompletionDataProvider(), ch);
                return(true);
            }
            else if (ch == '>')
            {
                if (IsInComment(editor))
                {
                    return(false);
                }
                char prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' ';
                if (prevChar == '-')
                {
                    editor.ShowCompletionWindow(new PointerArrowCompletionDataProvider(), ch);

                    return(true);
                }
            }

            if (char.IsLetter(ch) && CodeCompletionOptions.CompleteWhenTyping)
            {
                if (editor.ActiveTextAreaControl.SelectionManager.HasSomethingSelected)
                {
                    // allow code completion when overwriting an identifier
                    cursor = editor.ActiveTextAreaControl.SelectionManager.SelectionCollection[0].Offset;
                    int endOffset = editor.ActiveTextAreaControl.SelectionManager.SelectionCollection[0].EndOffset;
                    // but block code completion when overwriting only part of an identifier
                    if (endOffset < editor.Document.TextLength && char.IsLetterOrDigit(editor.Document.GetCharAt(endOffset)))
                    {
                        return(false);
                    }
                    editor.ActiveTextAreaControl.SelectionManager.RemoveSelectedText();
                    editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursor);
                }
                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.Text, cursor);
                    LoggingService.Debug("CC: Beginning to type a word, result=" + result);
                    if (result.Context != ExpressionContext.IdentifierExpected)
                    {
                        editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(result.Context)
                        {
                            ShowTemplates = true,
                            AllowCompleteExistingExpression = afterUnderscore
                        }, '\0');
                    }
                }
            }

            return(base.HandleKeyPress(editor, ch));
        }
		public override bool HandleKeyPress(SharpDevelopTextAreaControl editor, char ch)
		{
			CSharpExpressionFinder ef = CreateExpressionFinder(editor.FileName);
			int cursor = editor.ActiveTextAreaControl.Caret.Offset;
			ExpressionContext context = null;
			if (ch == '(') {
				if (context != null) {
					if (IsInComment(editor)) return false;
					editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ch);
					return true;
				} else if (EnableMethodInsight && CodeCompletionOptions.InsightEnabled) {
					editor.ShowInsightWindow(new MethodInsightDataProvider());
					return true;
				}
				return false;
			} else if (ch == '[') {
				LineSegment line = editor.Document.GetLineSegmentForOffset(cursor);
				if (TextUtilities.FindPrevWordStart(editor.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) {
				if (InsightRefreshOnComma(editor, ch))
					return true;
			} else if(ch == '=') {
				LineSegment curLine = editor.Document.GetLineSegmentForOffset(cursor);
				string documentText = editor.Text;
				int position = editor.ActiveTextAreaControl.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.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, 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))) {
								EventHandlerCompletitionDataProvider eventHandlerProvider = new EventHandlerCompletitionDataProvider(result.Expression, resolveResult);
								eventHandlerProvider.InsertSpace = true;
								editor.ShowCompletionWindow(eventHandlerProvider, ch);
							}
						}
					}
				} else if (position > 0) {
					ExpressionResult result = ef.FindFullExpression(documentText, position);
					
					if(result.Expression != null) {
						ResolveResult resolveResult = ParserService.Resolve(result, editor.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, editor.FileName, documentText);
						if (resolveResult != null && resolveResult.ResolvedType != null) {
							if (ProvideContextCompletion(editor, resolveResult.ResolvedType, ch)) {
								return true;
							}
						}
					}
				}
			} else if (ch == '.') {
				editor.ShowCompletionWindow(new CSharpCodeCompletionDataProvider(), ch);
				return true;
			} else if (ch == '>') {
				if (IsInComment(editor)) return false;
				char prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' ';
				if (prevChar == '-') {
					editor.ShowCompletionWindow(new PointerArrowCompletionDataProvider(), ch);
					
					return true;
				}
			}
			
			if (char.IsLetter(ch) && CodeCompletionOptions.CompleteWhenTyping) {
				if (editor.ActiveTextAreaControl.SelectionManager.HasSomethingSelected) {
					// allow code completion when overwriting an identifier
					cursor = editor.ActiveTextAreaControl.SelectionManager.SelectionCollection[0].Offset;
					int endOffset = editor.ActiveTextAreaControl.SelectionManager.SelectionCollection[0].EndOffset;
					// but block code completion when overwriting only part of an identifier
					if (endOffset < editor.Document.TextLength && char.IsLetterOrDigit(editor.Document.GetCharAt(endOffset)))
						return false;
					editor.ActiveTextAreaControl.SelectionManager.RemoveSelectedText();
					editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursor);
				}
				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.Text, cursor);
					LoggingService.Debug("CC: Beginning to type a word, result=" + result);
					if (result.Context != ExpressionContext.IdentifierExpected) {
						editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(result.Context) {
						                            	ShowTemplates = true,
						                            	AllowCompleteExistingExpression = afterUnderscore
						                            }, '\0');
					}
				}
			}
			
			return base.HandleKeyPress(editor, ch);
		}