Show More
@@ -116,6 +116,22 b' def black_reformat_handler(text_before_cursor):' | |||||
116 | return formatted_text |
|
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( | |||
|
125 | text_before_cursor, style_config=style_config | |||
|
126 | ) | |||
|
127 | if was_formatted: | |||
|
128 | if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"): | |||
|
129 | formatted_text = formatted_text[:-1] | |||
|
130 | return formatted_text | |||
|
131 | else: | |||
|
132 | return text_before_cursor | |||
|
133 | ||||
|
134 | ||||
119 | class TerminalInteractiveShell(InteractiveShell): |
|
135 | class TerminalInteractiveShell(InteractiveShell): | |
120 | mime_renderers = Dict().tag(config=True) |
|
136 | mime_renderers = Dict().tag(config=True) | |
121 |
|
137 | |||
@@ -185,7 +201,7 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
185 |
|
201 | |||
186 | autoformatter = Unicode( |
|
202 | autoformatter = Unicode( | |
187 | "black", |
|
203 | "black", | |
188 | help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`", |
|
204 | help="Autoformatter to reformat Terminal code. Can be `'black'`, `'yapf'` or `None`", | |
189 | allow_none=True |
|
205 | allow_none=True | |
190 | ).tag(config=True) |
|
206 | ).tag(config=True) | |
191 |
|
207 | |||
@@ -232,6 +248,8 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
232 | self.reformat_handler = lambda x:x |
|
248 | self.reformat_handler = lambda x:x | |
233 | elif formatter == 'black': |
|
249 | elif formatter == 'black': | |
234 | self.reformat_handler = black_reformat_handler |
|
250 | self.reformat_handler = black_reformat_handler | |
|
251 | elif formatter == "yapf": | |||
|
252 | self.reformat_handler = yapf_reformat_handler | |||
235 | else: |
|
253 | else: | |
236 | raise ValueError |
|
254 | raise ValueError | |
237 |
|
255 |
General Comments 0
You need to be logged in to leave comments.
Login now