-
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
Enhancements to running code in a terminal #1432
Changes from 9 commits
3412ec1
fff01c9
fbdb935
c321fb1
840b6ef
03322b7
2c4a166
748fdb6
a1ee823
057ff8f
86d17ea
578c359
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 @@ | ||
Remove empty spaces from the selected text of the active editor when executing in a terminal. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Save the python file before running it in the terminal using the command/menu `Run Python File in Terminal`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add `Ctrl+Enter` keyboard shortcut for `Run Selection/Line in Python Terminal`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add blank lines to seprate blocks of indented code (function defs, classes, and the like) to ensure the code can be run within a Python interactive prompt. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import io | ||
import os | ||
import sys | ||
import token | ||
import tokenize | ||
|
||
try: | ||
unicode | ||
except: | ||
unicode = str | ||
|
||
|
||
def normalizeLines(content): | ||
"""Removes empty lines and adds empty lines only to sepaparate | ||
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. Docstrings always have a single line summary that fits within the 80 column limit, then a newline, then anything extra. E.g. def normalize_lines(content):
"""Normalize blank lines for sending to the terminal.
Blank lines within a statement block are removed to prevent the REPL
from thinking the block is finished. Newlines are added to separate
top-level statements so that the REPL does not think there is a syntax
error.
"""
lines = content.splitlines(False)
# ... |
||
indented code. So that the code can be used for execution in | ||
the Python interactive prompt """ | ||
|
||
lines = content.splitlines(False) | ||
|
||
# Find out if we have any trailing blank lines | ||
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. Missing a period. |
||
has_blank_lines = len(lines[-1].strip()) == 0 or content.endswith(os.linesep) | ||
|
||
# Remove empty lines | ||
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. Missing a period. |
||
tokens = tokenize.generate_tokens(io.StringIO(content).readline) | ||
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. FYI that is undocumented in Python 3. Is there a reason you aren't using |
||
|
||
new_lines_to_remove = [] | ||
for toknum, _, spos, epos, line in tokens: | ||
if token.tok_name[toknum] == 'NL' and len(line.strip()) == 0 and spos[0] == epos[0]: | ||
new_lines_to_remove.append(spos[0] - 1) | ||
|
||
for line_index in reversed(new_lines_to_remove): | ||
lines.pop(line_index) | ||
|
||
# Add new lines just before every dedent | ||
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. Missing a period. |
||
content = os.linesep.join(lines) | ||
tokens = tokenize.generate_tokens(io.StringIO(content).readline) | ||
dedented_lines = [] | ||
for toknum, _, spos, epos, line in tokens: | ||
if toknum == token.DEDENT and spos[0] == epos[0] and spos[0] <= len(lines): | ||
index = spos[0] - 1 | ||
if not index in dedented_lines: | ||
dedented_lines.append(index) | ||
|
||
for line_index in reversed(dedented_lines): | ||
line = lines[line_index] | ||
indent_size = line.index(line.strip()) | ||
indentation = line[0:indent_size] | ||
lines.insert(line_index, indentation) | ||
|
||
sys.stdout.write(os.linesep.join(lines) + (os.linesep if has_blank_lines else '')) | ||
sys.stdout.flush() | ||
|
||
|
||
if __name__ == '__main__': | ||
contents = unicode(sys.argv[1]) | ||
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. What are you trying to accomplish here? This is a rather dramatic decoding if you were handed bytes through sys.argv. 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. When sending via stdin for python2, bytes get sent in stdin. |
||
normalizeLines(contents) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Sample block 1 | ||
def square(x): | ||
return x**2 | ||
|
||
print('hello') | ||
# Sample block 2 | ||
a = 2 | ||
if a < 2: | ||
print('less than 2') | ||
|
||
else: | ||
print('more than 2') | ||
|
||
print('hello') | ||
# Sample block 3 | ||
for i in range(5): | ||
print(i) | ||
|
||
print('complete') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Sample block 1 | ||
def square(x): | ||
return x**2 | ||
|
||
print('hello') | ||
# Sample block 2 | ||
a = 2 | ||
if a < 2: | ||
print('less than 2') | ||
else: | ||
print('more than 2') | ||
|
||
print('hello') | ||
# Sample block 3 | ||
for i in range(5): | ||
print(i) | ||
|
||
print('complete') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def add(x, y): | ||
"""Adds x to y""" | ||
# Some comment | ||
return x + y | ||
|
||
v = add(1, 7) | ||
print(v) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def add(x, y): | ||
"""Adds x to y""" | ||
# Some comment | ||
|
||
return x + y | ||
|
||
v = add(1, 7) | ||
print(v) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
if True: | ||
print(1) | ||
print(2) | ||
|
||
print(3) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
if True: | ||
print(1) | ||
|
||
print(2) | ||
print(3) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class pc(object): | ||
def __init__(self, pcname, model): | ||
self.pcname = pcname | ||
self.model = model | ||
|
||
def print_name(self): | ||
print('Workstation name is', self.pcname, 'model is', self.model) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class pc(object): | ||
def __init__(self, pcname, model): | ||
self.pcname = pcname | ||
self.model = model | ||
|
||
def print_name(self): | ||
print('Workstation name is', self.pcname, 'model is', self.model) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
for i in range(10): | ||
print('a') | ||
for j in range(5): | ||
print('b') | ||
print('b2') | ||
for k in range(2): | ||
print('c') | ||
|
||
print('done with first loop') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
for i in range(10): | ||
print('a') | ||
for j in range(5): | ||
print('b') | ||
|
||
print('b2') | ||
|
||
for k in range(2): | ||
print('c') | ||
|
||
print('done with first loop') |
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.
normalize_lines
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.
aaarg..