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

Send point or region as code action range #473

Closed
wants to merge 2 commits into from

Conversation

muffinmad
Copy link
Collaborator

Trying to implement refactor.inline code action provided by jedi.

Looks like eglot never send the current point on codeAction request.

This PR aimed to send region boundaries when region is active and just the point otherwise:

  • Each diagnostic contains its own range, there are no need to calculate codeAction range based on diagnostics under point.
  • Exact point is certainly giving more context information to server than (point-min) (point-max).

@nemethf
Copy link
Collaborator

nemethf commented May 13, 2020

There is a typo in the "subject": acrion -> action

I like this PR. (Can you add a code-action test using one of the servers run by travis?)

@muffinmad muffinmad changed the title Send point or region as code acrion range Send point or region as code action range May 13, 2020
@muffinmad
Copy link
Collaborator Author

There is a typo in the "subject": acrion -> action

Fixed, thanks!

I like this PR. (Can you add a code-action test using one of the servers run by travis?)

Do we really need the server to test this? Isn't it enough to test that the valid request is generated?

@nemethf
Copy link
Collaborator

nemethf commented May 13, 2020

(Can you add a code-action test using one of the servers run by travis?)

Do we really need the server to test this? Isn't it enough to test that the valid request is generated?

Probably yes. Eglot has no tests in this regard, so a test of any kind is an improvement.

@joaotavora
Copy link
Owner

joaotavora commented May 13, 2020 via email

@nemethf
Copy link
Collaborator

nemethf commented May 13, 2020

There is a typo in the "subject": acrion -> action

Fixed, thanks!

I mean in the subject of the commit message. Have you fixed that as well?

* eglot.el (eglot-code-actions): Send region as codeAction range
if region is active. Otherwise send just point.
@muffinmad
Copy link
Collaborator Author

I mean in the subject of the commit message. Have you fixed that as well?

Yes :)

@joaotavora
Copy link
Owner

I've thought a bit more about this. How do I tell the server to give me all code actions for the whole buffer? That used to be the default. Do I just mark the whole buffer now?

Not saying it's bad, mind you.

@muffinmad
Copy link
Collaborator Author

I've thought a bit more about this. How do I tell the server to give me all code actions for the whole buffer? That used to be the default. Do I just mark the whole buffer now?

Yes. IMO this is predictable behavior in contrast of sending whole buffer region when there are no selection.

Perhaps it's not needed to test your particular change, but I would appreciate a test of code actions where, say, a buffer had, say, 4 diagnostics and you request code actions for the middle 2.

I'll see what I can do about it.

Does pyls do code actions?

No. Action support is declared in the server capabilities but there are no actions provided.

"Inline variable" code action provided by recent anakinls :)
But that action is available only if point is under variable that must be inlined.

@joaotavora
Copy link
Owner

Yes. IMO this is predictable behavior in contrast of sending whole buffer region when there are no selection.

OK. I'm sold. I like that logic.

I'll see what I can do about it.

Do you think anakinls is easy (and quick) to install on Travis? And is free software, by the way? If so, then it'd be nice to move all the tests to anakinls :-)

@muffinmad
Copy link
Collaborator Author

Perhaps it's not needed to test your particular change, but I would appreciate a test of code actions where, say, a buffer had, say, 4 diagnostics and you request code actions for the middle 2.

Done. Not exactly with those diagnostic counts but see basic-actions-point and basic-actions-selection tests.

Do you think anakinls is easy (and quick) to install on Travis?

I think so. Just pip install.

And is free software, by the way?

Yes.

If so, then it'd be nice to move all the tests to anakinls :-)

anakinls will not pass some tests: it doesn't do code formatting (yet :-)), completions works in slightly different way and so on.

IMO more important question is if we need the server in the tests at all.

It is enough to test that eglot is making correct requests and correctly handle responses. I mean there are no need to install pyls and autopep8 to test how TextDocumentEdit is applied to the buffer.

Because otherwise we testing servers :-) E.g. at the moment the travis config or the rename-a-symbol test must be fixed because the latest pyls give us No support for renaming in Python 2/3.5 with Jedi. Consider using the rope_rename plugin instead error. pyls update breaks eglot tests. Not good.

@joaotavora
Copy link
Owner

