【求助】lsp-java如何禁用关键词后空格的自动删除

环境: MacOS,Emacs 28.2 场景: lsp-java编辑java文件时,如果是关键字(class、package等),则会自动将关键字后的空格自动删除掉,需要先输入后续信息,再返回插入关键字,想问下哪位老兄有没有碰到类似的问题,求助一下,谢谢! 关联信息: lsp-mode相关配置: (setq lsp-java-format-on-type-enabled nil lsp-java-format-settings-url nil lsp-java-format-enabled nil lsp-enable-on-type-formatting nil lsp-trim-trailing-whitespace nil) 另用eglot没有这个问题,十分感谢!

1 个赞

什么时候会自动删除,在输入class之后,然后按下空格的时候?按下空格会看到插入了一个空格然后又立即自动删除了这个空格?

嗯,是的。感谢回复!

检查空格键是不是self-insert-command,有没有被advice,再检查post-command-hook

不好意思,周中上班,没有及时反馈,感谢回答,谢谢 :pray: ,我也再研究下

  1. 应该是没有advice的,下图是c-h k SPC 的结果
Signature
(self-insert-command N &optional C)

Documentation
Insert the character you type.

Whichever character C you type to run this command is inserted.
The numeric prefix argument N says how many times to repeat the insertion.
Before insertion, expand-abbrev is executed if the inserted character does
not have word syntax and the previous character in the buffer does.
After insertion, internal-auto-fill is called if
auto-fill-function is non-nil and if the auto-fill-chars table has
a non-nil value for the inserted character.  At the end, it runs
post-self-insert-hook.
  1. post-command-hook的值如下
post-command-hook is a buffer-local variable defined in C source code.

Value in #<buffer Games.java>
(lsp--post-command
 lsp-ui-sideline
 flycheck-perform-deferred-syntax-check
 flycheck-error-list-update-source
 flycheck-error-list-highlight-errors
 flycheck-maybe-display-error-at-point-soon
 flycheck-hide-error-buffer
 company-post-command
 jit-lock--antiblink-post-command
 sp--post-command-hook-handler
 yas--post-command-handler
 t
 flyspell-post-command-hook)

lsp--post-command很可疑,看看它干了些什么。另外lsp-bridge真的很好用,除了部分用户体验还不如lsp-mode完善,不过可以请坛里各位一起改

非常感谢,回复晚了!

通过二分法,终于定位到原因了,是 aggressive-indent 这个导致的,类似这个issue:Issues · Malabarba/aggressive-indent-mode · GitHub

通过添加如下配置解决:

(add-to-list 'aggressive-indent-dont-indent-if '(and (eq (char-before) ?\s) (looking-at-p "$")))
1 个赞