I'm still learning to use/configure lsp-mode so please forgive me if I misunderstand something.
When I'm using lsp-mode for editing C++ code (doesn't seem to be specific to C++ code though), the lsp--apply-text-edits is being applied at some very annoying moments, for instance when commenting out some code.
This might not be that much of an issue if I had actually figured out how to configure lsp-mode to format my code correctly, but I'm fairly certain that I would like to use clang-format for formatting my C++ code anyway.
Is there a way to disable that behavior or am I missing something?
Looking through the source code with my limited lisp knowledge that doesn't seem to be the case.
Thanks a lot and thanks for this wonderful Emacs mode.
I think you are looking for lsp-enable-on-type-formatting
Thanks a lot for the quick reply.
I've tried setting that to nil, but that doesn't seem to change anything. I still get my code reformatted with the message "Applying [some number] edits to [some file] ...done".
Try M-x debug-on-entry RET lsp--apply-text-edits and paste the callstack.
Thanks once again for the quick reply.
Hope this is useful:
* lsp--apply-text-edits((#<hash-table equal 2/65 0x45115b1> #<hash-table equal 2/65 0x5d2ff91> #<hash-table equal 2/65 0x6029c5d>))
#f(compiled-function () #<bytecode 0x1769e3d>)()
#f(compiled-function (result) #<bytecode 0x1769e49>)([#<hash-table equal 2/65 0x45115b1> #<hash-table equal 2/65 0x5d2ff91> #<hash-table equal 2/65 0x6029c5d>])
lsp--parser-on-message(#<hash-table equal 3/65 0x5cfb0fd> #s(lsp--workspace :ewoc nil :server-capabilities #<hash-table equal 17/65 0xea67c1> :registered-server-capabilities nil :root "/home/klau/code/tardis_dev" :client #s(lsp--client :language-id nil :add-on? nil :new-connection (:connect #f(compiled-function (filter sentinel name environment-fn) #<bytecode 0x159d275>) :test\? #f(compiled-function () #<bytecode 0x159d28d>)) :ignore-regexps nil :ignore-messages nil :notification-handlers #<hash-table equal 0/65 0x159d299> :request-handlers #<hash-table equal 0/65 0x159d2b9> :response-handlers #<hash-table eql 1/65 0x159d2d9> :prefix-function nil :uri-handlers #<hash-table equal 0/65 0x159dbb9> :action-handlers #<hash-table equal 0/65 0x159d7b9> :major-modes (c-mode c++-mode objc-mode) :activation-fn nil :priority -1 :server-id clangd :multi-root nil :initialization-options nil :custom-capabilities nil :library-folders-fn nil :before-file-open-fn nil :initialized-fn nil :remote? nil :completion-in-comments? nil :path->uri-fn nil :uri->path-fn nil :environment-fn nil :after-open-fn nil :async-request-handlers #<hash-table equal 0/65 0x159e22d> :download-server-fn nil :download-in-progress? nil :buffers nil) :host-root nil :proc #<process clangd> :cmd-proc #<process clangd> :buffers (#<buffer bootp_server.h> #<buffer bootp_server.cpp> #<buffer bootp_tester.cpp> #<buffer udp_v4_socket.h>) :semantic-highlighting-faces nil :extra-client-capabilities nil :status initialized :metadata #<hash-table equal 0/65 0x130371d> :watches #<hash-table equal 0/65 0x132ca7d> :workspace-folders nil :last-id 0 :status-string nil :shutdown-action nil :diagnostics #<hash-table equal 1/65 0x132ca51>))
#f(compiled-function (proc input) #<bytecode 0x1573305>)(#<process clangd> "Content-Length: 40\015\n\015\n{\"id\":11600,\"jsonrpc\":\"2.0\",\"result\":[]}Content-Length: 344\015\n\015\n{\"id\":11601,\"jsonrpc\":\"2.0\",\"result\":[{\"newText\":\"\\n // \",\"range\":{\"end\":{\"character\":81,\"line\":21},\"start\":{\"character\":80,\"line\":21}}},{\"newText\":\"\\n // \",\"range\":{\"end\":{\"character\":148,\"line\":21},\"start\":{\"character\":147,\"line\":21}}},{\"newText\":\"\\n \",\"range\":{\"end\":{\"character\":0,\"line\":22},\"start\":{\"character\":162,\"line\":21}}}]}")
Sorry, but it seems like all the useful info is not here since this is a callback. Can you do: (setq lsp-print-io t), reproduce the issue, then lsp-workspace-show-log and paste the content here?
Sure. I've attached it here:
Looks mostly like it's a log of the communication with clangd.
I see:
[Trace - 03:38:30 ] Received response 'textDocument/onTypeFormatting - (76)' in 15ms.
Can you set lsp-enable-on-type-formatting to nil and reopen the buffer?
Sure:
How do you set lsp-enable-on-type-formatting to nil?
Put this in your config and restart emacs - it will verify that it has been set correctly to nil. If you get warning you haven't set it correctly.
(with-eval-aflter-load 'lsp-mode
(add-hook 'lsp-managed-mode-hook
(lambda ()
(when lsp-enable-on-type-formatting (warn "You have lsp-enable-on-type-formatting set to t")))))
I tested it with set-variable for a buffer where I experienced the issue.
Setting it in my emacs config (I get lsp and other packages from use-package) doesn't make a difference and the warning doesn't appear. I checked the *Messages* buffer as well.
It is extremely likely that I'm doing something stupid as I'm definitely not very experienced in elisp or configuring emacs in general (although I've used emacs for quite a few years), so thanks a lot for your patience and great help.
I can attach my emacs config if that is any help?
I tested it with set-variable for a buffer where I experienced the issue.
You should either use (setq lsp-enable-on-type-formatting nil) or M-x customize-variable RET lsp-enable-on-type-formatting RET . Also, make sure you are using the latest version of lsp-mode. If this is not sufficient we should put some debug markers to find out who is enabling the on-type formatting functionality.
OK, this is embarrassing.
I am very certain that I disabled it in my config, made sure to reload the config/restart emacs and didn't see a difference, but when I try to do the same now, autoformatting is indeed disabled.
Might be that I have updated lsp-mode and that made a difference, but it's much more likely just me not being thorough enough when testing that I had changed the setting.
Thanks so much for your help. Not sure I would have had the same patience if I were you :-)
I had exactly the same problem, which was fixed after setting lsp-enable-indentation to nil. Maybe the variable should have a more descriptive name? :)
@Maverobot feel free to provide a PR and suggest a better name.
@Maverobot I would like to. However I just started using lsp since yesterday... What does lsp-enable-indentation actually do?
@Maverobot thanks, setting lsp-enable-indentation to nil helps to disable autoformatting (Applying [some number] edits to [some file] ...done). I use C++ with ccls.