Exemple #1
0
        public override SignatureHelpState GetCurrentArgumentState(SyntaxNode root, int position, ISyntaxFactsService syntaxFacts, TextSpan currentSpan, CancellationToken cancellationToken)
        {
            ExpressionSyntax expression;
            SyntaxToken      openBracket;

            if (!TryGetElementAccessExpression(
                    root,
                    position,
                    syntaxFacts,
                    SignatureHelpTriggerReason.InvokeSignatureHelpCommand,
                    cancellationToken,
                    out expression,
                    out openBracket) ||
                currentSpan.Start != expression.SpanStart)
            {
                return(null);
            }

            ElementAccessExpressionSyntax elementAccessExpression = SyntaxFactory.ElementAccessExpression(
                expression,
                SyntaxFactory.ParseBracketedArgumentList(openBracket.Parent.ToString()));

            // Because we synthesized the elementAccessExpression, it will have an index starting at 0
            // instead of at the actual position it's at in the text.  Because of this, we need to
            // offset the position we are checking accordingly.
            var offset = expression.SpanStart - elementAccessExpression.SpanStart;

            position -= offset;
            return(SignatureHelpUtilities.GetSignatureHelpState(elementAccessExpression.ArgumentList, position));
        }
        protected override async Task <SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            ConstructorInitializerSyntax constructorInitializer;

            if (!TryGetConstructorInitializer(root, position, document.GetLanguageService <ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out constructorInitializer))
            {
                return(null);
            }

            var semanticModel = await document.GetCSharpSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var within = semanticModel.GetEnclosingNamedType(position, cancellationToken);

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

            if (within.TypeKind != TypeKind.Struct && within.TypeKind != TypeKind.Class)
            {
                return(null);
            }

            var type = constructorInitializer.Kind() == SyntaxKind.BaseConstructorInitializer
                ? within.BaseType
                : within;

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

            var symbolDisplayService   = document.Project.LanguageServices.GetService <ISymbolDisplayService>();
            var accessibleConstructors = type.InstanceConstructors
                                         .Where(c => c.IsAccessibleWithin(within))
                                         .Where(c => c.IsEditorBrowsable(document.ShouldHideAdvancedMembers(), semanticModel.Compilation))
                                         .Sort(symbolDisplayService, semanticModel, constructorInitializer.SpanStart);

            if (!accessibleConstructors.Any())
            {
                return(null);
            }

            var anonymousTypeDisplayService           = document.Project.LanguageServices.GetService <IAnonymousTypeDisplayService>();
            var documentationCommentFormattingService = document.Project.LanguageServices.GetService <IDocumentationCommentFormattingService>();
            var textSpan    = SignatureHelpUtilities.GetSignatureHelpSpan(constructorInitializer.ArgumentList);
            var syntaxFacts = document.GetLanguageService <ISyntaxFactsService>();

            return(CreateSignatureHelpItems(accessibleConstructors.Select(c =>
                                                                          Convert(c, constructorInitializer.ArgumentList.OpenParenToken, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, cancellationToken)),
                                            textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken)));
        }
