zls icon indicating copy to clipboard operation
zls copied to clipboard

allow autofix to be configurable instead of just on/off

Open xdBronch opened this issue 2 years ago • 7 comments

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 avatar Nov 30 '23 11:11 xdBronch

i'd like to disable the // autofix comment

iacore avatar Dec 30 '23 10:12 iacore

I'd like to enable back var -> const

Cloudef avatar Jan 17 '24 03:01 Cloudef

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"),

Techatrix avatar Jan 17 '24 03:01 Techatrix

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

Cloudef avatar Jan 17 '24 03:01 Cloudef