Because otherwise we testing servers :-)

We need to do that. We need real-life fixtures, not idealized ones.

@joaotavora
Copy link
Owner

pyls update breaks eglot tests. Not good.

Indeed I agree. Can you help us "pin" the pyls version in Travis?

@muffinmad
Copy link
Collaborator Author

Because otherwise we testing servers :-)

We need to do that. We need real-life fixtures, not idealized ones.

Got it. In this case it make sense to test how action actually applied to the buffer. I need your advice here: how can the test pick one of the actions returned by the server?

@muffinmad
Copy link
Collaborator Author

pyls update breaks eglot tests. Not good.

Indeed I agree. Can you help us "pin" the pyls version in Travis?

Fixed on master branch.

@muffinmad
Copy link
Collaborator Author

Friendly ping

@joaotavora
Copy link
Owner

joaotavora commented May 30, 2020 via email

@joaotavora
Copy link
Owner

joaotavora commented May 31, 2020 via email

@muffinmad
Copy link
Collaborator Author

Actually, it does appear to break, so I fixed it.

Thanks!

which, of course, has the very same effect that you wanted.

Can confirm, works as expected.

First, the code needs to be indented. Do this by making sure all the
macros are loaded and then indenting the region with the code.

My bad. Didn't know about indent at the moment.

(flymake-start)

It would be nice to have a comment here saying that you do this to make
the diagnostics waited on appear as actual Flymake diagnostics.

Got it.

(goto-char 94)

"94" seems waaay to brittle. Isn't there a better way?

Agree. I think search-forward will fit better here.

 (ignore-errors (call-interactively (eglot-code-actions)))

We certainly don't want ignore-errors, and it's better just to call:

Not with pyls which returns no actions. Test will fail with the No code actions here error.

(eglot-code-actions (point))

, becasue, in lisp programs such as tests, it's safer to rely on
eglot.el's Lisp interface, not the interactive specs (or at least not
like this). It's true there is an exception with xref-find-definitions,
but it's better not to repeat it.

But my goal was to test that interactive code.

It's OK to wait for the client request to go to the server, but the
specific wait for a request that meets exactly those characteristics
seems off to me. So basically I would only wait for:

(string-equal method "textDocument/codeAction")

Also, I wouldn't check the nitty gritty details of this request which,
again, are brittle. It's better to check more generic stuff, such as

(= (length diags) 1)

But I would do this with an is assertion after the wait.

I'm verifying here that the diagnostic from the current line is sent and not from the first line. Thats the reason to check all the details.

If you prefer, you can even continue the test in the sense that you
eventually invoke the code-action request and then verify its results in
the buffer.

I need your advice here: how can the test pick one of the actions returned by the server?

(eglot--sniffing
 (:client-requests c-reqs)
 (ignore-errors (call-interactively #'eglot-code-actions))

It's better to

(eglot-code-actions (region-beginning) (region-end))

Again, I wanted to test the interactive part; verify that request is composed correctly when no arguments for eglot-code-actions is specified. In case there are no need in such test, we surely can write some other test to just check buffer text after applying code action.

One more thing: should I continue work in this PR or better move the test to the new one?

Thanks again!

bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 18, 2022
If no region is active, ask for code actions at point, even if there
are no diagnostics at point.

Co-authored-by: João Távora <joaotavora@gmail.com>

* eglot.el (eglot-code-actions): Simplify.
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
If no region is active, ask for code actions at point, even if there
are no diagnostics at point.

Co-authored-by: João Távora <joaotavora@gmail.com>

* eglot.el (eglot-code-actions): Simplify.
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
If no region is active, ask for code actions at point, even if there
are no diagnostics at point.

Co-authored-by: João Távora <joaotavora@gmail.com>

* eglot.el (eglot-code-actions): Simplify.

#473: joaotavora/eglot#473
jollaitbot pushed a commit to sailfishos-mirror/emacs that referenced this pull request Oct 12, 2022
If no region is active, ask for code actions at point, even if there
are no diagnostics at point.

Co-authored-by: João Távora <joaotavora@gmail.com>

* eglot.el (eglot-code-actions): Simplify.

GitHub-reference: close joaotavora/eglot#473
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.

3 participants