Hp740319
Hp740319

Reputation: 747

SyntaxFactory.ParseStatement Generates extra spaces

I want to create a method based on json value. this is my json :

"handler": {
            "logic": "var response = new IsoMessage(context.Request.Id + 10); \n for (int i = 0; i < 128; i++)\n if ((i != 64) && context.Request.Has(i)) \n response[i] = context.Request[i]; \n\n await Task.Delay(200); \n\n await context.SourceLink.SendAsync(response);"

        }

when it is parsed, normalized with whitespace ,and add to a method, the item that is print is not in a good format. How can I fix it?

This is the code for method syntax creation:

var syntax = SyntaxFactory.ParseStatement(logic).NormalizeWhitespace(); 
    
// Create a method public async Task ProcessAsync(IncomingMessageContext context)
var methodDeclaration = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName(typeof(Task).Name), "ProcessAsync")
        .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
        .AddModifiers(SyntaxFactory.Token(SyntaxKind.AsyncKeyword))
        .WithParameterList(parameterList)
        .WithBody(SyntaxFactory.Block(syntax));

before printing, it is the code:

var code = @namespace.NormalizeWhitespace().ToFullString();

The output is like this:

public async Task ProcessAsync(Nikan.Channels.IncomingMessageContext context)
        {
            var response = new IsoMessage(context.Request.Id + 10); for  ( int  i  =  0 ;  i < 128 ;  i ++ ) if  ( ( i != 64 ) && context . Request . Has ( i ) ) response [ i ]  =  context . Request [ i ] ;  await  Task . Delay ( 200 ) ;  await  context . SourceLink . SendAsync ( response ) ;
        }

There is a lot of space in function and no newline is applied. :(

Upvotes: 1

Views: 519

Answers (1)

Ray
Ray

Reputation: 192406

The code that is being parsed contains multiple statements instead of a single statement. If you notice the first statement:

var response = new IsoMessage(context.Request.Id + 10);

is formatted correctly and doesn't look like this:

var  response  =  new  IsoMessage ( context . Request . Id + 10 ) ;

Therefore, when using SyntaxFactory.ParseStatement() if your code consists of multiple statements then surround all of the statements with a pair of curly braces ({...}) and cast to BlockSyntax which will subsequently parse the code as a "block":

var syntax = (BlockSyntax) SyntaxFactory.ParseStatement("{" + logic + "}");

// ...additional code transformations that adds `ProcessAync()`, etc....

var code = @namespace.NormalizeWhitespace().ToFullString();

Also in the method declaration remove SyntaxFactory.Block() since it should now cast to the correct type:

.WithBody(syntax);

Then your code should output this:

public async Task ProcessAsync(Nikan.Channels.IncomingMessageContext context)
{
    var response = new IsoMessage(context.Request.Id + 10);
    for (int i = 0; i < 128; i++)
        if ((i != 64) && context.Request.Has(i))
            response[i] = context.Request[i];
    await Task.Delay(200);
    await context.SourceLink.SendAsync(response);
}

Upvotes: 3

Related Questions