Exemple #1
0
        private void AddNode(ItemCollection items, SyntaxNodeBase node, string prefix)
        {
            var syntaxFactsService = GetSyntaxFactsService();

            var tooltip      = string.Join(Environment.NewLine, node.GetDiagnostics().Select(x => x.ToString()));
            var treeViewItem = new TreeViewItem
            {
                Background = (node.ContainsDiagnostics) ? Brushes.Pink : Brushes.Transparent,
                Foreground = (node.IsToken) ? Brushes.DarkGreen : (node.Ancestors().Any(a => a.IsToken) ? Brushes.DarkRed : Brushes.Blue),
                Header     = $"{prefix}{syntaxFactsService.GetKindText(node.RawKind)} [{node.SourceRange.Start}..{node.SourceRange.End})",
                ToolTip    = string.IsNullOrEmpty(tooltip) ? null : tooltip,
                Tag        = node
            };

            foreach (var childNode in node.ChildNodes)
            {
                AddNode(treeViewItem.Items, childNode, string.Empty);
            }

            if (node.IsToken)
            {
                var token = (ISyntaxToken)node;
                foreach (var childNode in token.LeadingTrivia)
                {
                    AddNode(treeViewItem.Items, childNode, "Lead: ");
                }
                foreach (var childNode in token.TrailingTrivia)
                {
                    AddNode(treeViewItem.Items, childNode, "Trail: ");
                }
            }

            items.Add(treeViewItem);
        }
        public bool ShouldIndent(SyntaxNodeBase nodeBase)
        {
            var node = (SyntaxNode)nodeBase;

            if (node.Kind == SyntaxKind.ElseClause)
            {
                return(false);
            }

            switch (node.Parent.Kind)
            {
            case SyntaxKind.Block:
                return(true);

            case SyntaxKind.IfStatement:
            case SyntaxKind.DoStatement:
            case SyntaxKind.WhileStatement:
            case SyntaxKind.ForStatement:
            case SyntaxKind.SwitchStatement:
                return(node.Kind != SyntaxKind.Block);

            case SyntaxKind.Namespace:
            case SyntaxKind.ConstantBufferDeclaration:
            case SyntaxKind.ClassType:
            case SyntaxKind.StructType:
            case SyntaxKind.InterfaceType:
            case SyntaxKind.ArrayInitializerExpression:
                return(true);

            default:
                return(false);
            }
        }
Exemple #3
0
        private static ISymbol GetGlobalMember(SemanticModelBase semanticModel, SyntaxNodeBase node)
        {
            switch (node)
            {
            case FunctionSyntax t: return(semanticModel.GetDeclaredSymbol(t));

            case VariableDeclaratorSyntax t: return(semanticModel.GetDeclaredSymbol(t));
            }

            return(null);
        }
Exemple #4
0
        public Task <IFormattingResult> FormatAsync(SyntaxTreeBase tree, SyntaxNodeBase node, IEnumerable <TextSpan> spans, OptionSet options, CancellationToken cancellationToken)
        {
            var edits = new List <TextChange>();

            foreach (var span in spans)
            {
                edits.AddRange(Formatter.GetEdits((SyntaxTree)tree, (SyntaxNode)node, span, _optionsService.GetFormattingOptions(options)));
            }

            return(Task.FromResult((IFormattingResult) new FormattingResult(edits)));
        }
Exemple #5
0
        // From https://github.com/KirillOsenkov/XmlParser/blob/master/src/Microsoft.Language.Xml.Editor/SmartIndent/SmartIndent.cs#L39
        public static int FindTotalParentChainIndent(
            SyntaxNodeBase node,
            int position,
            int indent,
            int indentSize,
            IIndentationService indentationService,
            ISyntaxFactsService syntaxFactsService)
        {
            var textSpanOpt = syntaxFactsService.GetFileSpanRoot(node);

            if (textSpanOpt == null)
            {
                return(indent);
            }

            var textSpan = textSpanOpt.Value;

            if (!textSpan.IsInRootFile)
            {
                return(indent);
            }

            if (position < textSpan.Span.Start || position > textSpan.Span.End)
            {
                return(indent);
            }

            foreach (var child in node.ChildNodes)
            {
                var childSpan = syntaxFactsService.GetFileSpanRoot(child);
                if (childSpan == null || !childSpan.Value.IsInRootFile)
                {
                    continue;
                }

                var shouldIndent = indentationService.ShouldIndent(child);
                if (shouldIndent)
                {
                    indent += indentSize;
                }

                if (position <= childSpan.Value.Span.End)
                {
                    return(FindTotalParentChainIndent(child, position, indent, indentSize, indentationService, syntaxFactsService));
                }

                if (shouldIndent)
                {
                    indent -= indentSize;
                }
            }

            return(indent);
        }
        public override TypeInfo GetTypeInfo(SyntaxNodeBase node)
        {
            var expression = node as ExpressionSyntax;

            if (expression != null)
            {
                var expressionType = GetExpressionType(expression);
                return(expressionType != null
                    ? new TypeInfo(expressionType, expressionType)
                    : TypeInfo.None);
            }

            return(TypeInfo.None);
        }
