public WebInfoGenerationData GetClassForWebElement(WebElementInfo info) { var className = GetClassNameFromElementName(info.Name); var propName = className.Substring(1); var constInfoCD = GetClassForConstInfo(info); var baseClassName = info.GetType().Name; var docComment = GetDocCommentWithText(info.Description); var infoComment = GetDocCommentWithText("Information about element"); var infoProperty = SF.PropertyDeclaration( SF.ParseTypeName($"{InfoStaticClassName}.{InfoClassName}"), SF.Identifier(InfoClassName) ) .WithAccessorList( SF.AccessorList( SF.List( new List <AccessorDeclarationSyntax> { SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)), SF.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)) } ) ) .WithOpenBraceToken(SF.Token(SyntaxKind.OpenBraceToken)) .WithCloseBraceToken(SF.Token(SyntaxKind.CloseBraceToken)) ) .AddModifiers(SF.Token(SyntaxKind.PublicKeyword).WithLeadingTrivia(SF.Trivia(infoComment))); var getInstMd = GetGetInstanceMethod(className); var cd = SF.ClassDeclaration(className) .AddModifiers(SF.Token(SyntaxKind.PublicKeyword).WithLeadingTrivia(SF.Trivia(docComment))) .AddBaseListTypes(SF.SimpleBaseType(SF.IdentifierName(baseClassName))) .AddMembers(infoProperty, getInstMd, constInfoCD); var genData = new WebInfoGenerationData { ClassName = className, PropertyName = propName, ClassSyntax = cd, Element = info }; FillWithChildrenElementsProperties(genData, out List <WebInfoGenerationData> childrenGens); AddCtor(genData, childrenGens); FillWithChildrenElementsClasses(genData, childrenGens); return(genData); }
private void FillWithChildrenElementsProperties(WebInfoGenerationData genData, out List <WebInfoGenerationData> childrenGens) { childrenGens = null; if (!(genData.Element is CombinedWebElementInfo combinedInfo)) { return; } childrenGens = combinedInfo.Elements ?.Select(e => GetClassForWebElement(e)).ToList() ?? new List <WebInfoGenerationData>(); if (childrenGens.Count == 0) { return; } var members = new List <MemberDeclarationSyntax>(); foreach (var childGen in childrenGens) { var docComment = GetDocCommentWithText(childGen.Element.Description); var pd = SF.PropertyDeclaration( SF.IdentifierName(SF.Identifier(childGen.ClassName)), SF.Identifier(childGen.PropertyName) ) .WithAccessorList( SF.AccessorList( SF.List( new List <AccessorDeclarationSyntax> { SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)), SF.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)) } ) ) .WithOpenBraceToken(SF.Token(SyntaxKind.OpenBraceToken)) .WithCloseBraceToken(SF.Token(SyntaxKind.CloseBraceToken)) ) .AddModifiers(SF.Token(SyntaxKind.PublicKeyword).WithLeadingTrivia(SF.Trivia(docComment))); members.Add(pd); } var cd = genData.ClassSyntax.AddMembers(members.ToArray()); genData.ClassSyntax = cd; }
private SyntaxList <MemberDeclarationSyntax> GenerateProperties(List <Property> props) { return(SF.List(props.Select(p => { //return (MemberDeclarationSyntax)SF.PropertyDeclaration( // SF.PredefinedType( // SF.Token(SyntaxKind.StringKeyword)), // SF.Identifier(p.CleanName)) // .WithAttributeLists( // SF.SingletonList( // SF.AttributeList( // GenerateAttributes(p.Attributes)))) // .WithModifiers( // SF.TokenList( // SF.Token(SyntaxKind.PublicKeyword))) // .WithAccessorList( // SF.AccessorList( // SF.List( // new AccessorDeclarationSyntax[]{ // SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) // .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)), // SF.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) // .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)) // }))); return (MemberDeclarationSyntax)p.ToDeclarationSyntax() .WithAttributeLists( SF.SingletonList( SF.AttributeList( GenerateAttributes(p.XmlAttributes)))) .WithModifiers( SF.TokenList( SF.Token(SyntaxKind.PublicKeyword))) .WithAccessorList( SF.AccessorList( SF.List( new AccessorDeclarationSyntax[] { SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)), SF.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)) }))); }).ToList())); }
public static PropertyDeclarationSyntax GenProperty(string type, string name, bool setter, string initializer = null) { var accessors = setter ? new[] { SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)), SF.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)) } : new[] { SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)) }; return(SF.PropertyDeclaration( attributeLists: SF.List <AttributeListSyntax>(), modifiers: SF.TokenList(SF.Token(SyntaxKind.PublicKeyword)), type: SF.ParseTypeName(type), explicitInterfaceSpecifier: null, identifier: SF.Identifier(name), accessorList: SF.AccessorList(SF.List(accessors)), expressionBody: null, initializer: initializer == null ? null : SF.EqualsValueClause(SF.ParseExpression(initializer)), semicolonToken: initializer == null ? SF.Token(SyntaxKind.None) : SF.Token(SyntaxKind.SemicolonToken))); }
AccessorListSyntax GetAccessorList() { return(SF.AccessorList(SF.List(GetAccessors()))); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); // TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest var diagnostic = context.Diagnostics.First(); if (diagnostic.Id == StacksAnalyzer.CommandsEndWithCommand.Id) { var diagnosticSpan = diagnostic.Location.SourceSpan; // Find the type declaration identified by the diagnostic. var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <TypeDeclarationSyntax>().First(); // Register a code action that will invoke the fix. context.RegisterCodeFix( CodeAction.Create( title: "Append 'Command'", createChangedSolution: c => this.AppendCommandText(context.Document, "Command", declaration, c), equivalenceKey: "Append 'Command'"), diagnostic); } else if (diagnostic.Id == StacksAnalyzer.EventsEndWithEvents.Id) { var diagnosticSpan = diagnostic.Location.SourceSpan; // Find the type declaration identified by the diagnostic. var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <TypeDeclarationSyntax>().First(); // Register a code action that will invoke the fix. context.RegisterCodeFix( CodeAction.Create( title: "Append 'Event'", createChangedSolution: c => this.AppendCommandText(context.Document, "Event", declaration, c), equivalenceKey: "Append 'Event'"), diagnostic); } else if (diagnostic.Id == StacksAnalyzer.MessagePropertiesAreImmutable.Id) { var diagnosticSpan = diagnostic.Location.SourceSpan; var property = root.FindNode(diagnosticSpan) as PropertyDeclarationSyntax; // Register a code action that will invoke the fix. context.RegisterCodeFix( CodeAction.Create( title: "Remove setter", createChangedDocument: async c => { var previousWhiteSpacesToken = SF.Token(property.GetLeadingTrivia(), SyntaxKind.StringLiteralToken, SyntaxTriviaList.Empty); var target = property.WithModifiers(SF.TokenList(previousWhiteSpacesToken, SF.Token(SyntaxKind.PublicKeyword))) .WithAccessorList(SF.AccessorList(SF.List(new[] { SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)) }))); var updated = await context.Document.GetSyntaxRootAsync(c); return(context.Document.WithSyntaxRoot(updated.ReplaceNode(property, new[] { target }))); }, equivalenceKey: "Remove setter"), diagnostic); context.RegisterCodeFix( CodeAction.Create( title: "Make setter private", createChangedDocument: async c => { var previousWhiteSpacesToken = SF.Token(property.GetLeadingTrivia(), SyntaxKind.StringLiteralToken, SyntaxTriviaList.Empty); var target = property.WithModifiers(SF.TokenList(previousWhiteSpacesToken, SF.Token(SyntaxKind.PublicKeyword))) .WithAccessorList(SF.AccessorList(SF.List(new[] { SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)), SF.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithModifiers(SF.TokenList(SF.Token(SyntaxKind.PrivateKeyword))) .WithSemicolonToken(SF.Token(SyntaxKind.SemicolonToken)) }))); var updated = await context.Document.GetSyntaxRootAsync(c); return(context.Document.WithSyntaxRoot(updated.ReplaceNode(property, new[] { target }))); }, equivalenceKey: "Make setter private"), diagnostic); } else if (diagnostic.Id == StacksAnalyzer.UseCaseShouldHaveRules.Id) { var diagnosticSpan = diagnostic.Location.SourceSpan; var property = root.FindNode(diagnosticSpan) as ClassDeclarationSyntax; context.RegisterCodeFix( CodeAction.Create( title: "Create business rule", createChangedDocument: async c => { //property.Identifier.ValueText; var cu = SF.CompilationUnit().AddUsings( SF.UsingDirective(SF.IdentifierName("System")), SF.UsingDirective(SF.IdentifierName("Slalom.Stacks.Messaging.Validation"))); NamespaceDeclarationSyntax namespaceDeclarationSyntax = null; if (!SyntaxNodeHelper.TryGetParentSyntax(property, out namespaceDeclarationSyntax)) { } var ns = SF.NamespaceDeclaration(namespaceDeclarationSyntax.Name); var comp = await context.Document.Project.GetCompilationAsync(); string name = property.Identifier.Value + "_rule"; var command = ((IdentifierNameSyntax)((GenericNameSyntax)property.BaseList.Types[0].Type).TypeArgumentList.Arguments[0]).Identifier.ValueText; //if (comp.GetSymbolsWithName(x => x == name) != null) //{ // int i = 1; // var current = name + i; // while (comp.GetSymbolsWithName(x => x == current) != null) // { // i++; // } //} var cl = SF.ClassDeclaration(name) .WithModifiers(SF.TokenList(SF.Token(SyntaxKind.PublicKeyword))) .AddBaseListTypes(SF.SimpleBaseType(SF.ParseTypeName("BusinessRule<" + command + ">"))); ns = ns.AddMembers(cl); cu = cu.AddMembers(ns); SyntaxNode formattedNode = Formatter.Format(cu, context.Document.Project.Solution.Workspace); StringBuilder sb = new StringBuilder(); using (StringWriter writer = new StringWriter(sb)) { formattedNode.WriteTo(writer); } return(context.Document.Project.AddDocument(name + ".cs", sb.ToString(), namespaceDeclarationSyntax.Name.ToString().Split('.').Skip(1).Concat(new[] { "Rules" }))); }, equivalenceKey: "Create business rule"), diagnostic); } }