Show More
@@ -116,6 +116,20 b' def black_reformat_handler(text_before_cursor):' | |||
|
116 | 116 | return formatted_text |
|
117 | 117 | |
|
118 | 118 | |
|
119 | def yapf_reformat_handler(text_before_cursor): | |
|
120 | from yapf.yapflib import file_resources | |
|
121 | from yapf.yapflib import yapf_api | |
|
122 | ||
|
123 | style_config = file_resources.GetDefaultStyleForDir(os.getcwd()) | |
|
124 | formatted_text, was_formatted = yapf_api.FormatCode(text_before_cursor, style_config=style_config) | |
|
125 | if was_formatted: | |
|
126 | if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"): | |
|
127 | formatted_text = formatted_text[:-1] | |
|
128 | return formatted_text | |
|
129 | else: | |
|
130 | return text_before_cursor | |
|
131 | ||
|
132 | ||
|
119 | 133 | class TerminalInteractiveShell(InteractiveShell): |
|
120 | 134 | mime_renderers = Dict().tag(config=True) |
|
121 | 135 | |
@@ -185,7 +199,7 b' class TerminalInteractiveShell(InteractiveShell):' | |||
|
185 | 199 | |
|
186 | 200 | autoformatter = Unicode( |
|
187 | 201 | "black", |
|
188 | help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`", | |
|
202 | help="Autoformatter to reformat Terminal code. Can be `'black'`, `'yapf'` or `None`", | |
|
189 | 203 | allow_none=True |
|
190 | 204 | ).tag(config=True) |
|
191 | 205 | |
@@ -232,6 +246,8 b' class TerminalInteractiveShell(InteractiveShell):' | |||
|
232 | 246 | self.reformat_handler = lambda x:x |
|
233 | 247 | elif formatter == 'black': |
|
234 | 248 | self.reformat_handler = black_reformat_handler |
|
249 | elif formatter == 'yapf': | |
|
250 | self.reformat_handler = yapf_reformat_handler | |
|
235 | 251 | else: |
|
236 | 252 | raise ValueError |
|
237 | 253 |
General Comments 0
You need to be logged in to leave comments.
Login now