[python] Add lsp as possible python formatter
This commit is contained in:
parent
290cacf2a9
commit
53553e2307
|
@ -222,8 +222,8 @@ directory local variable in your project root. ~SPC f v d~ in Spacemacs. See
|
|||
The root of the project is detected with a =.git= directory or a =setup.cfg= file.
|
||||
|
||||
** Buffer formatting
|
||||
One of [[https://github.com/google/yapf][YAPF]] (the default) or [[https://github.com/ambv/black][black]] may be selected as the formatter, via
|
||||
=python-formatter=, as ='yapf= or ='black= respectively.
|
||||
One of [[https://github.com/google/yapf][YAPF]] (the default), [[https://github.com/ambv/black][black]] or =lsp= may be selected as the formatter, via
|
||||
=python-formatter=, as ='yapf=, ='black= or =lsp= respectively.
|
||||
|
||||
#+BEGIN_SRC emacs-lisp
|
||||
(setq-default dotspacemacs-configuration-layers '(
|
||||
|
@ -231,11 +231,11 @@ One of [[https://github.com/google/yapf][YAPF]] (the default) or [[https://githu
|
|||
#+END_SRC
|
||||
|
||||
The key binding ~SPC m =~ invokes the selected formatter on the current buffer
|
||||
when in python mode.
|
||||
when in non LSP python mode otherwise ~SPC m ==~ is used.
|
||||
|
||||
Note that YAPF and black may also be invoked unconditionally via
|
||||
=yapfify-buffer= and =blacken-buffer=, respectively, provided that they are
|
||||
installed.
|
||||
Note that a specific formatter may also be invoked unconditionally via
|
||||
=yapfify-buffer=, =blacken-buffer= or =lsp-format-buffer=, provided
|
||||
these are installed.
|
||||
|
||||
** Automatic buffer formatting on save
|
||||
To enable automatic buffer formatting on save set the variable
|
||||
|
@ -446,8 +446,8 @@ Live coding is provided by the [[https://github.com/donkirkby/live-py-plugin][li
|
|||
** Other Python commands
|
||||
|
||||
| Key binding | Description |
|
||||
|---------------+-----------------------------------------------------------------------------------|
|
||||
| ~SPC m ==~ | reformat the buffer using formatter specified in =python-formatter= |
|
||||
|--------------------------+-----------------------------------------------------------------------------------|
|
||||
| ~SPC m =~ or ~SPC m = =~ | reformat the buffer using default formatter specified in =python-formatter= |
|
||||
| ~SPC m d b~ | toggle a breakpoint using =wdb=, =ipdb=, =pudb=, =pdb= or =python3.7= (and above) |
|
||||
| ~SPC m g a~ | go to assignment using =anaconda-mode-find-assignments= (~C-o~ to jump back) |
|
||||
| ~SPC m g b~ | jump back |
|
||||
|
|
|
@ -29,8 +29,8 @@ and `mspyls'")
|
|||
"If non-nil, activate pipenv before enabling backend")
|
||||
|
||||
(defvar python-formatter 'yapf
|
||||
"The formatter to use. Possible values are `yapf' and
|
||||
`black'.")
|
||||
"The formatter to use. Possible values are `yapf',
|
||||
`black' and 'lsp'.")
|
||||
|
||||
(defvar python-format-on-save nil
|
||||
"If non-nil, automatically format code with formatter selected
|
||||
|
|
|
@ -349,17 +349,19 @@ to be called for each testrunner. "
|
|||
|
||||
(defun spacemacs//bind-python-formatter-keys ()
|
||||
"Bind the python formatter keys.
|
||||
Bind formatter to == for LSP and = for all other backends."
|
||||
Bind formatter to '==' for LSP and '='for all other backends."
|
||||
(spacemacs/set-leader-keys-for-major-mode 'python-mode
|
||||
(if (eq python-backend 'lsp)
|
||||
"=="
|
||||
"=") 'spacemacs/python-format-buffer))
|
||||
|
||||
(defun spacemacs/python-format-buffer ()
|
||||
"Bind possible python formatters."
|
||||
(interactive)
|
||||
(pcase python-formatter
|
||||
(`yapf (yapfify-buffer))
|
||||
(`black (blacken-buffer))
|
||||
(`lsp (lsp-format-buffer))
|
||||
(code (message "Unknown formatter: %S" code))))
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue