-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Ensure code is executed when last line of selected code is indented #2222
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Ensure code is executed when the last line of selected code is indented. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,9 +76,15 @@ def normalize_lines(source): | |
|
||
""" | ||
lines = source.splitlines(False) | ||
# If we have two blank lines, then add two blank lines. | ||
# Do not trim the spaces, if we have blank lines with spaces, its possible | ||
# we have indented code. | ||
if (len(lines) > 1 and len(''.join(lines[-2:])) == 0) or \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A line continuation (the trailing if ((...)
or ...
or ...
):
... Note the parens wrapping the whole expression. Also putting the closing paren on its own line helps with readability. |
||
source.endswith('\n\n') or source.endswith('\r\n\r\n'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just in case the source file has the "other" line ending, right? How important is that case? I was going to point out the possibility of other line endings, but realistically that's not an issue. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes
Very
Good, lets leave it for now. as no one has reported an issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please elaborate on why handling the "other" line ending matters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the file is saved on Windows then line endings will be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I.e. cross platform support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I was thinking about literal strings. However, for those we should be fine. So carry on! :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this might be less confusing if you put each condition on its own line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
source.endswith(('\n\n', '\r\n\r\n')) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WOWOWOWOWOWOWOWOWOWOWOW |
||
trailing_newline = os.linesep * 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, is changing it from what it was to the platform-native line separator always the right thing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is for sending the code to the terminal. We're not modifying the code in the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would a non-native line ending in a file necessarily be meant to be sent to the terminal as a native line ending? Am I missing some context here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never mind |
||
# Find out if we have any trailing blank lines | ||
if len(lines[-1].strip()) == 0 or source.endswith('\n'): | ||
trailing_newline = '\n' | ||
elif len(lines[-1].strip()) == 0 or source.endswith('\n') or source.endswith('\r\n'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, you don't need that first condition, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes we do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please explain why. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, pass a tuple of possible endings to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
trailing_newline = os.linesep | ||
else: | ||
trailing_newline = '' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a spot further down that uses |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point of this condition if you are checking the other possibilities in the other two conditions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to look for code that looks like the following:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm saying is, isn't
(len(lines) > 1 and len(''.join(lines[-2:])) == 0)
redundant when also checkingsource.endswith('\n\n') or source.endswith('\r\n\r\n')
?