Set .py tempfile suffix when using vi/emacs editor shortcuts. #12167
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Since the vi keybinding (v) and emacs mode keybinding (c-x c-e) are handled by prompt_toolkit directly, and the tempfile_suffix argument was not provided to prompt_toolkit, the tempfile created when the editor was opened via these keybindings was a .txt file, breaking syntax highlighting in some editors.
By passing the tempfile_suffix=".py" argument to the PromptSession constructor, this ensures that when the open_in_editor() method of the current buffer is called, the tempfile opened in the editor will always have a .py extension.
The open_input_in_editor(event) function associated with the f2 keybinding manually set the tempfile_suffix property of the current buffer before calling the buffer's open_in_editor() method. Passing the tempfile_suffix argument to the PromptSession constructor ensures that .py extension is always used, so setting the attribute manually is no longer required.
With these small changes, vi and emacs open_in_editor shortcuts produce a tempfile with the correct extension, and F2 continues to work correctly.