Exemple #7
0
        private static ISymbol GetType(SemanticModelBase semanticModel, SyntaxNodeBase node)
        {
            switch (node)
            {
            case ConstantBufferSyntax t: return(semanticModel.GetDeclaredSymbol(t));

            case NamespaceSyntax t: return(semanticModel.GetDeclaredSymbol(t));

            case TechniqueSyntax t: return(semanticModel.GetDeclaredSymbol(t));

            case TypeDefinitionSyntax t: return(semanticModel.GetDeclaredSymbol(t));
            }

            return(null);
        }
        public override SymbolInfo GetSymbolInfo(SyntaxNodeBase node)
        {
            Symbol getSymbol()
            {
                var identifierDeclarationName = node as IdentifierDeclarationNameSyntax;

                if (identifierDeclarationName != null)
                {
                    return(GetSymbol(identifierDeclarationName));
                }

                var semantic = node as SemanticSyntax;

                if (semantic != null)
                {
                    return(GetSymbol(semantic));
                }

                var attribute = node as AttributeSyntax;

                if (attribute != null)
                {
                    return(GetSymbol(attribute));
                }

                var expression = node as ExpressionSyntax;

                if (expression != null)
                {
                    return(GetSymbol(expression));
                }

                return(null);
            }

            var symbol = getSymbol();

            return(symbol != null
                ? new SymbolInfo(symbol)
                : SymbolInfo.None);
        }
Exemple #9
0
 private static void AssertNodeKind(SyntaxNodeBase node, SyntaxKind kind)
 {
     Assert.Equal((ushort)kind, node.RawKind);
 }
Exemple #10
0
 private static IEnumerable <T> DescendantNodesOfType <T>(SyntaxNodeBase root)
 => root.DescendantNodes().OfType <T>();
        internal static async Task <IList <TextChange> > GetFormattedTextChangesAsync(SyntaxTreeBase tree, SyntaxNodeBase node, IEnumerable <TextSpan> spans, Workspace workspace, OptionSet options, CancellationToken cancellationToken)
        {
            if (workspace == null)
            {
                throw new ArgumentNullException(nameof(workspace));
            }

            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (spans == null)
            {
                throw new ArgumentNullException(nameof(spans));
            }

            var languageFormatter = workspace.Services.GetLanguageServices(node.Language).GetService <ISyntaxFormattingService>();

            if (languageFormatter != null)
            {
                options = options ?? workspace.Options;
                return((await languageFormatter.FormatAsync(tree, node, spans, options, cancellationToken).ConfigureAwait(false)).GetTextChanges(cancellationToken));
            }
            else
            {
                return(SpecializedCollections.EmptyList <TextChange>());
            }
        }
 public static bool IsKind(this SyntaxNodeBase node, SyntaxKind kind)
 {
     return(node != null && node.RawKind == (ushort)kind);
 }
Exemple #13
0
 internal SyntaxTree(SourceText text, Func <SyntaxTree, SyntaxNode> parseFunc)
 {
     _sourceFile = new SourceFile(text, null);
     Root        = parseFunc(this);
 }
Exemple #14
0
 public abstract TypeInfo GetTypeInfo(SyntaxNodeBase node);
Exemple #15
0
 public abstract SymbolInfo GetSymbolInfo(SyntaxNodeBase node);
Exemple #16
0
 public abstract ISymbol GetDeclaredSymbol(SyntaxNodeBase node);
        public override ISymbol GetDeclaredSymbol(SyntaxNodeBase declaration)
        {
            var node = (SyntaxNode)declaration;

            var parameter = node as ParameterSyntax;

            if (parameter != null)
            {
                return(GetDeclaredSymbol(parameter));
            }

            var @namespace = node as NamespaceSyntax;

            if (@namespace != null)
            {
                return(GetDeclaredSymbol(@namespace));
            }

            var interfaceType = node as InterfaceTypeSyntax;

            if (interfaceType != null)
            {
                return(GetDeclaredSymbol(interfaceType));
            }

            var structType = node as StructTypeSyntax;

            if (structType != null)
            {
                return(GetDeclaredSymbol(structType));
            }

            var variableDeclarator = node as VariableDeclaratorSyntax;

            if (variableDeclarator != null)
            {
                return(GetDeclaredSymbol(variableDeclarator));
            }

            var typeAlias = node as TypeAliasSyntax;

            if (typeAlias != null)
            {
                return(GetDeclaredSymbol(typeAlias));
            }

            var constantBuffer = node as ConstantBufferSyntax;

            if (constantBuffer != null)
            {
                return(GetDeclaredSymbol(constantBuffer));
            }

            var functionDeclaration = node as FunctionDeclarationSyntax;

            if (functionDeclaration != null)
            {
                return(GetDeclaredSymbol(functionDeclaration));
            }

            var functionDefinition = node as FunctionDefinitionSyntax;

            if (functionDefinition != null)
            {
                return(GetDeclaredSymbol(functionDefinition));
            }

            var technique = node as TechniqueSyntax;

            if (technique != null)
            {
                return(GetDeclaredSymbol(technique));
            }

            return(null);
        }
 public SourceFileSpan?GetFileSpanRoot(SyntaxNodeBase node)
 {
     return(((SyntaxNode)node).GetTextSpanRoot());
 }
 internal static IList <TextChange> GetFormattedTextChanges(SyntaxTreeBase tree, SyntaxNodeBase node, IEnumerable <TextSpan> spans, Workspace workspace, OptionSet options, CancellationToken cancellationToken)
 {
     return(GetFormattedTextChangesAsync(tree, node, spans, workspace, options, cancellationToken).WaitAndGetResult(cancellationToken));
 }