Exemplo n.º 1
0
        bool ShowNewCompletion(ITextEditor editor)
        {
            CSharpExpressionFinder ef         = CreateExpressionFinder(editor.Document.Text);
            int              cursor           = editor.Caret.Offset;
            string           documentToCursor = editor.Document.GetText(0, cursor);
            ExpressionResult expressionResult = ef.FindExpression(documentToCursor, cursor);

            if (expressionResult.Context.IsObjectCreation)
            {
                var    currentLine = editor.Document.GetLineForOffset(cursor);
                string lineText    = editor.Document.GetText(currentLine.Offset, cursor - currentLine.Offset);
                // when the new follows an assignment, improve code-completion by detecting the
                // type of the variable that is assigned to
                if (lineText.Replace(" ", "").EndsWith("=new"))
                {
                    int pos = lineText.LastIndexOf('=');
                    ExpressionContext context = FindExactContextForNewCompletion(editor, documentToCursor,
                                                                                 currentLine, pos);
                    if (context != null)
                    {
                        expressionResult.Context = context;
                    }
                }
                ShowCompletion(new NRefactoryCtrlSpaceCompletionItemProvider(LanguageProperties.CSharp, expressionResult.Context, ProjectContent), editor, ProjectContent);
                return(true);
            }
            return(false);
        }
        bool ShowNewCompletion(SharpDevelopTextAreaControl editor)
        {
            CSharpExpressionFinder ef         = CreateExpressionFinder(editor.FileName);
            int              cursor           = editor.ActiveTextAreaControl.Caret.Offset;
            string           documentToCursor = editor.Document.GetText(0, cursor);
            ExpressionResult expressionResult = ef.FindExpression(documentToCursor, cursor);

            LoggingService.Debug("ShowNewCompletion: expression is " + expressionResult);
            if (expressionResult.Context.IsObjectCreation)
            {
                LineSegment currentLine = editor.Document.GetLineSegmentForOffset(cursor);
                string      lineText    = editor.Document.GetText(currentLine.Offset, cursor - currentLine.Offset);
                // when the new follows an assignment, improve code-completion by detecting the
                // type of the variable that is assigned to
                if (lineText.Replace(" ", "").EndsWith("=new"))
                {
                    int pos = lineText.LastIndexOf('=');
                    ExpressionContext context = FindExactContextForNewCompletion(editor, documentToCursor,
                                                                                 currentLine, pos);
                    if (context != null)
                    {
                        expressionResult.Context = context;
                    }
                }
                editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(expressionResult.Context), ' ');
                return(true);
            }
            return(false);
        }
        ExpressionContext FindExactContextForNewCompletion(SharpDevelopTextAreaControl editor, string documentToCursor,
                                                           LineSegment currentLine, int pos)
        {
            CSharpExpressionFinder ef = CreateExpressionFinder(editor.FileName);
            // find expression on left hand side of the assignment
            ExpressionResult lhsExpr = ef.FindExpression(documentToCursor, currentLine.Offset + pos);

            if (lhsExpr.Expression != null)
            {
                ResolveResult rr = ParserService.Resolve(lhsExpr, currentLine.LineNumber, pos, editor.FileName, editor.Text);
                if (rr != null && rr.ResolvedType != null)
                {
                    ExpressionContext context;
                    IClass            c;
                    if (rr.ResolvedType.IsArrayReturnType)
                    {
                        // when creating an array, all classes deriving from the array's element type are allowed
                        IReturnType elementType = rr.ResolvedType.CastToArrayReturnType().ArrayElementType;
                        c = elementType != null?elementType.GetUnderlyingClass() : null;

                        context = ExpressionContext.TypeDerivingFrom(elementType, false);
                    }
                    else
                    {
                        // when creating a normal instance, all non-abstract classes deriving from the type
                        // are allowed
                        c       = rr.ResolvedType.GetUnderlyingClass();
                        context = ExpressionContext.TypeDerivingFrom(rr.ResolvedType, true);
                    }
                    if (c != null && context.ShowEntry(c))
                    {
                        // Try to suggest an entry (List<int> a = new => suggest List<int>).

                        string suggestedClassName = LanguageProperties.CSharp.CodeGenerator.GenerateCode(
                            CodeGenerator.ConvertType(
                                rr.ResolvedType,
                                new ClassFinder(ParserService.GetParseInformation(editor.FileName), editor.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1)
                                ), "");
                        if (suggestedClassName != c.Name)
                        {
                            // create an IClass instance that includes the type arguments in its name
                            context.SuggestedItem = new RenamedClass(c, suggestedClassName);
                        }
                        else
                        {
                            context.SuggestedItem = c;
                        }
                    }
                    return(context);
                }
            }
            return(null);
        }
Exemplo n.º 4
0
        public ExpressionResult getExpressionFromCodeSnippet(TextArea textArea)
        {
            IExpressionFinder finder = new CSharpExpressionFinder(this.parseInformation);
            var firstMethodOffset    = calculateFirstMethodOffset();
            var adjustedSnippeetText = getAdjustedSnippetText(textArea, firstMethodOffset);

            var offset = firstMethodOffset + textArea.Caret.Offset;

            var expression = finder.FindExpression(adjustedSnippeetText, offset);

            return(expression);
        }
Exemplo n.º 5
0
        private bool ShowNewCompletion(CodeEditorControl editor)
        {
            CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.ActiveViewControl.FileName);
            int cursor = editor.ActiveViewControl.Caret.Offset;
            ExpressionContext context = ef.FindExpression(ScriptControl.Parser.ProjectParser.GetFileContents(editor.FileName).Substring(0, cursor) + " T.", cursor + 2).Context;

            if (context.IsObjectCreation)
            {
                editor.ActiveViewControl.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ' ');
                return(true);
            }
            return(false);
        }
Exemplo n.º 6
0
        bool ShowNewCompletion(SharpDevelopTextAreaControl editor)
        {
            CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.FileName);
            int cursor = editor.ActiveTextAreaControl.Caret.Offset;
            ExpressionContext context = ef.FindExpression(editor.Document.GetText(0, cursor) + " T.", cursor + 2).Context;

            if (context.IsObjectCreation)
            {
                editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ' ');
                return(true);
            }
            return(false);
        }
Exemplo n.º 7
0
        public override CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch, IProjectContent projectContent)
        {
            CSharpExpressionFinder ef = CreateExpressionFinder(editor.Document.Text);
            int cursor = editor.Caret.Offset;
            CodeCompletionKeyPressResult?returnResult = null;

            switch (ch)
            {
            case '[':
            {
                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;
                 * }*/
            }
            break;

            case ' ':
                returnResult = CompleteSpace(editor, ef);
                break;

            case ',':
                returnResult = CompleteComma(editor, ch, projectContent);
                break;

            case '=':
                returnResult = CompleteAssigment(editor, ch, ef);
                break;

            case '.':
                returnResult = ShowDotCompletion(editor);
                break;

            case '>':
            {
                if (IsInComment(editor))
                {
                    returnResult = CodeCompletionKeyPressResult.None;
                    break;
                }
                returnResult = CompleteMore(editor, cursor, projectContent);
            }
            break;
            }

            if (returnResult.HasValue)
            {
                return(returnResult.Value);
            }

            if (char.IsLetter(ch) || ch == '_')
            {
                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;
                }
                var prevChar        = GetPreviousChar(editor, cursor);
                var afterUnderscore = prevChar == '_';
                if (afterUnderscore)
                {
                    cursor--;
                    prevChar = GetPreviousChar(editor, cursor);
                }

                if (!char.IsLetterOrDigit(prevChar) && prevChar != '.' && !IsInComment(editor))
                {
                    var result = ef.FindExpression(editor.Document.Text, cursor);
                    if (result.Context != ExpressionContext.IdentifierExpected)
                    {
                        var ctrlSpaceProvider = new NRefactoryCtrlSpaceCompletionItemProvider(
                            LanguageProperties.CSharp, result.Context, ProjectContent)
                        {
                            ShowTemplates = true,
                            AllowCompleteExistingExpression = afterUnderscore
                        };
                        ShowCompletion(ctrlSpaceProvider, editor, projectContent);
                        return(CodeCompletionKeyPressResult.CompletedIncludeKeyInCompletion);
                    }
                }
            }
            return(base.HandleKeyPress(editor, ch, projectContent));
        }
        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));
        }
        void FindExpr(string program, string location, string expectedExpression, ExpressionContext expectedContext)
        {
            int pos = program.IndexOf(location);

            if (pos < 0)
            {
                Assert.Fail("location not found in program");
            }
            ExpressionResult er = ef.FindExpression(program, pos);

            Assert.AreEqual(expectedExpression, er.Expression);
            Assert.AreEqual(expectedContext, er.Context);
            Assert.AreEqual(expectedExpression, ExtractRegion(program, er.Region));
        }
Exemplo n.º 10
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));
        }
Exemplo n.º 11
0
        public ExpressionResult getExpressionFromTextArea(TextArea textArea)
        {
            IExpressionFinder finder = new CSharpExpressionFinder(this.parseInformation);

            return(finder.FindExpression(textArea.Document.TextContent, textArea.Caret.Offset));
        }