Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df3248f

Browse files
committed
enable formatting by default
1 parent cc07a8e commit df3248f

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

IPython/terminal/interactiveshell.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,16 @@ def get_default_editor():
102102
_use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
103103

104104
def black_reformat_handler(text_before_cursor):
105+
"""
106+
We do not need to protect against error,
107+
this is taken care at a higher level where any reformat error is ignored.
108+
Indeed we may call reformatting on incomplete code.
109+
"""
105110
import black
111+
106112
formatted_text = black.format_str(text_before_cursor, mode=black.FileMode())
107-
if not text_before_cursor.endswith('\n') and formatted_text.endswith('\n'):
108-
formatted_text = formatted_text[:-1]
113+
if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"):
114+
formatted_text = formatted_text[:-1]
109115
return formatted_text
110116

111117

@@ -176,7 +182,8 @@ def debugger_cls(self):
176182
sequence to complete.""",
177183
).tag(config=True)
178184

179-
autoformatter = Unicode(None,
185+
autoformatter = Unicode(
186+
"black",
180187
help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`",
181188
allow_none=True
182189
).tag(config=True)
@@ -210,16 +217,19 @@ def _editing_mode(self, change):
210217
if self.pt_app:
211218
self.pt_app.editing_mode = getattr(EditingMode, change.new.upper())
212219

213-
@observe('autoformatter')
214-
def _autoformatter_changed(self, change):
215-
formatter = change.new
220+
def _set_formatter(self, formatter):
216221
if formatter is None:
217222
self.reformat_handler = lambda x:x
218223
elif formatter == 'black':
219224
self.reformat_handler = black_reformat_handler
220225
else:
221226
raise ValueError
222227

228+
@observe("autoformatter")
229+
def _autoformatter_changed(self, change):
230+
formatter = change.new
231+
self._set_formatter(formatter)
232+
223233
@observe('highlighting_style')
224234
@observe('colors')
225235
def _highlighting_style_changed(self, change):
@@ -561,6 +571,7 @@ def __init__(self, *args, **kwargs):
561571
self.init_prompt_toolkit_cli()
562572
self.init_term_title()
563573
self.keep_running = True
574+
self._set_formatter(self.autoformatter)
564575

565576

566577
def ask_exit(self):

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ zip_safe = False
3131
install_requires =
3232
setuptools>=18.5
3333
jedi>=0.16
34+
black
3435
decorator
3536
pickleshare
3637
traitlets>=5

0 commit comments

Comments
 (0)