internal FileModel(Microsoft.CodeAnalysis.SyntaxTree tree, ParseOptions options) { Tree = tree; var structs = tree.GetCompilationUnitRoot().DescendantNodes(n => !(n is StructDeclarationSyntax)).OfType <StructDeclarationSyntax>(); var classes = tree.GetCompilationUnitRoot().DescendantNodes(n => !(n is ClassDeclarationSyntax)).OfType <ClassDeclarationSyntax>(); Structs = new List <StructModel>(); foreach (var s in structs) { var matchingProxy = classes.SingleOrDefault(c => c.Identifier.Text == StructModel.MakeProxyName(s.Identifier.Text)); var model = ParseStruct(s, matchingProxy); if (model != null) { Structs.Add(model); } } if (Structs.Count > 1 && options == ParseOptions.DisallowMultipleStructs) { throw new InvalidOperationException("File contains multiple component definitions"); } }
public virtual Microsoft.CodeAnalysis.SyntaxTree OnTranslate(VSGraphModel graphModel, AssemblyType assemblyType, CompilationOptions compilationOptions, ref CompilationResult compilationResult) { const string windowsLineEndings = "\r\n"; const string unixLineEndings = "\n"; Microsoft.CodeAnalysis.SyntaxTree syntaxTree = Translate(graphModel, compilationOptions); // we will measure plugins time later string preferredLineEndings; LineEndingsMode lineEndingsForNewScripts = EditorSettings.lineEndingsForNewScripts; switch (lineEndingsForNewScripts) { case LineEndingsMode.OSNative: preferredLineEndings = Application.platform == RuntimePlatform.WindowsEditor ? windowsLineEndings : unixLineEndings; break; case LineEndingsMode.Unix: preferredLineEndings = unixLineEndings; break; case LineEndingsMode.Windows: preferredLineEndings = windowsLineEndings; break; default: preferredLineEndings = unixLineEndings; break; } var adHocWorkspace = new AdhocWorkspace(); var options = adHocWorkspace.Options .WithChangedOption(CSharpFormattingOptions.NewLineForMembersInObjectInit, true) .WithChangedOption(CSharpFormattingOptions.WrappingPreserveSingleLine, false) .WithChangedOption(CSharpFormattingOptions.WrappingKeepStatementsOnSingleLine, false) .WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInObjectCollectionArrayInitializers, true) .WithChangedOption(FormattingOptions.NewLine, LanguageNames.CSharp, preferredLineEndings); compilationResult.sourceCode[(int)SourceCodePhases.Initial] = syntaxTree.GetText().ToString(); var formattedTree = Formatter.Format(syntaxTree.GetCompilationUnitRoot(), adHocWorkspace, options); formattedTree = new VisualScriptingCSharpFormatter().Visit(formattedTree); string codeText = formattedTree.GetText().ToString(); compilationResult.sourceCode[(int)SourceCodePhases.Final] = codeText; return(syntaxTree); }
private static bool IsSelectingADifferentMethod(IEnumerable<SyntaxNode> childNodes, SimpleNameSyntax methodName, SyntaxTree tree, IMethodSymbol methodSymbol, ExpressionSyntax invocationExpression, Compilation compilation) { var parameterExpressions = GetParameterExpressions(childNodes); var firstArgument = parameterExpressions.FirstOrDefault(); var argumentList = CreateArgumentListSyntaxFrom(parameterExpressions.Skip(1)); var newInvocationStatement = CreateInvocationExpression(firstArgument, methodName, argumentList) .WithAdditionalAnnotations(introduceExtensionMethodAnnotation); var extensionMethodNamespaceUsingDirective = SyntaxFactory.UsingDirective(methodSymbol.ContainingNamespace.ToNameSyntax()); var speculativeRootWithExtensionMethod = tree.GetCompilationUnitRoot() .ReplaceNode(invocationExpression, newInvocationStatement) .AddUsings(extensionMethodNamespaceUsingDirective); var speculativeModel = compilation.ReplaceSyntaxTree(tree, speculativeRootWithExtensionMethod.SyntaxTree) .GetSemanticModel(speculativeRootWithExtensionMethod.SyntaxTree); var speculativeInvocationStatement = speculativeRootWithExtensionMethod.SyntaxTree.GetCompilationUnitRoot().GetAnnotatedNodes(introduceExtensionMethodAnnotation).Single() as InvocationExpressionSyntax; var speculativeExtensionMethodSymbol = speculativeModel.GetSymbolInfo(speculativeInvocationStatement.Expression).Symbol as IMethodSymbol; var speculativeNonExtensionFormOfTheMethodSymbol = speculativeExtensionMethodSymbol?.GetConstructedReducedFrom(); return speculativeNonExtensionFormOfTheMethodSymbol == null || !speculativeNonExtensionFormOfTheMethodSymbol.Equals(methodSymbol); }
private static bool IsSelectingADifferentMethod(IEnumerable<SyntaxNode> childNodes, SimpleNameSyntax methodName, SyntaxTree tree, IMethodSymbol methodSymbol, ExpressionSyntax invocationExpression, Compilation compilation) { var parameterExpressions = GetParameterExpressions(childNodes); var firstArgument = parameterExpressions.FirstOrDefault(); var argumentList = CreateArgumentListSyntaxFrom(parameterExpressions.Skip(1)); var newInvocationStatement = CreateInvocationExpression(firstArgument, methodName, argumentList) .WithAdditionalAnnotations(introduceExtensionMethodAnnotation); var extensionMethodNamespaceUsingDirective = SyntaxFactory.UsingDirective(methodSymbol.ContainingNamespace.ToNameSyntax()); var speculativeRootWithExtensionMethod = tree.GetCompilationUnitRoot() .ReplaceNode(invocationExpression, newInvocationStatement) .AddUsings(extensionMethodNamespaceUsingDirective); var speculativeTree = speculativeRootWithExtensionMethod.SyntaxTree; var speculativeTreeOptions = (CSharpParseOptions)speculativeTree.Options; var speculativeTreeWithCorrectLanguageVersion = speculativeTree.WithRootAndOptions(speculativeRootWithExtensionMethod, speculativeTreeOptions.WithLanguageVersion(((CSharpParseOptions)tree.Options).LanguageVersion)); var speculativeModel = compilation.ReplaceSyntaxTree(tree, speculativeTreeWithCorrectLanguageVersion) .GetSemanticModel(speculativeTreeWithCorrectLanguageVersion); var speculativeInvocationStatement = speculativeTreeWithCorrectLanguageVersion.GetCompilationUnitRoot().GetAnnotatedNodes(introduceExtensionMethodAnnotation).Single() as InvocationExpressionSyntax; var speculativeExtensionMethodSymbol = speculativeModel.GetSymbolInfo(speculativeInvocationStatement.Expression).Symbol as IMethodSymbol; var speculativeNonExtensionFormOfTheMethodSymbol = speculativeExtensionMethodSymbol?.GetConstructedReducedFrom(); return speculativeNonExtensionFormOfTheMethodSymbol == null || speculativeNonExtensionFormOfTheMethodSymbol.ToString() != methodSymbol.ToString();//can't compare equality, as speculative symbol might be different }
public static SyntaxTree Genesis(SyntaxTree syntaxTree) { CodeGenSyntaxVisitor cbs = new CodeGenSyntaxVisitor(syntaxTree.GetCompilationUnitRoot()); cbs.Visit(); var @class = SyntaxFactory.ClassDeclaration("MyGenerator") .AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword)) .AddMembers(cbs.Generated.ToArray()); var @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.IdentifierName("MyGenerator")) .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System"))) .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Collections.Generic"))) .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Linq"))) .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Text"))) .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Reflection"))) .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("Microsoft.CodeAnalysis"))) .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("Microsoft.CodeAnalysis.CSharp"))) .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("Microsoft.CodeAnalysis.CSharp.Syntax"))) .AddMembers(@class); syntaxTree = SyntaxFactory.SyntaxTree(@namespace, path: "output"); return syntaxTree; }
private static void CheckOverloadResolutionResults(SyntaxTree tree, SemanticModel model, params string[] expected) { var actual = GetElementAccessExpressions(tree.GetCompilationUnitRoot()).Select(syntax => model.GetSymbolInfo(syntax).Symbol.ToTestDisplayString()); AssertEx.Equal(expected, actual, itemInspector: s => string.Format("\"{0}\"", s)); }