private async Task <Document> FormatXmlAsync(Document document, ArgumentSyntax argSntax, CancellationToken cancellationToken) { var root = await document.GetSyntaxRootAsync(cancellationToken); var tree = root.SyntaxTree; //Get the character position of the ArgumentSyntax FileLinePositionSpan position = tree.GetLineSpan(argSntax.Span); int cSpace = position.StartLinePosition.Character; //Get the parent VariableDeclaration to figure out the preceeding trivia since we can't //get the column position from GetLineSpan (bug?) var parent = argSntax.Ancestors().Where(t => t.IsKind(SyntaxKind.VariableDeclaration)).FirstOrDefault(); var parentTrivia = parent.GetLeadingTrivia().ToFullString(); var xml = argSntax.Expression.GetFirstToken().ValueText; var newXml = FormatXml(xml); //Process each line of the formatted XML and prepend the parent trivia & spaces string[] xmlLines = newXml.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); for (int i = 1; i < xmlLines.Length; i++) { xmlLines[i] = parentTrivia + new String(' ', (cSpace - 14)) + xmlLines[i]; } newXml = String.Join("\r\n", xmlLines); newXml = "@\"" + newXml + "\""; var newNode = SyntaxFactory.Argument(null, SyntaxFactory.Token(SyntaxKind.None), SyntaxFactory.IdentifierName(newXml)); var newRoot = root.ReplaceNode(argSntax, newNode); return(document.WithSyntaxRoot(newRoot)); }
private Task <Solution> UseNameOfAsync(Document document, SyntaxNode root, ArgumentSyntax argumentDeclaration) { var properties = argumentDeclaration.Ancestors().OfType <ClassDeclarationSyntax>().First().ChildNodes().OfType <PropertyDeclarationSyntax>(); foreach (var property in properties) { if (string.Equals(property.Identifier.ValueText, ((LiteralExpressionSyntax)argumentDeclaration.Expression).Token.ValueText, StringComparison.OrdinalIgnoreCase)) { root = root.ReplaceNode(argumentDeclaration.Expression, SyntaxFactory.ParseExpression($"nameof({property.Identifier.ValueText})")); var newDocument = document.WithSyntaxRoot(root); return(Task.FromResult(newDocument.Project.Solution)); } } return(null); }