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

Show popup with code actions when hovering over lightbulb icon #1929

Merged
merged 5 commits into from
Jan 18, 2022

Conversation

jwortmann
Copy link
Member

I always wondered why this wasn't implemented yet, I would expect the dynamic lightbulbs to be interactive

I think it is a nice little addition, but feel free to close if popups from mouse hover over the gutter are not desired.

Most of the code is copied from hover.py, so maybe it would be possible / better to refactor the code a bit to reduce duplication, if we want to proceed to merge this.

@jwortmann jwortmann changed the title Make code actions lightbulb icon clickable Make code actions lightbulb icon clickable (more or less) Jan 10, 2022
@predragnikolic
Copy link
Member

Hello,
I tried this, but it seems like it doesn't work.

When I click on the bulb in the gutter
I expect code actions to be shown,
but currently nothing is being shown:

output

@jwortmann
Copy link
Member Author

Well, the icon is not really clickable, sorry for the clickbait title ;)

It should show a small hover popup when you hover over the gutter icon.

lightbulb

Doesn't it work for you?

I think what happened in your GIF, is that when you clicked on the icon, it selected the full line and the caret moved to the next line. Because of that, the lightbulb went away (the popup should only show when there is currently a lightbulb at the same place where you hover over the gutter).

I've tested also with multiple code actions on a line, and then it should open the command palette when you click the link, just like how it works for the annotations and the "normal" hover popups.

@predragnikolic
Copy link
Member

Yeah, the title got me confused.

Doesn't it work for you?

It does work when I hover over the icon. :)

@jwortmann jwortmann changed the title Make code actions lightbulb icon clickable (more or less) Show popup with code actions when hovering over lightbulb icon Jan 10, 2022
@jwortmann
Copy link
Member Author

By the way, to give a bit more context; initially I intended to show all available code actions for the entire line when hovering over the lightbulb icon. But then I realized that the icon is not necessarily shown in the line where the diagnostics are, but rather where the selection starts. This is a result of how the code actions are requested for the combined covering region of the selection & diagnostic:

LSP/plugin/documents.py

Lines 527 to 529 in 19a01aa

def _do_code_actions(self) -> None:
diagnostics_by_config, covering = self.diagnostics_intersecting_async(self._stored_region)
actions_manager.request_for_region_async(self.view, covering, diagnostics_by_config, self._on_code_actions)

If the selection starts at a previous line, then the lightbulb will be shown at the line where the selection begins, and not at the line where the diagnostic is / begins. Therefore I used the exact same logic when showing the available code actions on gutter hover.

But I wonder whether LSP is doing the correct thing in general by combining the regions, when the code actions are requested from the server. It seems that this can cause possible issues when a code action is executed, too. Look at the following example:

This is when I execute a code action with an empty selection, using the Lua server (everything works as expected):

line

Now I execute the same code action, but with a selection starting at a previous line. Notice how the line comment from the code action gets inserted at a wrong position (at the line in front of the selection).

selection

Is this a bug in the Lua server, or should LSP better use only the region where the diagnostics are, when requesting code actions?


What do you think in general about the popup when hovering the gutter icon? We could also add the same for the diagnostic icons, i.e. a popup could show all the diagnostics which are contained in that line, but of course without all the additional symbol information which is often there in the normal hover popups.

Copy link
Member

@rwols rwols left a comment

Choose a reason for hiding this comment

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

I do think the code for building the minihtml should be shared between the two features, can you make that happen?

@jwortmann
Copy link
Member Author

Yes, I will try to do that.

@rchl
Copy link
Member

rchl commented Jan 16, 2022

Is this a bug in the Lua server, or should LSP better use only the region where the diagnostics are, when requesting code actions?

Looks like as server issue to me. It should know where the diagnostic is located and where the comment should be inserted so it doesn't make sense that it chooses to insert it at the first line of the selection.

And for reference, it returns the edit in the initial code action response already. For example:

  {
    "edit": {
      "changes": {
        "file:///Users/rafal/workspace/temp/LUA-Projects/projects/1.Basics.lua": [
          {
            "newText": "---@diagnostic disable-next-line: unused-local\n",
            "range": {
              "end": {
                "character": 0,
                "line": 56
              },
              "start": {
                "character": 0,
                "line": 56
              }
            }
          }
        ]
      }
    },
    "kind": "quickfix",
    "title": "Disable diagnostics on this line (unused-local)."
  },

@jwortmann
Copy link
Member Author

The code to create the minihtml for the code actions is reused from hover.py now, and I removed the additional request when hovering over the lightbulb, because it wasn't really necessary.

There is still a bit of duplication of the code for handling of clicking on the link, but I think sharing this code would need some bigger refactoring of LspHoverCommand.

@rwols rwols merged commit 3585ca4 into sublimelsp:main Jan 18, 2022
@jwortmann jwortmann deleted the code-actions-lightbulb-hover branch January 18, 2022 19:19
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.

4 participants