Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-formatting lambda-functions in Visual studio 2010

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;
                                                   }
                                               }
                                           };
like image 805
Peter17 Avatar asked Nov 22 '11 09:11

Peter17


People also ask

How to define a lambda in c++?

C++ 11 introduced lambda expressions to allow inline functions which can be used for short snippets of code that are not going to be reused and therefore do not require a name. In their simplest form a lambda expression can be defined as follows: [ capture clause ] (parameters) -> return-type { definition of method }

Why lambda function c++?

It is a convenient way to define an anonymous function object or functor. It is convenient because we can define it locally where we want to call it or pass it to a function as an argument. Lambda is easy to read too because we can keep everything in the same place.

How to capture lambda c++?

Much like functions can change the value of arguments passed by reference, we can also capture variables by reference to allow our lambda to affect the value of the argument. To capture a variable by reference, we prepend an ampersand ( & ) to the variable name in the capture.

Are there lambdas in c++?

A lambda can introduce new variables in its body (in C++14), and it can also access, or capture, variables from the surrounding scope. A lambda begins with the capture clause. It specifies which variables are captured, and whether the capture is by value or by reference.


2 Answers

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;
            }
        }
like image 136
Tod Avatar answered Sep 30 '22 12:09

Tod


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;
        }
    }
};
like image 30
BoltClock Avatar answered Sep 30 '22 13:09

BoltClock