0

Problem

In the last couple of weeks I am creating a application where it is possible to generate a C# class file (.cs file extention). I am trying to find a Nuget-Package, or some other way to format this document so it is readable but I can't seem to find any solution, either no question on here.

I know there is a way to format a document, but this is too much work for my project.

Question

Do any of you have the same "problem" and is there a way to format a document properly?

Some extra explanation

How I want to have my document (kind of) formatted:

GINLoop1 = new List < GINLoop1 > () {
    new GINLoop1 {
        GIN = new GIN {
            Objectidentificationcodequalifier_01 = "test1",
            IDENTITYNUMBERRANGE_02 = new C208_2 {
                Objectidentifier_01 = "test",
                Objectidentifier_02 = null,
            },
            IDENTITYNUMBERRANGE_03 = null,
            IDENTITYNUMBERRANGE_04 = null,
            IDENTITYNUMBERRANGE_05 = null,
            IDENTITYNUMBERRANGE_06 = null,
        },
    },
},

How my document is now formatted:

GINLoop1 = new List < GINLoop1 > () {
new GINLoop1 {
GIN = new GIN {
Objectidentificationcodequalifier_01 = "test1",
IDENTITYNUMBERRANGE_02 = new C208_2 {
Objectidentifier_01 = "test",
Objectidentifier_02 = null,
},
IDENTITYNUMBERRANGE_03 = null,
IDENTITYNUMBERRANGE_04 = null,
IDENTITYNUMBERRANGE_05 = null,
IDENTITYNUMBERRANGE_06 = null,
},
},
},
Max
  • 23
  • 8
  • 2
    any reason you can't just format while writing it? as an example, [here's how I do this in protogen](https://github.com/mgravell/protobuf-net/blob/987bc1856f3012488f6eacab84581d2a901c9c49/src/protobuf-net.Reflection/CSharpCodeGenerator.cs#L240-L244) - note the `Indent()` and `Outdent()` methods; I have a wrapper between the codegen and the underlying writer that applies the current indent automatically – Marc Gravell Apr 19 '18 at 13:42
  • How are you generating the file in the first place? Have you looked at [CodeDOM](https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/how-to-create-a-class-using-codedom)? – Dan Wilson Apr 19 '18 at 13:43
  • 1
    You can have a look here https://github.com/dotnet/codeformatter – Soufiane Tahiri Apr 19 '18 at 13:49
  • @CodeDom I am using `StreamWriter`, and no I have not looked at CodeDOM. I will look at it. – Max Apr 19 '18 at 13:49
  • @MarcGravell I did not found an easy way for it, but I will look at your example. Thanks! – Max Apr 19 '18 at 13:51

2 Answers2

1

JetBrains has (free) commandline tools available as a NuGet package, you can find them at https://www.nuget.org/packages/JetBrains.ReSharper.CommandLineTools.

It's not very thoroughly documented but I am sure there's some way to format a .cs file in it. They have code cleanup available in their ReSharper plugin and their Rider IDE, which I believe both make use of this library one way or another.

ManIkWeet
  • 1,298
  • 1
  • 15
  • 36
0

It's pretty old, but NArrange used to work the way you want. However, I haven't been using it for several years. Check it out on nuget.org here:

https://www.nuget.org/packages/NArrange/

Dag Baardsen
  • 774
  • 7
  • 9