Skip to content

Improve C# conditional support #1191

Open
@JohnEfford

Description

@JohnEfford

Currently Cursorless supports conditions in the conditional part of: 'if', 'while' and 'for' statements and also for ternary expressions so given:

  public bool  GreaterThan(
      int num1,
      int num2)
  =>num1 > num2; //Greater than expression

Then if the mark on r of GreaterThan then "Take condition red" selects GreaterThan(a,b) correctly:

public void ForLoop(int a)
{
    for (int b = 0; GreaterThan(a,b); b++)
    {
        Console.WriteLine("Badger");
    }
}

The following do not work in the same way, but should:

public int SwitchExpression(
    int a)
    => a switch
    {
        var i when GreaterThan(i, 0) => i,
        _ => 1 * -1
    };

public void SwitchStatement(int a)
{
    switch (a)
    {
        case var i when GreaterThan(i,0):
        {
            Console.WriteLine("Positive");

        } break;
        default: Console.WriteLine("Zero or negative"); break;
    }
}

public void DoWhile(int a, int b)
{
    do
    {
        b++;
    }while(GreaterThan(a,b));
}

The above all should work by knowing that a condition must be present in that lexical position however if the expression contains a conditional operator it is also possible to infer that the expression is a condition. It is possible to infer all of the expressions, on right hand side of the assignments, as conditions without recourse to a compilation for the statements below:

  public  void BooleanExpressionStatements(
      int num1, int num2)
  {
      var equals = num1 == num2; //Equals expression
      var notEquals = num2 != num1; //Not equals expression
      var greaterThem = num1 > num2; //Greater than expression
      var lessThan = num2 < num1; //Less than expression
      var greaterThanOrEqualTo =1 >= 2; //Greater than or equals expression
      var lessThanOrEqualTo = num2 <= Value(num1);//Less than or equals expression
      var notExpression = !GreaterThan(num1, num2);//Logical not expression
      var and = equals && notEquals; //Ampersand ampersand expression
      var or = notEquals || greaterThem; //Logical or expression
      var isExpressin = num1 is int; //Is Expression
      var combined = num1 == num2 || GreaterThan(num1, num2); //Ensure find outer most expression when token on argument in GreaterThan
      var initialExpressionNotBoolean = (1+num1) == num2; ;//Ensure find outer expression when mark on +
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions