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

Add Code Action / Quick Fix to remove unused include #1202

Closed
robertoaloi opened this issue Feb 16, 2022 · 0 comments · Fixed by rahulraina7/erlang_ls#1 or #1227
Closed

Add Code Action / Quick Fix to remove unused include #1202

robertoaloi opened this issue Feb 16, 2022 · 0 comments · Fixed by rahulraina7/erlang_ls#1 or #1227
Labels

Comments

@robertoaloi
Copy link
Member

robertoaloi commented Feb 16, 2022

Context

One of the core features of Erlang LS is to be able to publish a diagnostic message whenever an issue is detected in the user code. Diagnostics are often rendered by the IDE with a squiggle of different colour, depending on the severity of the message (e.g. a warning uses a yellow squiggle, while an error uses a red squiggle).

One of these messages is displayed whenever an include or include_lib attribute is added to a module, but unused. See the picture below.

Screenshot 2022-02-16 at 15 59 03

A Code Action request is sent from the IDE to the language server to compute the possible code actions available for a given document and text range. In the case above, the following request is sent from the IDE to the language server as soon as the user hovers the unused include:

[Trace - 03:59:43 PM] Sending request 'textDocument/codeAction - (4981)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/robertoaloi/git/github/erlang-ls/erlang_ls/apps/els_lsp/test/els_rename_SUITE.erl"
  },
  "range": {
    "start": {
      "line": 37,
      "character": 25
    },
    "end": {
      "line": 37,
      "character": 25
    }
  },
  "context": {
    "diagnostics": [
      {
        "source": "UnusedIncludes",
        "severity": 2,
        "range": {
          "start": {
            "line": 37,
            "character": 0
          },
          "end": {
            "line": 37,
            "character": 40
          }
        },
        "message": "Unused file: file.hrl"
      }
    ]
  }
}


[Trace - 03:59:43 PM] Received response 'textDocument/codeAction - (4981)' in 48ms.
Result: []

As you can see, the request contains the current location (file path, line number, line column) and an additional context (in this case a list containing one diagnostic, which can in turn be used by the server to provide a list of available code actions. Right now, since this feature is not yet implemented, no code actions (i.e. an empty list or []) is returned to the client.

What is Required

In case a textDocument/codeAction request is received by the language server, the server should check for presence of "Unused include" diagnostics. If one is present, the server should include a code action (say remove-unused-include) in the response.

Once the possible code action is received by the IDE, the user gets notified. This usually happens via a little bulb icon or similar, which suggests that a code action is available. In this case, the code action would be of type quickfix and suggest to remove the unused include.

Code Pointers

  • Code action requests are dispatched to the els_code_action_provider module, so you probably want to start there

Testing

Don't forget to include a test for the new functionality! You can look at existing examples in the els_code_action_SUITE module and at this code lenses tutorial which includes a testing overview which also applies to code actions.

Problems following the instructions?

Get in touch! We'll be glad to help you to get unblocked and to improve our docs.

rahulraina7 added a commit to rahulraina7/erlang_ls that referenced this issue Feb 25, 2022
rahulraina7 added a commit to rahulraina7/erlang_ls that referenced this issue Feb 25, 2022
robertoaloi pushed a commit that referenced this issue Feb 28, 2022
* Add Fix for #1202 remove unused include

* Fixing lint line length

* Fixing failing tests

* Fixing Var name

* Update diagnostic type spec

* Update diagnostic type spec

* Fixing dialyzer errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant