seagle0128 / .emacs.d

Centaur Emacs - A Fancy and Fast Emacs Configuration
https://seagle0128.github.io/.emacs.d/
GNU General Public License v3.0
1.97k stars 270 forks source link

How to disable auto-formatting #175

Closed rotywonka closed 4 years ago

rotywonka commented 4 years ago

Hi,

Every time I edit a python script, the python linter keeps auto-formatting the buffer before saving it. Is there a way to disable this feature?

seagle0128 commented 4 years ago
(with-after-load 'lsp-mode
  (add-hook 'lsp-mode-hook
     (lambda ()
        (remove-hook 'before-save-hook #'lsp-format-buffer t)
        (remove-hook 'before-save-hook #'lsp-organize-imports t))
))
rotywonka commented 4 years ago

I pasted that snippet into my init.el and it still auto formats the buffer whenever I save the file.

seagle0128 commented 4 years ago

Try this in custom-post.el

(with-after-load 'lsp-mode
  (add-hook 'lsp-mode-hook
     (lambda ()
        (remove-hook 'before-save-hook #'lsp-format-buffer t)
        (remove-hook 'before-save-hook #'lsp-organize-imports t))
))

or

(with-after-load 'lsp-mode
(remove-hook 'lsp-mode-hook
                (lambda ()
                  (lsp-enable-which-key-integration)
                  (unless
                      (derived-mode-p 'c-mode 'c++-mode)
                    (add-hook 'before-save-hook
                              (function lsp-format-buffer)
                              t t)
                    (add-hook 'before-save-hook
                              (function lsp-organize-imports)
                              t t)))))
rotywonka commented 4 years ago

I did that and it still doesn't stop auto-formatting :(

What minor modes do Centaur Emacs execute whenever it opens a python script?

rotywonka commented 4 years ago

It seems that yapf-mode is actually being called before a python file is saved. Disabling yapf-mode fixes this issue.

seagle0128 commented 4 years ago

Yes. I may remove it.

cliffzhao commented 2 years ago

Hi @seagle0128,

Thanks for building Centaur Emacs!

I recently started using it (latest version) and almost use the default setup. Until now, every thing works great except some auto-formatting issues. Is there any way to disable auto formatting with current version of Centaur Emacs? I have tried adding the script mentioned in your earlier replies in custom.el, but it doesn't work.

By the way, when I type python code, the initial indentation seems to be 2 spaces (for example, after typing "def func():" and Enter, the new line will start with 2 spaces indentation). I will get correct indentation (i.e., 4 spaces) after saving, and the correction is done by auto formatting. Is this expected?

I appreciate your help!

seagle0128 commented 2 years ago

@cliffzhao To be clear, are you using editorconfig or prettier in your project?

cliffzhao commented 2 years ago

@cliffzhao To be clear, are you using editorconfig or prettier in your project?

Hi Vincent (@seagle0128),

When I open a python script, I notice that both modes are on (prettier in Local Modes and editorconfig in Global Modes). After I manually disable prettier mode, the indentation becomes correct. So I think I'm using prettier in the project. Actually, I only added sourcery and tabnine support in custom.el and keep others as default.

In addition, after I disable both prettier mode and editorconfig mode manually, there are still some auto formatting when I save the file. For example, I type following:

net = nn.Sequential( nn.Linear(10,20), nn.Linear(20, 20), nn.Linear(20,1) )

When I save the file, it will auto format to:

net = nn.Sequential(nn.Linear(10, 20), nn.Linear(20, 20), nn.Linear(20, 1))

I'm wondering this kind of auto formatting is handled by which module. Sorry for my naive questions and thanks for your help again!

seagle0128 commented 2 years ago

@cliffzhao I've disable global-prettier-mode and only enable in web modes. If you are using lsp-mode, please check centaur-lsp-format-on-save-ignore-modes. Of course it should respect editorconfig.

cliffzhao commented 2 years ago

Thanks @seagle0128. I have noticed the new commit! After the update, now, everything works perfectly.