Auto-formatting lambda-functions in Visual studio

2020-07-10 07:18发布

问题:

How to set up Visual studio 2010 so that multi-line lambda-functions will not look ugly, with all that empty space in the left?

   dataView.CellFormatting += (s, e) =>
                                           {
                                               if ((e.ColumnIndex == 1)&&((dataView.SelectedCells.Count == 1)))
                                               {    
                                                   var scope = Scope.Instance;    
                                                   var row = dataView.Rows[e.RowIndex];
                                                   var variable = row.DataBoundItem as Variable;

                                                   if (scope.Variables.Contains(variable))
                                                   {
                                                       dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                                                           scope.GetGraph(variable).Color;
                                                   }

                                                   else
                                                   {
                                                       dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
                                                   }
                                               }
                                           };

回答1:

It depends on how much white space you consider ugly but one thing you can do to minimize it is hit a carriage return right after the equal. Then you end up with something like this. `

   {
        var raw_custs =
            (from customer in GetActive()
             where customer.Name.Contains(name)
             select customer).Take(numberToGet).ToList();

I usually hit CTRl-E CTRL-D right after making a change like this to get the document to auto-format (Edit->Advanced->Format Document)

(Just saw your amended post - when I put that in VS and hit return after the +=

dataView.CellFormatting +=
    (s, e) =>
    {
        if ((e.ColumnIndex == 1) && ((dataView.SelectedCells.Count == 1)))
        {
            var scope = Scope.Instance;
            var row = dataView.Rows[e.RowIndex];
            var variable = row.DataBoundItem as Variable;

            if (scope.Variables.Contains(variable))
            {
                dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                    scope.GetGraph(variable).Color;
            }

            else
            {
                dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
            }
        }


回答2:

Now that's odd — the indentation shouldn't go that far.

Try cutting and pasting it in place and Visual Studio should fix it for you on pasting. This is what I get:

dataView.CellFormatting += (s, e) =>
{
    if ((e.ColumnIndex == 1) && ((dataView.SelectedCells.Count == 1)))
    {
        var scope = Scope.Instance;
        var row = dataView.Rows[e.RowIndex];
        var variable = row.DataBoundItem as Variable;

        if (scope.Variables.Contains(variable))
        {
            dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                scope.GetGraph(variable).Color;
        }

        else
        {
            dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
        }
    }
};