// Internal for testing
        internal static bool AtMarkupTransitionCompletionPoint(RazorSyntaxNode owner)
        {
            /* Only provide IntelliSense for C# code blocks, of the form:
             *  @{ }, @code{ }, @functions{ }, @if(true){ }
             *
             * Note for the `< te` and `< te=""` cases:
             * The cases are not handled by AtMarkupTransitionCompletionPoint but
             * rather by the HtmlFactsService which purposely prohibits the completion
             * when it's unable to extract the tag contents. This ensures we aren't
             * providing incorrect completion in the above two syntactically invalid
             * scenarios.
             */
            var encapsulatingMarkupElementNodeSeen = false;

            foreach (var ancestor in owner.Ancestors())
            {
                if (ancestor is MarkupElementSyntax markupNode)
                {
                    if (encapsulatingMarkupElementNodeSeen)
                    {
                        return(false);
                    }

                    encapsulatingMarkupElementNodeSeen = true;
                }

                if (ancestor is CSharpCodeBlockSyntax)
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        // Internal for testing
        internal static bool AtMarkupTransitionCompletionPoint(RazorSyntaxNode owner)
        {
            /* Only provide IntelliSense for C# code blocks, of the form:
             *  @{ }, @code{ }, @functions{ }, @if(true){ }
             *
             * Note for the `< te` and `< te=""` cases:
             * The cases are not handled by AtMarkupTransitionCompletionPoint but
             * rather by the HtmlFactsService which purposely prohibits the completion
             * when it's unable to extract the tag contents. This ensures we aren't
             * providing incorrect completion in the above two syntactically invalid
             * scenarios.
             */
            var closestSignificantAncestor = owner.Ancestors().FirstOrDefault(node =>
            {
                if (node is MarkupElementSyntax markupNode && markupNode.ChildNodes().Count != 1)
                {
                    return(true);
                }

                if (node is CSharpCodeBlockSyntax)
                {
                    return(true);
                }

                return(false);
            });

            return(closestSignificantAncestor is CSharpCodeBlockSyntax);
        }
            public SyntaxResult(SyntaxNode node, SyntaxKind kind, RazorCodeDocument razorCodeDocument)
            {
                var range = node.GetRange(razorCodeDocument.Source);

                Range = range;
                Kind  = kind;
            }
        // Internal for testing
        internal static bool TryGetElementInfo(RazorSyntaxNode element, out string containingTagName, out IEnumerable <string> attributeNames)
        {
            if (element is MarkupStartTagSyntax startTag)
            {
                containingTagName = startTag.Name.GetContent();
                attributeNames    = ExtractAttributeNames(startTag.Attributes);
                return(true);
            }

            if (element is MarkupTagHelperStartTagSyntax startTagHelper)
            {
                containingTagName = startTagHelper.Name.GetContent();
                attributeNames    = ExtractAttributeNames(startTagHelper.Attributes);
                return(true);
            }

            containingTagName = null;
            attributeNames    = default;
            return(false);
        }
        // Internal for testing
        internal static bool TryGetAttributeInfo(
            RazorSyntaxNode attributeLeafOwner,
            out TextSpan?prefixLocation,
            out string attributeName,
            out TextSpan attributeNameLocation,
            out string parameterName,
            out TextSpan parameterLocation)
        {
            var attribute = attributeLeafOwner.Parent;

            // The null check on the `NamePrefix` field is required for cases like:
            // `<svg xml:base=""x| ></svg>` where there's no `NamePrefix` available.
            switch (attribute)
            {
            case MarkupMinimizedAttributeBlockSyntax minimizedMarkupAttribute:
                prefixLocation = minimizedMarkupAttribute.NamePrefix?.Span;
                TryExtractIncompleteDirectiveAttribute(
                    minimizedMarkupAttribute.Name.GetContent(),
                    minimizedMarkupAttribute.Name.Span,
                    out attributeName,
                    out attributeNameLocation,
                    out parameterName,
                    out parameterLocation);

                return(true);

            case MarkupAttributeBlockSyntax markupAttribute:
                prefixLocation = markupAttribute.NamePrefix?.Span;
                TryExtractIncompleteDirectiveAttribute(
                    markupAttribute.Name.GetContent(),
                    markupAttribute.Name.Span,
                    out attributeName,
                    out attributeNameLocation,
                    out parameterName,
                    out parameterLocation);
                return(true);

            case MarkupMinimizedTagHelperAttributeSyntax minimizedTagHelperAttribute:
                prefixLocation = minimizedTagHelperAttribute.NamePrefix?.Span;
                TryExtractIncompleteDirectiveAttribute(
                    minimizedTagHelperAttribute.Name.GetContent(),
                    minimizedTagHelperAttribute.Name.Span,
                    out attributeName,
                    out attributeNameLocation,
                    out parameterName,
                    out parameterLocation);
                return(true);

            case MarkupTagHelperAttributeSyntax tagHelperAttribute:
                prefixLocation = tagHelperAttribute.NamePrefix?.Span;
                TryExtractIncompleteDirectiveAttribute(
                    tagHelperAttribute.Name.GetContent(),
                    tagHelperAttribute.Name.Span,
                    out attributeName,
                    out attributeNameLocation,
                    out parameterName,
                    out parameterLocation);
                return(true);

            case MarkupTagHelperDirectiveAttributeSyntax directiveAttribute:
            {
                var attributeNameNode            = directiveAttribute.Name;
                var directiveAttributeTransition = directiveAttribute.Transition;
                var nameStart = directiveAttributeTransition?.SpanStart ?? attributeNameNode.SpanStart;
                var nameEnd   = attributeNameNode?.Span.End ?? directiveAttributeTransition.Span.End;
                prefixLocation        = directiveAttribute.NamePrefix?.Span;
                attributeName         = string.Concat(directiveAttributeTransition?.GetContent(), attributeNameNode?.GetContent());
                attributeNameLocation = new TextSpan(nameStart, nameEnd - nameStart);
                parameterName         = directiveAttribute.ParameterName?.GetContent();
                parameterLocation     = directiveAttribute.ParameterName?.Span ?? default;
                return(true);
            }

            case MarkupMinimizedTagHelperDirectiveAttributeSyntax minimizedDirectiveAttribute:
            {
                var attributeNameNode            = minimizedDirectiveAttribute.Name;
                var directiveAttributeTransition = minimizedDirectiveAttribute.Transition;
                var nameStart = directiveAttributeTransition?.SpanStart ?? attributeNameNode.SpanStart;
                var nameEnd   = attributeNameNode?.Span.End ?? directiveAttributeTransition.Span.End;
                prefixLocation        = minimizedDirectiveAttribute.NamePrefix?.Span;
                attributeName         = string.Concat(directiveAttributeTransition?.GetContent(), attributeNameNode?.GetContent());
                attributeNameLocation = new TextSpan(nameStart, nameEnd - nameStart);
                parameterName         = minimizedDirectiveAttribute.ParameterName?.GetContent();
                parameterLocation     = minimizedDirectiveAttribute.ParameterName?.Span ?? default;
                return(true);
            }
            }

            prefixLocation        = default;
            attributeName         = null;
            attributeNameLocation = default;
            parameterName         = null;
            parameterLocation     = default;
            return(false);
        }
        // Internal for testing
        internal static bool TryGetAttributeInfo(
            RazorSyntaxNode attributeLeafOwner,
            out TextSpan prefixLocation,
            out string name,
            out TextSpan nameLocation,
            out string parameterName,
            out TextSpan parameterLocation)
        {
            var attribute = attributeLeafOwner.Parent;

            switch (attribute)
            {
            case MarkupMinimizedAttributeBlockSyntax minimizedMarkupAttribute:
                prefixLocation = minimizedMarkupAttribute.NamePrefix.Span;
                TryExtractIncompleteDirectiveAttribute(
                    minimizedMarkupAttribute.Name.GetContent(),
                    minimizedMarkupAttribute.Name.Span,
                    out name,
                    out nameLocation,
                    out parameterName,
                    out parameterLocation);

                return(true);

            case MarkupAttributeBlockSyntax markupAttribute:
                prefixLocation = markupAttribute.NamePrefix.Span;
                TryExtractIncompleteDirectiveAttribute(
                    markupAttribute.Name.GetContent(),
                    markupAttribute.Name.Span,
                    out name,
                    out nameLocation,
                    out parameterName,
                    out parameterLocation);
                return(true);

            case MarkupMinimizedTagHelperAttributeSyntax minimizedTagHelperAttribute:
                prefixLocation = minimizedTagHelperAttribute.NamePrefix.Span;
                TryExtractIncompleteDirectiveAttribute(
                    minimizedTagHelperAttribute.Name.GetContent(),
                    minimizedTagHelperAttribute.Name.Span,
                    out name,
                    out nameLocation,
                    out parameterName,
                    out parameterLocation);
                return(true);

            case MarkupTagHelperAttributeSyntax tagHelperAttribute:
                prefixLocation = tagHelperAttribute.NamePrefix.Span;
                TryExtractIncompleteDirectiveAttribute(
                    tagHelperAttribute.Name.GetContent(),
                    tagHelperAttribute.Name.Span,
                    out name,
                    out nameLocation,
                    out parameterName,
                    out parameterLocation);
                return(true);

            case MarkupTagHelperDirectiveAttributeSyntax directiveAttribute:
            {
                var attributeName = directiveAttribute.Name;
                var directiveAttributeTransition = directiveAttribute.Transition;
                var nameStart = directiveAttributeTransition?.SpanStart ?? attributeName.SpanStart;
                var nameEnd   = attributeName?.Span.End ?? directiveAttributeTransition.Span.End;
                prefixLocation    = directiveAttribute.NamePrefix.Span;
                name              = string.Concat(directiveAttributeTransition?.GetContent(), attributeName?.GetContent());
                nameLocation      = new TextSpan(nameStart, nameEnd - nameStart);
                parameterName     = directiveAttribute.ParameterName?.GetContent();
                parameterLocation = directiveAttribute.ParameterName?.Span ?? default;
                return(true);
            }

            case MarkupMinimizedTagHelperDirectiveAttributeSyntax minimizedDirectiveAttribute:
            {
                var attributeName = minimizedDirectiveAttribute.Name;
                var directiveAttributeTransition = minimizedDirectiveAttribute.Transition;
                var nameStart = directiveAttributeTransition?.SpanStart ?? attributeName.SpanStart;
                var nameEnd   = attributeName?.Span.End ?? directiveAttributeTransition.Span.End;
                prefixLocation    = minimizedDirectiveAttribute.NamePrefix.Span;
                name              = string.Concat(directiveAttributeTransition?.GetContent(), attributeName?.GetContent());
                nameLocation      = new TextSpan(nameStart, nameEnd - nameStart);
                parameterName     = minimizedDirectiveAttribute.ParameterName?.GetContent();
                parameterLocation = minimizedDirectiveAttribute.ParameterName?.Span ?? default;
                return(true);
            }
            }

            prefixLocation    = default;
            name              = null;
            nameLocation      = default;
            parameterName     = null;
            parameterLocation = default;
            return(false);
        }
Ejemplo n.º 7
0
 public SyntaxNode(GreenNode green, SyntaxNode parent, int position)
 {
     Green    = green;
     Parent   = parent;
     Position = position;
 }