Exemple #3
0
        public override SignatureHelpState GetCurrentArgumentState(SyntaxNode root, int position, ISyntaxFactsService syntaxFacts, TextSpan currentSpan, CancellationToken cancellationToken)
        {
            AttributeSyntax expression;

            if (TryGetAttributeExpression(root, position, syntaxFacts, SignatureHelpTriggerReason.InvokeSignatureHelpCommand, cancellationToken, out expression) &&
                currentSpan.Start == SignatureHelpUtilities.GetSignatureHelpSpan(expression.ArgumentList).Start)
            {
                return(SignatureHelpUtilities.GetSignatureHelpState(expression.ArgumentList, position));
            }

            return(null);
        }
        protected override async Task <SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            InvocationExpressionSyntax invocationExpression;

            if (!TryGetInvocationExpression(root, position, document.GetLanguageService <ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out invocationExpression))
            {
                return(null);
            }

            var semanticModel = await document.GetSemanticModelForNodeAsync(invocationExpression, cancellationToken).ConfigureAwait(false);

            var within = semanticModel.GetEnclosingNamedTypeOrAssembly(position, cancellationToken);

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

            var symbolDisplayService = document.Project.LanguageServices.GetService <ISymbolDisplayService>();
            var methodGroup          = semanticModel.GetMemberGroup(invocationExpression.Expression, cancellationToken)
                                       .OfType <IMethodSymbol>()
                                       .FilterToVisibleAndBrowsableSymbols(document.ShouldHideAdvancedMembers(), semanticModel.Compilation)
                                       .Sort(symbolDisplayService, semanticModel, invocationExpression.SpanStart);

            var expressionType = semanticModel.GetTypeInfo(invocationExpression.Expression, cancellationToken).Type as INamedTypeSymbol;

            var anonymousTypeDisplayService           = document.Project.LanguageServices.GetService <IAnonymousTypeDisplayService>();
            var documentationCommentFormattingService = document.Project.LanguageServices.GetService <IDocumentationCommentFormattingService>();

            var textSpan    = SignatureHelpUtilities.GetSignatureHelpSpan(invocationExpression.ArgumentList);
            var syntaxFacts = document.GetLanguageService <ISyntaxFactsService>();

            if (methodGroup.Any())
            {
                return(CreateSignatureHelpItems(
                           GetMethodGroupItems(invocationExpression, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, within, methodGroup, cancellationToken),
                           textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken)));
            }
            else if (expressionType != null && expressionType.TypeKind == TypeKind.Delegate)
            {
                return(CreateSignatureHelpItems(
                           GetDelegateInvokeItems(invocationExpression, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, within, expressionType, cancellationToken),
                           textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken)));
            }
            else
            {
                return(null);
            }
        }
Exemple #5
0
        public override SignatureHelpState GetCurrentArgumentState(SyntaxNode root, int position, ISyntaxFactsService syntaxFacts, TextSpan currentSpan, CancellationToken cancellationToken)
        {
            ExpressionSyntax expression;
            SyntaxToken      openBracket;

            if (!TryGetElementAccessExpression(
                    root,
                    position,
                    syntaxFacts,
                    SignatureHelpTriggerReason.InvokeSignatureHelpCommand,
                    cancellationToken,
                    out expression,
                    out openBracket) ||
                currentSpan.Start != expression.SpanStart)
            {
                return(null);
            }

            // If the user is actively typing, it's likely that we're in a broken state and the
            // syntax tree will be incorrect.  Because of this we need to synthesize a new
            // bracketed argument list so we can correctly map the cursor to the current argument
            // and then we need to account for this and offset the position check accordingly.
            int offset;
            BracketedArgumentListSyntax argumentList;
            var newBracketedArgumentList = SyntaxFactory.ParseBracketedArgumentList(openBracket.Parent.ToString());

            if (expression.Parent is ConditionalAccessExpressionSyntax)
            {
                // The typed code looks like: <expression>?[
                var conditional    = (ConditionalAccessExpressionSyntax)expression.Parent;
                var elementBinding = SyntaxFactory.ElementBindingExpression(newBracketedArgumentList);
                var conditionalAccessExpression = SyntaxFactory.ConditionalAccessExpression(expression, elementBinding);
                offset       = expression.SpanStart - conditionalAccessExpression.SpanStart;
                argumentList = ((ElementBindingExpressionSyntax)conditionalAccessExpression.WhenNotNull).ArgumentList;
            }
            else
            {
                // The typed code looks like:
                //   <expression>[
                // or
                //   <identifier>?[
                ElementAccessExpressionSyntax elementAccessExpression = SyntaxFactory.ElementAccessExpression(expression, newBracketedArgumentList);
                offset       = expression.SpanStart - elementAccessExpression.SpanStart;
                argumentList = elementAccessExpression.ArgumentList;
            }

            position -= offset;
            return(SignatureHelpUtilities.GetSignatureHelpState(argumentList, position));
        }
Exemple #6
0
        protected override async Task <SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            AttributeSyntax attribute;

            if (!TryGetAttributeExpression(root, position, document.GetLanguageService <ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out attribute))
            {
                return(null);
            }

            var semanticModel = await document.GetSemanticModelForNodeAsync(attribute, cancellationToken).ConfigureAwait(false);

            var attributeType = semanticModel.GetTypeInfo(attribute, cancellationToken).Type as INamedTypeSymbol;

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

            var within = semanticModel.GetEnclosingNamedTypeOrAssembly(position, cancellationToken);

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

            var symbolDisplayService   = document.Project.LanguageServices.GetService <ISymbolDisplayService>();
            var accessibleConstructors = attributeType.InstanceConstructors
                                         .Where(c => c.IsAccessibleWithin(within))
                                         .FilterToVisibleAndBrowsableSymbols(document.ShouldHideAdvancedMembers(), semanticModel.Compilation)
                                         .Sort(symbolDisplayService, semanticModel, attribute.SpanStart);

            if (!accessibleConstructors.Any())
            {
                return(null);
            }

            var anonymousTypeDisplayService   = document.Project.LanguageServices.GetService <IAnonymousTypeDisplayService>();
            var documentationCommentFormatter = document.Project.LanguageServices.GetService <IDocumentationCommentFormattingService>();
            var textSpan = SignatureHelpUtilities.GetSignatureHelpSpan(attribute.ArgumentList);

            var syntaxFacts = document.GetLanguageService <ISyntaxFactsService>();

            return(CreateSignatureHelpItems(accessibleConstructors.Select(c =>
                                                                          Convert(c, within, attribute, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormatter, cancellationToken)),
                                            textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken)));
        }
        protected override async Task <SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            ObjectCreationExpressionSyntax objectCreationExpression;

            if (!TryGetObjectCreationExpression(root, position, document.GetLanguageService <ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out objectCreationExpression))
            {
                return(null);
            }

            var semanticModel = await document.GetSemanticModelForNodeAsync(objectCreationExpression, cancellationToken).ConfigureAwait(false);

            var type = semanticModel.GetTypeInfo(objectCreationExpression, cancellationToken).Type as INamedTypeSymbol;

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

            var within = semanticModel.GetEnclosingNamedType(position, cancellationToken);

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

            var symbolDisplayService                  = document.Project.LanguageServices.GetService <ISymbolDisplayService>();
            var anonymousTypeDisplayService           = document.Project.LanguageServices.GetService <IAnonymousTypeDisplayService>();
            var documentationCommentFormattingService = document.Project.LanguageServices.GetService <IDocumentationCommentFormattingService>();
            var textSpan    = SignatureHelpUtilities.GetSignatureHelpSpan(objectCreationExpression.ArgumentList);
            var syntaxFacts = document.GetLanguageService <ISyntaxFactsService>();

            return(CreateSignatureHelpItems(type.TypeKind == TypeKind.Delegate
                ? GetDelegateTypeConstructors(objectCreationExpression, semanticModel, symbolDisplayService, anonymousTypeDisplayService, type, within, cancellationToken)
                : GetNormalTypeConstructors(document, objectCreationExpression, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, type, within, cancellationToken),
                                            textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken)));
        }
        public override SignatureHelpState GetCurrentArgumentState(SyntaxNode root, int position, ISyntaxFactsService syntaxFacts, TextSpan currentSpan, CancellationToken cancellationToken)
        {
            SyntaxToken genericIdentifier, lessThanToken;

            if (!TryGetGenericIdentifier(root, position, syntaxFacts, SignatureHelpTriggerReason.InvokeSignatureHelpCommand, cancellationToken,
                                         out genericIdentifier, out lessThanToken))
            {
                return(null);
            }

            GenericNameSyntax genericName;

            if (genericIdentifier.TryParseGenericName(cancellationToken, out genericName))
            {
                // Because we synthesized the generic name, it will have an index starting at 0
                // instead of at the actual position it's at in the text.  Because of this, we need to
                // offset the position we are checking accordingly.
                var offset = genericIdentifier.SpanStart - genericName.SpanStart;
                position -= offset;
                return(SignatureHelpUtilities.GetSignatureHelpState(genericName.TypeArgumentList, position));
            }

            return(null);
        }
 protected virtual TextSpan GetTextSpan(SyntaxToken genericIdentifier, SyntaxToken lessThanToken)
 {
     Contract.ThrowIfFalse(lessThanToken.Parent is TypeArgumentListSyntax && lessThanToken.Parent.Parent is GenericNameSyntax);
     return(SignatureHelpUtilities.GetSignatureHelpSpan(((GenericNameSyntax)lessThanToken.Parent.Parent).TypeArgumentList));
 }
Exemple #10
0
 internal static TextSpan GetTextSpan(SyntaxNode expression, SyntaxToken openBracket)
 {
     Contract.ThrowIfFalse(openBracket.Parent is BracketedArgumentListSyntax &&
                           (openBracket.Parent.Parent is ElementAccessExpressionSyntax || openBracket.Parent.Parent is ElementBindingExpressionSyntax));
     return(SignatureHelpUtilities.GetSignatureHelpSpan((BracketedArgumentListSyntax)openBracket.Parent));
 }