##// END OF EJS Templates
enable formatting by default
Matthias Bussonnier -
Show More
@@ -102,10 +102,16 b' else:'
102 _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
102 _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
103
103
104 def black_reformat_handler(text_before_cursor):
104 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 """
105 import black
110 import black
111
106 formatted_text = black.format_str(text_before_cursor, mode=black.FileMode())
112 formatted_text = black.format_str(text_before_cursor, mode=black.FileMode())
107 if not text_before_cursor.endswith('\n') and formatted_text.endswith('\n'):
113 if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"):
108 formatted_text = formatted_text[:-1]
114 formatted_text = formatted_text[:-1]
109 return formatted_text
115 return formatted_text
110
116
111
117
@@ -176,7 +182,8 b' class TerminalInteractiveShell(InteractiveShell):'
176 sequence to complete.""",
182 sequence to complete.""",
177 ).tag(config=True)
183 ).tag(config=True)
178
184
179 autoformatter = Unicode(None,
185 autoformatter = Unicode(
186 "black",
180 help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`",
187 help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`",
181 allow_none=True
188 allow_none=True
182 ).tag(config=True)
189 ).tag(config=True)
@@ -210,9 +217,7 b' class TerminalInteractiveShell(InteractiveShell):'
210 if self.pt_app:
217 if self.pt_app:
211 self.pt_app.editing_mode = getattr(EditingMode, change.new.upper())
218 self.pt_app.editing_mode = getattr(EditingMode, change.new.upper())
212
219
213 @observe('autoformatter')
220 def _set_formatter(self, formatter):
214 def _autoformatter_changed(self, change):
215 formatter = change.new
216 if formatter is None:
221 if formatter is None:
217 self.reformat_handler = lambda x:x
222 self.reformat_handler = lambda x:x
218 elif formatter == 'black':
223 elif formatter == 'black':
@@ -220,6 +225,11 b' class TerminalInteractiveShell(InteractiveShell):'
220 else:
225 else:
221 raise ValueError
226 raise ValueError
222
227
228 @observe("autoformatter")
229 def _autoformatter_changed(self, change):
230 formatter = change.new
231 self._set_formatter(formatter)
232
223 @observe('highlighting_style')
233 @observe('highlighting_style')
224 @observe('colors')
234 @observe('colors')
225 def _highlighting_style_changed(self, change):
235 def _highlighting_style_changed(self, change):
@@ -561,6 +571,7 b' class TerminalInteractiveShell(InteractiveShell):'
561 self.init_prompt_toolkit_cli()
571 self.init_prompt_toolkit_cli()
562 self.init_term_title()
572 self.init_term_title()
563 self.keep_running = True
573 self.keep_running = True
574 self._set_formatter(self.autoformatter)
564
575
565
576
566 def ask_exit(self):
577 def ask_exit(self):
@@ -31,6 +31,7 b' zip_safe = False'
31 install_requires =
31 install_requires =
32 setuptools>=18.5
32 setuptools>=18.5
33 jedi>=0.16
33 jedi>=0.16
34 black
34 decorator
35 decorator
35 pickleshare
36 pickleshare
36 traitlets>=5
37 traitlets>=5
General Comments 0
You need to be logged in to leave comments. Login now