Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement on-type-formatting support #899

Merged
merged 3 commits into from
Mar 29, 2022
Merged

Conversation

nemethf
Copy link
Collaborator

@nemethf nemethf commented Mar 27, 2022

  • eglot.el (eglot--on-type-formatting-chars): New local var, set
    and unset ...
    (eglot--managed-mode): ... here based on server's
    documentOnTypeFormatting capability, ...
    (eglot--post-self-insert-hook): and used here to call eglot-format
    for on-type-formatting characters.
    (eglot-format): Add new optional argument character to request
    :textDocument/onTypeFormatting.

  • eglot-tests.el (eglot--simulate-key-event): New helper defun.
    (rust-on-type-formatting): New test.

Copy link
Owner

@joaotavora joaotavora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much! Looks good, but please consider my comments and changes.

eglot.el Outdated
@@ -1974,7 +1989,9 @@ THINGS are either registrations or unregisterations (sic)."

(defun eglot--post-self-insert-hook ()
"Set `eglot--last-inserted-char'."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring needs an overhaul, it should become something more generic like Do Eglot things after a character is inserted., maybe with better imagination.

eglot.el Outdated
is not active."
is not active.

If CHARACTER is non-nil, assume it is an on-type-formatting,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say the type of CHARACTER, i.e. that it is a character (or a one-character string). Perhaps name it ON-TYPE-FORMAT.

eglot.el Outdated Show resolved Hide resolved
eglot.el Outdated Show resolved Hide resolved
eglot.el Outdated Show resolved Hide resolved
eglot.el Outdated Show resolved Hide resolved
eglot-tests.el Outdated Show resolved Hide resolved
@@ -262,6 +262,13 @@ Pass TIMEOUT to `eglot--with-timeout'."
(eglot-connect-timeout timeout))
(apply #'eglot--connect (eglot--guess-contact))))

(defun eglot--simulate-key-event (char)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is ert-simulate-keys in ert-x.el. A hack it itself seems less hacky than this (and not our responsibility ;-) ) Doesn't it work? If it doesn't then fine to add this substitute.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ert-simulate-keys is not in Emacs 27.1, but more importantly it does something else: it provides user input for a function if it queries the user input (eg. read-from-minibuffer) during its execution. The goal of eglot--simulate-key-event is initiate a command-loop for a given keypress. I think the whole thing can be replaced with (setq last-input-event ?.) (self-insert-command 1 ?.) as that also calls post-self-insert-hooks. But then some part of the implementation becomes part of the test. An earlier iteration of this PR redefined keybindings for the shake of execution speed. eglot--simulate-key-event worked with that implementation as well.

Shall I (a) change the test to use (setq last-input-event ?.) (self-insert-command 1 ?.), or (b) extend the documentation of eglot--simulate-key-event?

eglot.el Outdated Show resolved Hide resolved
@joaotavora
Copy link
Owner

Actually, I see that even the new server slot isn't needed. This is already in the server as "capabilities". So just grab it from there. I don't think processing required is extremely more onerous. At least not when compared to the performance hit we're already incurring in when calling jsonrpc-request from the post-self-insert-hook.

So this should simplify the code a lot.

@nemethf
Copy link
Collaborator Author

nemethf commented Mar 28, 2022

Actually, I see that even the new server slot isn't needed. This is already in the server as "capabilities". So just grab it from there. I don't think processing required is extremely more onerous. At least not when compared to the performance hit we're already incurring in when calling jsonrpc-request from the post-self-insert-hook.

So this should simplify the code a lot.

The goal of precomputing eglot--on-type-formatting-chars is make post-self-insert-hook run fast for chars that are not trigger chars for on-type-formatting. Shall I (a) add this to the documentation of eglot--on-type-formatting-chars, or (b) simplify the code and calculate the list every time the user presses a key?

I'll address the rest of your comments once I see clear with these two questions.

Thanks for the review.

@joaotavora
Copy link
Owner

joaotavora commented Mar 28, 2022 via email

@nemethf nemethf force-pushed the scratch/on-type-formatting branch from 2b182e2 to dfd3753 Compare March 28, 2022 16:13
eglot.el Outdated
"Set `eglot--last-inserted-char', call on-type-formatting if necessary."
(setq eglot--last-inserted-char last-input-event)
(when (or (eq last-input-event
(aref (eglot--server-capable
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this aref 💥 if the server is not capable of this?

@nemethf nemethf force-pushed the scratch/on-type-formatting branch from dfd3753 to 301ca1b Compare March 28, 2022 16:51
@nemethf
Copy link
Collaborator Author

nemethf commented Mar 28, 2022

I've force-pushed a new version addressing your concerns including the aref 💥.

It turned out the experimental/serverStatus is just as slow as waiting for the first diagnostic notification.

nemethf and others added 3 commits March 29, 2022 00:10
* eglot.el (eglot-format): Add new optional argument `on-type-format'
to request :textDocument/onTypeFormatting, and ...
(eglot--post-self-insert-hook): ... call it from here when necessary.

* eglot-tests.el (eglot--simulate-key-event): New helper defun.
(rust-on-type-formatting): New test.

* NEWS.md: mention feature.
* eglot.el (eglot--post-self-insert-hook): Tweak.
(eglot-format): Tweak docstring.
* NEWS.md: Fix typo.
@joaotavora joaotavora force-pushed the scratch/on-type-formatting branch from 301ca1b to 35588e0 Compare March 28, 2022 23:22
@joaotavora joaotavora merged commit cf5e4f8 into master Mar 29, 2022
joaotavora pushed a commit that referenced this pull request Mar 29, 2022
* eglot.el (eglot-format): Add new optional argument `on-type-format'
to request :textDocument/onTypeFormatting, and ...
(eglot--post-self-insert-hook): ... call it from here when necessary.

* eglot-tests.el (eglot--simulate-key-event): New helper defun.
(rust-on-type-formatting): New test.

* NEWS.md: mention feature.
joaotavora added a commit that referenced this pull request Mar 29, 2022
* eglot.el (eglot--post-self-insert-hook): Tweak.
(eglot-format): Tweak docstring.
@joaotavora
Copy link
Owner

I took the liberty of making some minor tweaks and pushed this. Let's keep eyes peeled for any problems (performance, 💥, etc), though I think this is pretty sane.

@nemethf
Copy link
Collaborator Author

nemethf commented Mar 29, 2022

Thanks!

bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 18, 2022
* eglot.el (eglot-format): Add new optional argument `on-type-format'
to request :textDocument/onTypeFormatting, and ...
(eglot--post-self-insert-hook): ... call it from here when necessary.

* eglot-tests.el (eglot--simulate-key-event): New helper defun.
(rust-on-type-formatting): New test.

* NEWS.md: mention feature.
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 18, 2022
* eglot.el (eglot--post-self-insert-hook): Tweak.
(eglot-format): Tweak docstring.
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
* eglot.el (eglot-format): Add new optional argument `on-type-format'
to request :textDocument/onTypeFormatting, and ...
(eglot--post-self-insert-hook): ... call it from here when necessary.

* eglot-tests.el (eglot--simulate-key-event): New helper defun.
(rust-on-type-formatting): New test.

* NEWS.md: mention feature.
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
* eglot.el (eglot--post-self-insert-hook): Tweak.
(eglot-format): Tweak docstring.
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
* eglot.el (eglot-format): Add new optional argument `on-type-format'
to request :textDocument/onTypeFormatting, and ...
(eglot--post-self-insert-hook): ... call it from here when necessary.

* eglot-tests.el (eglot--simulate-key-event): New helper defun.
(rust-on-type-formatting): New test.

* NEWS.md: mention feature.

#899: joaotavora/eglot#899
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
* eglot.el (eglot--post-self-insert-hook): Tweak.
(eglot-format): Tweak docstring.

#899: joaotavora/eglot#899
jollaitbot pushed a commit to sailfishos-mirror/emacs that referenced this pull request Oct 12, 2022
* eglot.el (eglot-format): Add new optional argument `on-type-format'
to request :textDocument/onTypeFormatting, and ...
(eglot--post-self-insert-hook): ... call it from here when necessary.

* eglot-tests.el (eglot--simulate-key-event): New helper defun.
(rust-on-type-formatting): New test.

* NEWS.md: mention feature.

GitHub-reference: close joaotavora/eglot#899
jollaitbot pushed a commit to sailfishos-mirror/emacs that referenced this pull request Oct 12, 2022
* eglot.el (eglot--post-self-insert-hook): Tweak.
(eglot-format): Tweak docstring.

GitHub-reference: per joaotavora/eglot#899
jollaitbot pushed a commit to sailfishos-mirror/emacs that referenced this pull request Oct 20, 2022
* eglot.el (eglot-format): Add new optional argument `on-type-format'
to request :textDocument/onTypeFormatting, and ...
(eglot--post-self-insert-hook): ... call it from here when necessary.

* eglot-tests.el (eglot--simulate-key-event): New helper defun.
(rust-on-type-formatting): New test.

* NEWS.md: mention feature.

GitHub-reference: close joaotavora/eglot#899
jollaitbot pushed a commit to sailfishos-mirror/emacs that referenced this pull request Oct 20, 2022
* eglot.el (eglot--post-self-insert-hook): Tweak.
(eglot-format): Tweak docstring.

GitHub-reference: per joaotavora/eglot#899
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants