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

Allow linters to provide fix #784

Open
renkun-ken opened this issue Mar 21, 2021 · 4 comments
Open

Allow linters to provide fix #784

renkun-ken opened this issue Mar 21, 2021 · 4 comments
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!

Comments

@renkun-ken
Copy link
Collaborator

renkun-ken commented Mar 21, 2021

Some code editors (e.g. vscode) supports code actions to allow language server to provide code refactoring or fixes via the language server protocol.

In languageserver, I'm trying to support code actions like this to provide quick fixes of the lint results for many linters. However, lintr might be the best place to provide the fix along with the problem as each linter has the best knowledge of the problem it finds and how it should be resolved if possible.

Does it make sense to allow an optional a field fix in the lint result which contains a list of edits (each has a range and a new text) so that languageserver could directly transform these fixes into code actions at downstream?

The code issues found by many linters could be easy to fix and the fix is optional rather than required so that implementing fixes could be done in a progressive way.

Also see REditorSupport/languageserver#396.

@AshesITR
Copy link
Collaborator

This sounds promising. Maybe we could upgrade the ranges attribute somehow?
IINM it's possible to specify multiple ranges for a single lint and they already contain start and end markers.
The current interface assumes a list of length-2 integer vectors.

We could add optional attributes to each vector (e.g. attr(., "fix")) or add a new attribute to Lint().
Using the names() of ranges would also be thinkable, but that will lead to issues when the source code is in a non-system encoding as R names must be encoded in the system encoding.

@renkun-ken
Copy link
Collaborator Author

renkun-ken commented Mar 21, 2021

A simple way is to add fixes to Lint structure like the following:

  structure(
    list(
      filename = filename,
      line_number = as.integer(line_number),
      column_number = as.integer(column_number),
      type = type,
      message = message,
      line = line,
      ranges = ranges,
      fixes = fixes,
      linter = NA_character_
    ),
    class = "lint")

where fixes is a character vector of replacement strings for each range in ranges. This assumes that the ranges to emphasize must be the ranges to replace text as fixes.

In most cases, a Lint only has one range, then fixes is a character vector of one string.

@renkun-ken
Copy link
Collaborator Author

Here's a simple demo with #785 and REditorSupport/languageserver#397:

Kapture 2021-03-21 at 23 35 50

@AshesITR AshesITR added the feature a feature request or enhancement label Mar 25, 2021
@MichaelChirico
Copy link
Collaborator

FWIW this is exactly the sort of thing I had in mind for #710

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!
Projects
None yet
Development

No branches or pull requests

3 participants