Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PY-15046: Invert 'if' statement implementation #1350

Closed

Conversation

outring
Copy link
Contributor

@outring outring commented Apr 26, 2020

Invert 'if' statement implementation

Inversion of if statements and conditionals is implemented. YouTrack issue PY-15046

What's supported

  • Recursive conditions inversion
    • Simple comparisons and coerced expressions
    • not in and is not expressions
    • Assignment expressions
    • Multiline expressions (as good as I could)
    • and and or expressions with precence support
  • If statements
    • if-only statements
    • if-else statements
    • Module, function and loops scopes with corresponding termination statements (return, raise, continue, break)
    • Comments switching
      • Inline comments after if ...: and else:
      • Multiple preceding line comments before if, else and if followup code.
      • Service comments are respected (type, pylint, noinspection)
  • Conditional statements

Issues

  • Had to mark switched statements for reformat in order to normalize indentation, would love to learn how to indent moved nodes explicitly without full reformat
  • Preceding comments switch might be too smart and could be simplified if the behavior is confusing
  • Detection of broken statements is not implemented because I couldn't find a good suitable existing examples in existing Py code, tests are there and need to be fixed
  • When missing else branch is being generated, pass is being focused but probably it's too much or must be implemented with editable placeholder functionality
  • Terminated statement detection using ControlFlow looks odd, not sure if it used the optimal way

Copy link
Contributor

@akniazev akniazev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding your questions:
I see no problem with preceding comment switch, marking to reformat and control flow usage.
Selecting pass when else branch is generated is a good idea, just move the caret there as well, otherwise it's confusing.
To detect broken conditions you can use PsiTreeUtil.hasErrorElements(ifStatement.ifPart.condition), it will return true if there are parse errors in the condition.
We've also discussed with you that something has to be done for conditions like 1 < x < 3.

Don't hesitate to contact me with any questions and thank you for the great work you're doing. 🙂

@outring outring force-pushed the feature/py-invert-if-statement branch from 6e607fc to 15f0895 Compare June 14, 2020 19:12
@outring outring force-pushed the feature/py-invert-if-statement branch from 15f0895 to b05fd74 Compare June 14, 2020 20:21
@SergeyZh SergeyZh closed this in 0831f57 Oct 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants