emacs-lsp / lsp-python-ms

lsp-mode :heart: Microsoft's python language server
https://emacs-lsp.github.io/lsp-python-ms
BSD 3-Clause "New" or "Revised" License
190 stars 41 forks source link

Formatter with microsoft python language server #131

Closed deb75 closed 3 years ago

deb75 commented 4 years ago

hello,

I tried lsp-format-buffer and it answeers :

Capability not supported by the language server: "documentFormattingProvider"

Can i add a formatter along microsoft language server ?

Regards

deb75 commented 3 years ago

Hi,

I think it should be possible to bound lsp-format-buffer to a custom function calling yapf. I have it installed together with the palantir language server which is configured to call yapf when invoking lsp-format-buffer.

Unfortunately, I am not skilled enough in lisp to perform that. Should anyone know how to call an external program (python script) and apply it to the current buffer, could provide me an example how to do that.

jcs090218 commented 3 years ago

Try shell-command function?

seagle0128 commented 3 years ago

Try this snippet or https://github.com/JorisE/yapfify.

(defun lsp-python-ms-format-buffer ()
  (interactive)
  (when (and (executable-find "yapf") buffer-file-name)
    (call-process "yapf" nil nil nil "-i" buffer-file-name)))
(add-hook 'python-mode-hook
          (lambda ()
            (add-hook 'after-save-hook #'lsp-python-ms-format-buffer t t)))
deb75 commented 3 years ago

Thanks, this wonderfully makes the job. I still have to bound this function to C-c l = = when in a python buffer.

Regards