dotnet/templating

Inline conditional processing or how to append a colon to the previous line

Opened this issue · 0 comments

Is your feature request related to a problem? Please describe.

Hello,

I am migrating a project from T4 templates. I often meet inline conditionals like these:

add(a<# if(cond) { #>, b<# } #>);

Meaning, it would result either in add(a); or add(a, b);.

Most of the time, it's enough to just #if(cond) add(a); #else add(a,b); #endif

But in complex cases, it would require a O(2^N)-wide block to pick a line with N conditions:

#if(cond_b)
 #if(cond_c)
add(a,b,c);
 #else
add(a,b);
 #endif
#else
 #if(cond_c)
add(a,c);
 #else
add(a);
 #endif
#endif

Another common place of pain for me is chained methods where I need to place a colon on top of the last invocation:

var array = list.Select(...)
#if(a)
 #if(!b && !c)
    .Where(...);
 #else
    .Where(...)
 #endif
#endif
#if(b)
 #if(!c)
    .Where(...);
 #else
    .Where(...)
 #endif
#endif
#if(c)
    .Where(...);
#endif

I tried the following, to no avail:

add(a/*#if(cond)*/, b/*#endif*/)

// or

//...
#if(c)
.Where()
#endif
;
"symbols": [ //...
{ // should remove all newline if it's followed by a colon
  "type": "computed",
  "replace": "\n;",
  "value": ";",
  "datatype": "text"
}
]
// or
"specialCustomOperators": { "**.cs": { "operations": [
{
  "type": "conditional",
  "configuration": {
    "if": [ "/*#if*/" ],
    "else": [ "/*#else*/" ],
    "elseif": [ "/*#elseif*/" ],
    "endif": [ "/*#endif*/" ],
    "trim": false,
    "wholeLine": false,
    "evaluator": "C++"
  }
}
]

Describe the solution you'd like.

I want to have inline conditionals like it was with T4:

add(a/*#if(cond)*/, b/*#endif*/)

Another solution would be to add a token/command that would "eat" the previous newline, concatenating the current line to the previous one:

add(a
#if (cond)
/*eatline*/, b
#endif
/*eatline*/)
#if(...)
    .chain()
#endline
/*eatline*/;

Additional context

No response