Skip to content

Commit

Permalink
First draft
Browse files Browse the repository at this point in the history
  • Loading branch information
galaunay committed Sep 27, 2020
1 parent 03a7517 commit 977591b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
8 changes: 6 additions & 2 deletions docs/ide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -773,13 +773,17 @@ Refactoring

Format code using the available formatter.

This command formats code using the first formatter found amongst
`yapf`_ , `autopep8`_ and `black`_.
If a region is selected, only that region is formatted.
Otherwise current buffer is formatted.

.. option:: elpy-formatter

Allow to select the formatter you want to use amongst
`yapf`_ , `autopep8`_ and `black`_.

`yapf`_ and `autopep8`_ can be configured with style files placed in
the project root directory (determined by ``elpy-project-root``).
`black`_ can be configured in the ``pyproject.toml`` file of your project.

.. _autopep8: https://github.com/hhatto/autopep8
.. _yapf: https://github.com/google/yapf
Expand Down
40 changes: 28 additions & 12 deletions elpy.el
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,18 @@ option is `pdb'."
:type 'boolean
:group 'elpy)


(defcustom elpy-formatter nil
"Auto formatter used by `elpy-format-code'.
if nil, use the first formatter found amongst
`yapf` , `autopep8` and `black`."
:type '(choice (const :tag "First one found" nil)
(const :tag "Yapf" yapf)
(const :tag "autopep8" autopep8)
(const :tag "Black" black))
:group 'elpy)

(defcustom elpy-syntax-check-command "flake8"
"The command to use for `elpy-check'."
:type 'string
Expand Down Expand Up @@ -2279,21 +2291,25 @@ prefix argument is given, prompt for a symbol from the user."
;;;;;;;;;;;;;;;;;;;;;
;;; Code reformatting


(defun elpy-format-code ()
"Format code using the available formatter."
(interactive)
(cond
((elpy-config--package-available-p "yapf")
(when (interactive-p) (message "Autoformatting code with yapf."))
(elpy-yapf-fix-code))
((elpy-config--package-available-p "autopep8")
(when (interactive-p) (message "Autoformatting code with autopep8."))
(elpy-autopep8-fix-code))
((elpy-config--package-available-p "black")
(when (interactive-p) (message "Autoformatting code with black."))
(elpy-black-fix-code))
(t
(message "Install yapf/autopep8 to format code."))))
(let ((elpy-formatter (or elpy-formatter
(catch 'available
(dolist (formatter '(yapf autopep8 black))
(when (elpy-config--package-available-p
formatter)
(throw 'available formatter)))))))
(unless elpy-formatter
(error "No formatter installed, please install one using `elpy-config'."))
(unless (elpy-config--package-available-p elpy-formatter)
(error "The '%s' formatter is not installed, please install it using `elpy-config' or choose another one using `elpy-formatter'."
elpy-formatter))
(when (interactive-p) (message "Autoformatting code with %s."
elpy-formatter))
(funcall (intern (format "elpy-%s-fix-code" elpy-formatter)))))


(defun elpy-yapf-fix-code ()
"Automatically formats Python code with yapf.
Expand Down

0 comments on commit 977591b

Please sign in to comment.