3

in my current project, we have a couple of code generator routines to help us through some mindless tasks. Everything works fine from the technical point of view, so that might be more a curiosity than a real issue: when I open a newly generated piece of code, it is (of course) not properly indented (although syntactically correct).

Now, the question: is there an API somewhere that can be used to indent a piece of c# code? Much like what happens when I use the shortcut Ctrl+E,D in VS2010.

Thanks!

Just to clarify, I am looking for a function like that:

string GetProperlyFormattedCode(string notFormattedCode);

where notFormattedCode is a piece of valid c# source code, and the output of the function is the same code after application of formatting rules. In other words, I am looking for the function that lies behind the "Edit -> Advanced -> Format selection" command of Visual Studio.

Andrea
  • 884
  • 1
  • 10
  • 23
  • 2
    Do you want indenting when you generate the code? Would the http://msdn.microsoft.com/en-us/library/system.codedom.compiler.indentedtextwriter.aspx help? – sisve Feb 02 '12 at 18:47
  • Hi, I think it is not what I am looking for - I'm editing the question with an example. – Andrea Feb 02 '12 at 18:50
  • possible duplicate of [How do you call "Document Format" programmatically from C#?](http://stackoverflow.com/questions/813119/how-do-you-call-document-format-programmatically-from-c) – Greg Hewgill Feb 02 '12 at 20:17
  • The question you referenced is regarding formatting from the "add-in context". That would require you to make an add-in. This question does not involve addins. – marknuzz Oct 29 '12 at 06:20

2 Answers2

4

To indent code just use Microsoft.CodeAnalysis.CSharp nuget package and .NET framework 4.6+. Sample code:

public string ArrangeUsingRoslyn(string csCode) {
    var tree = CSharpSyntaxTree.ParseText(csCode);
    var root = tree.GetRoot().NormalizeWhitespace();
    var ret = root.ToFullString();
    return ret;
}

One-liner:

csCode = CSharpSyntaxTree.ParseText(csCode).GetRoot().NormalizeWhitespace().ToFullString();

You may also use NArrange to sort methods in your cs file, organize usings, create regions, etc. Note that NArrange does not indent anything.

Anton Krouglov
  • 3,077
  • 2
  • 29
  • 50
1

This question seems to answer:

How do you call "Document Format" programmatically from C#?

The short is use Visual Studio's object model to call the format command.

Community
  • 1
  • 1
EBarr
  • 11,826
  • 7
  • 63
  • 85
  • That doesn't answer this question. The question you referenced is regarding formatting from the "add-in context". That would require you to make an add-in. This question does not involve addins. – marknuzz Oct 29 '12 at 06:19
  • I was suggesting that he can programatically leverage the code formatting built into VS, which by chance happens to be what the OP used as an example (CTRL+E, D). Why invent something when you can try to leverage the formatter you already like? – EBarr Oct 29 '12 at 14:30