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 macro #882

Closed
robertoaloi opened this issue Feb 1, 2021 · 0 comments · Fixed by fangzhzh/erlang_ls#1 or #1226
Closed

Add Code Action / Quick Fix to remove unused macro #882

robertoaloi opened this issue Feb 1, 2021 · 0 comments · Fixed by fangzhzh/erlang_ls#1 or #1226
Labels

Comments

@robertoaloi
Copy link
Member

robertoaloi commented Feb 1, 2021

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 a macro is defined, but unused. See the picture below.

Screenshot 2022-02-16 at 14 12 49

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 macro:

[Trace - 02:18:26 PM] Sending request 'textDocument/codeAction - (1168)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/robertoaloi/git/github/erlang-ls/erlang_ls/apps/els_lsp/test/els_rename_SUITE.erl"
  },
  "range": {
    "start": {
      "line": 31,
      "character": 13
    },
    "end": {
      "line": 31,
      "character": 13
    }
  },
  "context": {
    "diagnostics": [
      {
        "source": "UnusedMacros",
        "severity": 2,
        "range": {
          "start": {
            "line": 31,
            "character": 8
          },
          "end": {
            "line": 31,
            "character": 16
          }
        },
        "message": "Unused macro: MY_MACRO"
      }
    ]
  }
}


[Trace - 02:18:26 PM] Received response 'textDocument/codeAction - (1168)' in 53ms.
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 macro" diagnostics. If one is present, the server should include a code action (say remove-unused-macro) 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 macro.

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.

@robertoaloi robertoaloi self-assigned this Feb 16, 2022
@robertoaloi robertoaloi changed the title Add QuickFix for Unused macro / unused include file Add Code Action / Quick Fix to remove unused macro Feb 16, 2022
@robertoaloi robertoaloi added the good first issue Good for newcomers label Feb 16, 2022
@robertoaloi robertoaloi removed their assignment Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant