简体   繁体   中英

Eliminate excessive white space and new lines in C# code

I'm using Visual Studio 2015. The developer before me must have used a tool to introduce huge amounts of white space into his code. Also statements are spread across many lines when they really should be on one line.

Here's a sample:

var requestCustomer = new GetCustomersByIdentifierRequest
{
    Metadata =
                                  new CustomerSearchRetrieveReference
                                  .RequestMetadata
                                  {
                                      SecurityAction
                                              = "Get",
                                      UserId =
                                              "WebService"
                                  },
    Params =
                                  new GetCustomersByIdentifierParams
                                  {
                                      EffectiveAsOf
                                              =
                                              DateTime
                                              .Today,
                                      Identifiers
                                              =
                                              resultSearch
                                              .Select
                                              (
                                                  x
                                                  =>
                                                  x
                                                      .CustomerUd)
                                              .ToList
                                              (
                                                  )
                                  }
};

It's really quite excessive. Is there some command or tool I can use to auto-format or reformat this code and at least get expressions and basic assignments onto single lines? Even if it just gets me closer, that would be a great help. It's quite tedious to do it manually.

I have tried using Ctrl+K,F but it doesn't change the code at all.

Please, something simple and preferably built-in. I'm trying not to lose too much time on this.

I use 'Format Document'.
Menu: Edit > Advanced > Format Document
Shortcut: Ctrl+E + Ctrl+D

It uses the formatting configuration from Tools > Options > Text Editor > C# > Formatting

Ctrl+K + Ctrl+D is limited to formatting pure white space, and won't remove unnecessary lines in a document. For example:

SomeMethod(     );

Will format to SomeMethod();

However:

SomeMethod(


);

Will remain the same.

As far as I know, there is nothing built in Visual Studio 2010, 2013 or 2015 that would do this. I did find an online tool that was very close to achieving what it is you're after.

Your code sample of:

var requestCustomer = new GetCustomersByIdentifierRequest
{
Metadata =
                              new CustomerSearchRetrieveReference
                              .RequestMetadata
                              {
                                  SecurityAction
                                          = "Get",
                                  UserId =
                                          "WebService"
                              },
Params =
                              new GetCustomersByIdentifierParams
                              {
                                  EffectiveAsOf
                                          =
                                          DateTime
                                          .Today,
                                  Identifiers
                                          =
                                          resultSearch
                                          .Select
                                          (
                                              x
                                              =>
                                              x
                                                  .CustomerUd)
                                          .ToList
                                          (
                                              )
                              }
};

Was formatted to this result:

var requestCustomer = new GetCustomersByIdentifierRequest {
 Metadata =
  new CustomerSearchRetrieveReference
  .RequestMetadata {
   SecurityAction
    = "Get",
    UserId =
    "WebService"
  },
  Params =
  new GetCustomersByIdentifierParams {
   EffectiveAsOf
    =
    DateTime
    .Today,
    Identifiers =
    resultSearch
    .Select(
     x =>
     x
     .CustomerUd)
    .ToList()
  }
};

It's not perfect, but it does make the code far more sane, and wouldn't be incredibly time-consuming to get it where you'd like it to be manually. It may enable Ctrl+K + Ctrl+D to handle some of the load too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM