Repository: elm-tooling/elm-language-server
Pull request #1179 (2024-02-25 21:54:01)

Fix add missing case branches not being prefixed correctly
Add missing case branches does not include prefix for variant
```elm
test : Http.Response () -> ()
test response =
    case response of
        Http.GoodStatus_ _ _ ->
            ()
```

After using code action `Add missing case branches`:

```elm
test : Http.Response () -> ()
test response =
    case response of
        Http.GoodStatus_ _ _ ->
            ()

        BadUrl_ _ ->
            Debug.todo "branch 'BadUrl_ _' not implemented"

        Timeout_ ->
            Debug.todo "branch 'Timeout_' not implemented"

        NetworkError_ ->
            Debug.todo "branch 'NetworkError_' not implemented"

        BadStatus_ _ _ ->
            Debug.todo "branch 'BadStatus_ _ _' not implemented"
```
Which fails to compile since the variants without prefix are not available in the scope of the file.


Patch:
diff --git a/src/common/providers/codeAction/addMissingCaseBranchesCodeAction.ts b/src/common/providers/codeAction/addMissingCaseBranchesCodeAction.ts
index 606426f..75162b7 100644
--- a/src/common/providers/codeAction/addMissingCaseBranchesCodeAction.ts
+++ b/src/common/providers/codeAction/addMissingCaseBranchesCodeAction.ts
@@ -58,10 +58,22 @@ function getEdits(params: ICodeActionParams, range: Range): TextEdit[] {
         branches[0].startPosition.column + 4,
     );
 
+    // If the branch is prefixed like this Foo.Bar.Biz ->
+    // We need to prefix the other branches with the same prefix (Foo.Bar.) for it to compile
+    const prefix = branches[0]
+      .descendantsOfType("upper_case_identifier")
+      .slice(0, -1) // Don't take the last one since that's the variant
+      .map((x) => x.text)
+      .join(".");
+
     const edit = PatternMatches.missing(patterns, params.program).reduce(
-      (edit, missing) =>
-        edit +
-        `\n\n${branchIndent}${missing} ->\n${branchExprIndent}Debug.todo "branch '${missing}' not implemented"`,
+      (edit, missing) => {
+        if (prefix) {
+          missing = `${prefix}.${missing}`;
+        }
+
+        return `${edit}\n\n${branchIndent}${missing} ->\n${branchExprIndent}Debug.todo "branch '${missing}' not implemented"`;
+      },
       "",
     );
 


Test patch:
diff --git a/test/codeActionTests/addMissingCaseBranches.test.ts b/test/codeActionTests/addMissingCaseBranches.test.ts
index d47040c..c1433ff 100644
--- a/test/codeActionTests/addMissingCaseBranches.test.ts
+++ b/test/codeActionTests/addMissingCaseBranches.test.ts
@@ -128,4 +128,58 @@ func a =
       expectedSource,
     );
   });
+
+  it("should prefix branch variant if needed", async () => {
+    const source = `
+--@ Foo.elm
+module Foo exposing (..)
+
+type Foo
+    = Bar
+    | Biz
+
+--@ Test.elm
+module Test exposing (..)
+
+import Foo
+
+test : Foo -> ()
+test response =
+    case response of
+    --^
+        Foo.Bar ->
+            ()
+
+    `;
+
+    const expectedSource = `
+--@ Foo.elm
+module Foo exposing (..)
+
+type Foo
+    = Bar
+    | Biz
+
+--@ Test.elm
+module Test exposing (..)
+
+import Foo
+
+test : Foo -> ()
+test response =
+    case response of
+        Foo.Bar ->
+            ()
+
+        Foo.Biz ->
+            Debug.todo "branch 'Foo.Biz' not implemented"
+
+    `;
+
+    await testCodeAction(
+      source,
+      [{ title: "Add missing case branches" }],
+      expectedSource,
+    );
+  });
 });