##// END OF EJS Templates
Add support for autoformatting using yapf
Anton Älgmyr -
Show More
@@ -116,6 +116,20 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(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 class TerminalInteractiveShell(InteractiveShell):
133 class TerminalInteractiveShell(InteractiveShell):
120 mime_renderers = Dict().tag(config=True)
134 mime_renderers = Dict().tag(config=True)
121
135
@@ -185,7 +199,7 b' class TerminalInteractiveShell(InteractiveShell):'
185
199
186 autoformatter = Unicode(
200 autoformatter = Unicode(
187 "black",
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 allow_none=True
203 allow_none=True
190 ).tag(config=True)
204 ).tag(config=True)
191
205
@@ -232,6 +246,8 b' class TerminalInteractiveShell(InteractiveShell):'
232 self.reformat_handler = lambda x:x
246 self.reformat_handler = lambda x:x
233 elif formatter == 'black':
247 elif formatter == 'black':
234 self.reformat_handler = black_reformat_handler
248 self.reformat_handler = black_reformat_handler
249 elif formatter == 'yapf':
250 self.reformat_handler = yapf_reformat_handler
235 else:
251 else:
236 raise ValueError
252 raise ValueError
237
253
General Comments 0
You need to be logged in to leave comments. Login now