NeVeSpl / NTypewriter

File/code generator using Scriban text templates populated with C# code metadata from Roslyn API.
https://nevespl.github.io/NTypewriter/
MIT License
117 stars 24 forks source link

class.BareName at beginning of line with one tab adds extra whitespace #73

Closed washamdev closed 1 year ago

washamdev commented 1 year ago

Can anyone help me address this problem? The template contains code at one tab indentation. I've got a line that begins with {{ class.BareName }} on a new line:

image

But when that template is rendered, it contains an extra tab:

image

How do I get that line to start with the right amount of indentation?

Just to be clear, we use four spaces as tabs.

gregveres commented 1 year ago

Checkout this section of docs

Basically, you need to add a '-' to supress the extra whitespace.

{{ - class.BareName }}

washamdev commented 1 year ago

That removes all whitespaces and new lines before that line though.

image

NeVeSpl commented 1 year ago

I have another idea, maybe the class name contains whitespaces? I would try: {{ class.BareName | String.Strip }}

gregveres commented 1 year ago

or maybe add a {{ }} ahead of the {{ - class.BareName }} to ensure that the new line isn't stripped. In my templates, I make sure there are no indentations in the main body of my template. If I need to output something from a function, I return it as a string that the main body prints out. This way I control all the indentation from the main body.

Like this:

{{- # Helper classes }}
{{- func ImportType(type)
    useType = Type.Unwrap(type)
    if (useType.ArrayType != null) 
      useType = useType.ArrayType
    end
    if (type.IsEnum) 
      path = "@skycourt/api/enums"
    else
      path = "./" | String.Append useType.Name
    end
    if ((useType.Attributes | Array.Filter @AttrIsExportToTypescript).size > 0)
        ret "import { " | String.Append useType.Name | String.Append " } from '" | String.Append path | String.Append "';\r"
    end
    ret null
    end
}}
{{- func AttrIsExportToTypescript(attr)
    ret attr.Name | String.Contains "ExportToTypescript"
    end
}}

{{- # output classes }}
{{ $barrelFile = "" }}
{{- for class in data.Classes | Symbols.ThatHaveAttribute "ExportToTypescript" | Array.Concat (data.Classes | Symbols.ThatHaveAttribute "ExportToTypescriptWithKnockout") | Array.Sort "Name"
        capture output }}

{{- for type in (class | Type.AllReferencedTypes)}}
    {{- ImportType type}}
{{-end}}

export interface {{class.Name}}{{if class.HasBaseClass}} extends {{class.BaseClass.Name; end}} {
  {{- for prop in class.Properties | Symbols.ThatArePublic }}
  {{ prop.Name | String.ToCamelCase }}: {{prop | Custom.ToTypeScriptType }}{{if !for.last}},{{end}}
  {{-end}}
}
{{- end}}
washamdev commented 1 year ago

@gregveres I didn't need the {{-. Just using {{ worked, as long as I added the blank entry first. So {{ }}{{ class.BareName }} was the winner! Thank you! Is this a bug or expected behavior?

NeVeSpl commented 1 year ago

Obviously, it is not expected, but Scriban is responsible for that part. NTypewriter uses Scriban v5.5, and apparently, there was some fix in version 5.6 of scriban/scriban#483 related to not intended indentation.

gregveres commented 1 year ago

Based on the documentation, I would say it is working as expected. But if it is a bug, then it is a bug in Scriban, not in NTypewriter. You could as on their github site.