Example #1
0
        private CodeCompletionKeyPressResult?CompleteSpace(ITextEditor editor, IExpressionFinder ef)
        {
            CodeCompletionKeyPressResult?returnResult = null;
            var documentText = editor.Document.Text;
            var position     = editor.Caret.Offset - 3;

            if (position > 0 && (documentText[position + 1] == '+') && documentText[position + 2] == '=')
            {
                var result = ef.FindFullExpression(documentText, position);

                if (result.Expression != null)
                {
                    var resolveResult = ParserService.Resolve(result, editor.Caret.Line, editor.Caret.Column,
                                                              editor.Document.Text, documentText, ProjectContent);
                    if (resolveResult != null && resolveResult.ResolvedType != null)
                    {
                        var underlyingClass = resolveResult.ResolvedType.GetUnderlyingClass();
                        if (underlyingClass != null &&
                            underlyingClass.IsTypeInInheritanceTree(
                                ProjectContent.GetClass("System.MulticastDelegate", 0)))
                        {
                            var eventHandlerProvider =
                                new EventHandlerCompletionItemProvider(result.Expression, resolveResult);
                            ShowCompletion(eventHandlerProvider, editor, ProjectContent);
                            returnResult = CodeCompletionKeyPressResult.Completed;
                        }
                    }
                }
            }
            return(returnResult);
        }
Example #2
0
        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))
                {
                    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));
        }