0

I'm using Roslyn to create what should be a language agnostic code generator for some sort of data access layer. It will use metadata to output the desired code. It is expected to return C# and VB.NET versions of the code.

Current implementation is generating the desired output however the nullable types are not well formatted, the output contains extra white spaces.

Is there an option to ensure the nullable type generated by SyntaxGenerator.NullableTypeExpression return a well formatted - no white spaces - declaration?

Code Snippets

This is where SyntaxGenerator.NullableTypeExpression is used to return a SyntaxNode corresponding to the property type.

    private SyntaxNode ToTypeExpression(Type type, bool nullable, SyntaxGenerator generator)
    {

        SyntaxNode baseType;
        SyntaxNode propType;

        SpecialType specialType = ToSpecialType(type);

        if(specialType == SpecialType.None)
        {
            baseType = generator.IdentifierName(type.Name);                
        }
        else
        {
            baseType = generator.TypeExpression(specialType);
        }

        if (nullable && type.IsValueType)
        {
            propType = generator.NullableTypeExpression(baseType);
        }
        else
        {
            propType = baseType;
        }

        return propType;

    }

This is the generated VB.NET code:

    Public Property Salary As Integer?
        Get
            Return GetAttributeValue(Of Integer?)("salary").Value
        End Get

        Set(value As Integer?)
            SetPropertyValue("Salary", "salary", value)
        End Set
    End Property

This is the C# code, note the white spaces:

    public int ? Salary
    {
        get
        {
            return GetAttributeValue<int ? >("salary").Value;
        }

        set
        {
            SetPropertyValue("Salary", "salary", value);
        }
    }
Ebrito
  • 21
  • 5
  • Echoing Ed, what is the error message, expected condition, etc. here? – Kirk Woll Feb 23 '17 at 03:32
  • 1
    Have you tried `.W‌​ithAdditionalAnnotat‌​ions(Formatter.Annot‌​ation)` ? – Kris Vandermotten Feb 23 '17 at 15:24
  • 1
    Sorry guys but I didn't understand the "down vote". @EdPlunkett and Kirk, the question is on the post subject, which ends with a question mark :-) There is no "error message" but the "unexpected condition" is the additional white space on the C# version of the nullable value. It doesn't prevent it from compiling but doesn't look right either. – Ebrito Feb 23 '17 at 22:15
  • @Ebrito Clearly, you've got Roslyn properly generating the exact code you want. This is a question about *formatting* the generated code, contrary to the title. Maybe that's what the -1 was about; can't say, as it wasn't me. – 15ee8f99-57ff-4f92-890c-b56153 Feb 23 '17 at 23:32
  • @KrisVandermotten that didn't change the output. – Ebrito Feb 24 '17 at 05:06
  • Answer provided on this post did the trick: [link](http://stackoverflow.com/questions/33467295/generating-well-formatted-syntax-with-roslyn) – Ebrito Feb 24 '17 at 05:08

1 Answers1

0

Using Formatter.Format as suggest here: Generating well-formatted syntax with Roslyn, removed the undesired white spaces.

        string textOutput;
        var sb = new StringBuilder();

        var result = generator.CompilationUnit(declarations);

        var formattedResult = Formatter.Format(result, workspace);

        using (StringWriter writer = new StringWriter(sb))
        {
            formattedResult.WriteTo(writer);
            textOutput = writer.ToString();
        }
Community
  • 1
  • 1
Ebrito
  • 21
  • 5