Example #1
0
    internal static SyntaxTree GetSyntaxTree(Query query)
    {
        if (query != null)
        {
            switch (query.Language)
            {
            case QueryLanguage.Expression:
                return(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(query.Text,
                                                                                InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Interactive)));

            case QueryLanguage.Statements:
                return(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(query.Text,
                                                                                InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Script)));

            case QueryLanguage.Program:
                return(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(query.Text,
                                                                                InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Script)));

            case QueryLanguage.VBExpression:
                return(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(query.Text,
                                                                                          InternalsHelper.GetVisualBasicParseOptionsForSourceCodeKind(SourceCodeKind.Interactive)));

            case QueryLanguage.VBStatements:
                return(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(query.Text,
                                                                                          InternalsHelper.GetVisualBasicParseOptionsForSourceCodeKind(SourceCodeKind.Script)));

            case QueryLanguage.VBProgram:
                return(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(query.Text,
                                                                                          InternalsHelper.GetVisualBasicParseOptionsForSourceCodeKind(SourceCodeKind.Script)));

            default:
                return(null);
            }
        }
        return(null);
    }
Example #2
0
        public void GetIdentifierTokenValueTextReturnsNullForInvalidNode()
        {
            // Given
            SyntaxTree syntaxTree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText("int c = 0;",
                                                                                             InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Script));
            SyntaxNode syntaxNode = syntaxTree.GetRoot();  // The root will never have an identifier token

            // When
            string tokenValueText = InternalsHelper.GetIdentifierTokenValueText(syntaxNode);

            // Then
            Assert.IsNull(tokenValueText);
        }
Example #3
0
        public void GetIdentifierTokenValueTextReturnsTokenValueTextForValidNode()
        {
            // Given
            SyntaxTree syntaxTree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText("int c = 0;",
                                                                                             InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Script));
            SyntaxToken syntaxToken = syntaxTree.GetRoot()
                                      .DescendantNodesAndTokens()
                                      .Where(x => x.IsToken)
                                      .Select(x => x.AsToken())
                                      .First(x => Microsoft.CodeAnalysis.CSharp.CSharpExtensions.Kind(x) == Microsoft.CodeAnalysis.CSharp.SyntaxKind.IdentifierToken);
            SyntaxNode syntaxNode = syntaxToken.Parent;

            // When
            string tokenValueText = InternalsHelper.GetIdentifierTokenValueText(syntaxNode);

            // Then
            Assert.AreEqual("c", tokenValueText);
        }