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

Ensure code is executed when last line of selected code is indented #2222

Merged
merged 4 commits into from
Sep 6, 2018

Conversation

DonJayamanne
Copy link

Fixes #2167

  • Title summarizes what is changing
  • Includes a news entry file (remember to thank yourself!)
  • Unit tests & code coverage are not adversely affected (within reason)
  • Works on all actively maintained versions of Python (e.g. Python 2.7 & the latest Python 3 release)
  • Works on Windows 10, macOS, and Linux (e.g. considered file system case-sensitivity)
  • [n/a] Dependencies are pinned (e.g. "1.2.3", not "^1.2.3")
  • [n/a] package-lock.json has been regenerated if dependencies have changed

@codecov
Copy link

codecov bot commented Jul 21, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@6af8f99). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #2222   +/-   ##
=========================================
  Coverage          ?   75.25%           
=========================================
  Files             ?      309           
  Lines             ?    14341           
  Branches          ?     2540           
=========================================
  Hits              ?    10793           
  Misses            ?     3538           
  Partials          ?       10

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6af8f99...3af012e. Read the comment docs.

@ericsnowcurrently ericsnowcurrently self-assigned this Aug 1, 2018
@DonJayamanne
Copy link
Author

Close and reopen for VSTS

@DonJayamanne DonJayamanne reopened this Aug 1, 2018
# 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 \
source.endswith('\n\n') or source.endswith('\r\n\r\n'):
Copy link
Member

Choose a reason for hiding this comment

The 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. :)

Copy link
Author

Choose a reason for hiding this comment

The 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?

Yes

How important is that case?

Very

ut realistically that's not an issue. :)

Good, lets leave it for now. as no one has reported an issue.

Copy link
Member

Choose a reason for hiding this comment

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

Please elaborate on why handling the "other" line ending matters.

Copy link
Author

Choose a reason for hiding this comment

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

If the file is saved on Windows then line endings will be \r\n, and if opened from a Mac we'll see see \r\n

Copy link
Author

Choose a reason for hiding this comment

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

I.e. cross platform support

Copy link
Member

Choose a reason for hiding this comment

The 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! :)

# 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 \
Copy link
Member

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?

Copy link
Author

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:

1. if True:
2.     print(1)
3.     # white space
4.     print(3)
  • Assume user selects line 1,2 and 3. Line three has an indentation.
  • If the last line has an indentation, its possible there's more code.

Copy link
Member

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 checking source.endswith('\n\n') or source.endswith('\r\n\r\n')?

# 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 \
source.endswith('\n\n') or source.endswith('\r\n\r\n'):
Copy link
Member

Choose a reason for hiding this comment

The 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.

# 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 \
Copy link
Member

@ericsnowcurrently ericsnowcurrently Aug 1, 2018

Choose a reason for hiding this comment

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

A line continuation (the trailing \) isn't necessary. Also, the binary operator should be on the beginning of the next line:

if ((...)
     or ...
     or ...
     ):
    ...

Note the parens wrapping the whole expression. Also putting the closing paren on its own line helps with readability.

# we have indented code.
if (len(lines) > 1 and len(''.join(lines[-2:])) == 0) or \
source.endswith('\n\n') or source.endswith('\r\n\r\n'):
trailing_newline = os.linesep * 2
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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'):
Copy link
Member

Choose a reason for hiding this comment

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

As above, you don't need that first condition, right?

Copy link
Author

Choose a reason for hiding this comment

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

yes we do

Copy link
Member

Choose a reason for hiding this comment

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

Please explain why.

# 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 \
source.endswith('\n\n') or source.endswith('\r\n\r\n'):
Copy link
Member

Choose a reason for hiding this comment

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

str.endswith() can take a tuple of strings to check:

source.endswith(('\n\n', '\r\n\r\n'))

Copy link
Author

Choose a reason for hiding this comment

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

WOWOWOWOWOWOWOWOWOWOWOW
Ok, I didn't know this was possible and have never seen this before...

# 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'):
Copy link
Member

Choose a reason for hiding this comment

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

As above, pass a tuple of possible endings to source.endswith().

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

@DonJayamanne
Copy link
Author

@ericsnowcurrently
Please re-review

@DonJayamanne
Copy link
Author

@Microsoft/pvsc-team please could someone re-review this PR

if len(lines[-1].strip()) == 0 or source.endswith('\n'):
trailing_newline = '\n'
elif len(lines[-1].strip()) == 0 or source.endswith(('\n', '\r\n')):
trailing_newline = os.linesep
else:
trailing_newline = ''

Copy link
Member

Choose a reason for hiding this comment

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

There's a spot further down that uses \n instead of os.linesep (line 114 of the new file, sys.stdout.write('\n'.join(lines) + trailing_newline)). If your intention is to always use the native line separator then this needs to change.

# 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) \
Copy link
Member

Choose a reason for hiding this comment

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

I still think (len(lines) > 1 and len(''.join(lines[-2:])) == 0) is unnecessary.

Copy link
Author

Choose a reason for hiding this comment

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

To account for spaces

# 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', '\r\n')):
Copy link
Member

Choose a reason for hiding this comment

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

Likewise, len(lines[-1].strip()) == 0 is unnecessary, no?

Copy link
Author

Choose a reason for hiding this comment

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

To account for spaces

# we have indented code.
if (len(lines) > 1 and len(''.join(lines[-2:])) == 0) or \
source.endswith('\n\n') or source.endswith('\r\n\r\n'):
trailing_newline = os.linesep * 2
Copy link
Member

Choose a reason for hiding this comment

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

never mind

Copy link
Member

@ericsnowcurrently ericsnowcurrently left a comment

Choose a reason for hiding this comment

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

Other than the question of why the first condition is necessary, LGTM. Please address that before merging this.

@brettcannon
Copy link
Member

@DonJayamanne can we merge this?

@DonJayamanne DonJayamanne merged commit b5e1e44 into microsoft:master Sep 6, 2018
@DonJayamanne DonJayamanne deleted the issue2167NewLine branch October 2, 2018 22:46
@aminevsaziz
Copy link

aminevsaziz commented Oct 2, 2018

@DonJayamanne This issue still exists for me.

@d3r3kk
Copy link

d3r3kk commented Oct 3, 2018

@aminevsaziz Please open a new issue on our github if this issue isn't resolved for you, we can always use the help tracking down and fixing bugs! (Note, I've edited your comment to come off less 👿 and more 😺).

@lock lock bot locked as resolved and limited conversation to collaborators Jul 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shift+Enter doesn't fully run code when last line of code is indented
5 participants