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 autofix to be configurable instead of just on/off #1641

Open
xdBronch opened this issue Nov 30, 2023 · 5 comments
Open

allow autofix to be configurable instead of just on/off #1641

xdBronch opened this issue Nov 30, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@xdBronch
Copy link
Contributor

related to #1621 and #1623 i think it should be configurable which autofixes are applied rather than simply enabling and disabling the entire feature. i assume it was designed the way it is since there was only 1 autofix for a while but we're adding more now and likely will continue to in the future

@xdBronch xdBronch added the enhancement New feature or request label Nov 30, 2023
@iacore
Copy link

iacore commented Dec 30, 2023

i'd like to disable the // autofix comment

@Cloudef
Copy link

Cloudef commented Jan 17, 2024

I'd like to enable back var -> const

@Techatrix
Copy link
Member

I'd like to enable back var -> const

code actions that are part of autofix should ideally be reversible if necessary. This means that we would need the zig compiler to produce a "constant variable is being mutated" error so that we can do "const -> var".

If the use case is to update old code, recompiling ZLS with the following diff could be useful.

diff --git a/src/features/code_actions.zig b/src/features/code_actions.zig
index 74723ec7..17c2440d 100644
--- a/src/features/code_actions.zig
+++ b/src/features/code_actions.zig
@@ -355,7 +355,7 @@ fn handleVariableNeverMutated(builder: *Builder, actions: *std.ArrayListUnmanage
 
     try actions.append(builder.arena, .{
         .title = "use 'const'",
-        .kind = .quickfix,
+        .kind = .@"source.fixAll",
         .isPreferred = true,
         .edit = try builder.createWorkspaceEdit(&.{
             builder.createTextEditLoc(var_keyword_loc, "const"),

@Cloudef
Copy link

Cloudef commented Jan 17, 2024

The use case is not for updating old code but just having the feature in general.
I used this for automatic updates, as you can't call zls's autofix without server or using the library:

if [[ ! -d "$1" ]]; then
  printf 'error: no such directory: %s\n' "$1"
  exit 1
fi

cd "$1"
has_wontfix=0

while {
    IFS=$':' read -r file line col msg;
} do
  if [[ "$msg" ]]; then
    case "$msg" in
      *"local variable is never mutated")
        printf 'autofix: %s\n' "$file:$line:$col:$msg" 1>&2
        sed -i "''${line}s/var/const/" "$file"
        ;;
      *)
        printf 'wontfix: %s\n' "$file:$line:$col:$msg" 1>&2
        has_wontfix=1
        ;;
    esac
  fi
done < <(zig build 2>&1 | grep "error:")

exit $has_wontfix

@Techatrix
Copy link
Member

Techatrix commented Dec 16, 2024

The autofix feature is now configured through in-editor configuration. This makes it possible to configure individual code actions kinds to be applied on save.

Here is an example of how it could look like in VS Code:

"editor.codeActionsOnSave": {
  "source.fixAll": "explicit",
  "source.fixAll.varToConst": "explicit",
  "source.fixAll.preferRLS": "explicit", // https://github.com/zigtools/zls/issues/2098
  "source.organizeImports": "explicit",
}

I currently do not have any plans to implement this for "var -> const" but #2098 will need it if it wants to become triggered on save.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants