Exemple #1
0
        private CodeCompletionKeyPressResult?CompleteAssigment(ITextEditor editor, char ch, CSharpExpressionFinder ef)
        {
            CodeCompletionKeyPressResult?returnResult = null;
            string documentText = editor.Document.Text;
            int    position     = editor.Caret.Offset - 2;

            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.Document.Text, documentText, ProjectContent);
                    if (resolveResult != null && resolveResult.ResolvedType != null)
                    {
                        if (ProvideContextCompletion(editor, resolveResult.ResolvedType, ch))
                        {
                            returnResult = CodeCompletionKeyPressResult.Completed;
                        }
                    }
                }
            }
            return(returnResult);
        }
Exemple #2
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);
        }
        public IInsightItem[] ProvideInsight(ITextEditor editor)
        {
            if (editor == null)
            {
                throw new ArgumentNullException("editor");
            }

            IExpressionFinder expressionFinder = ParserService.GetExpressionFinder(editor.Document.Text, _projectContent);

            if (expressionFinder == null)
            {
                return(null);
            }

            IDocument        document         = editor.Document;
            int              useOffset        = (this.LookupOffset < 0) ? editor.Caret.Offset : this.LookupOffset;
            ExpressionResult expressionResult = expressionFinder.FindExpression(document.Text, useOffset);

            if (expressionResult.Expression == null) // expression is null when cursor is in string/comment
            {
                return(null);
            }

            var           position = document.OffsetToPosition(useOffset);
            ResolveResult rr       = ParserService.Resolve(expressionResult, position.Line, position.Column, editor.Document.Text, document.Text, _projectContent);

            return(ProvideInsight(expressionResult, rr));
        }
        IList <ResolveResult> ResolveCallParameters(ITextEditor editor, InspectedCall call)
        {
            List <ResolveResult> rr = new List <ResolveResult>();
            int    offset           = LocationToOffset(editor, call.start);
            string documentText     = editor.Document.Text;
            int    newOffset;

            foreach (Location loc in call.commas)
            {
                newOffset = LocationToOffset(editor, loc);
                if (newOffset < 0)
                {
                    break;
                }
                string text = editor.Document.GetText(offset + 1, newOffset - (offset + 1));
                rr.Add(ParserService.Resolve(new ExpressionResult(text), loc.Line, loc.Column, editor.Document.Text, documentText, _projectContent));
            }
            // the last argument is between the last comma and the caret position
            newOffset = editor.Caret.Offset;
            if (offset < newOffset)
            {
                string text = editor.Document.GetText(offset + 1, newOffset - (offset + 1));
                rr.Add(ParserService.Resolve(new ExpressionResult(text), editor.Caret.Line, editor.Caret.Column, editor.Document.Text, documentText, _projectContent));
            }
            return(rr);
        }
        private object GetParameter()
        {
            var expressionFinder = new CSharpExpressionFinder(ParserService.GetParseInformation(AssociatedObject.Text, ProjectContent));
            var caret            = AssociatedObject.TextArea.Caret;
            var result           = expressionFinder.FindFullExpression(AssociatedObject.Text, caret.Offset);

            var resolveResult = ParserService.Resolve(result, caret.Line, caret.Column, AssociatedObject.Text, ProjectContent);

            if (resolveResult == null || !resolveResult.IsValid)
            {
                return(null);
            }

            if (resolveResult is MemberResolveResult)
            {
                return(((MemberResolveResult)resolveResult).ResolvedMember);
            }
            if (resolveResult is TypeResolveResult)
            {
                return(((TypeResolveResult)resolveResult).ResolvedClass);
            }
            if (resolveResult is NamespaceResolveResult)
            {
                return(new NamespaceEntry(((NamespaceResolveResult)resolveResult).Name));
            }
            return(null);
        }
Exemple #6
0
        ExpressionContext FindExactContextForNewCompletion(ITextEditor editor, string documentToCursor,
                                                           IDocumentLine currentLine, int pos)
        {
            CSharpExpressionFinder ef = CreateExpressionFinder(editor.Document.Text);
            // 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.Document.Text, editor.Document.Text, ProjectContent);
                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.Document.Text, ProjectContent), editor.Caret.Line, editor.Caret.Column)
                                ), "");
                        context.SuggestedItem = suggestedClassName != c.Name
                            ? new SuggestedCodeCompletionItem(c, suggestedClassName, ProjectContent)
                            : new CodeCompletionItem(c, ProjectContent);
                    }
                    return(context);
                }
            }
            return(null);
        }