Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make work with polyvariants
  • Loading branch information
zth committed May 25, 2024
commit 08b05761ccda049e5c6ae070735a6fcb97dacb95
25 changes: 24 additions & 1 deletion analysis/src/Xform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,30 @@ module ExpandCatchAllForVariants = struct
in
codeActions := codeAction :: !codeActions
else ()
(*| Some (Tpolyvariant {constructors}) -> ()*)
| Some (Tpolyvariant {constructors}) ->
let missingConstructors =
constructors
|> List.filter (fun (c : SharedTypes.polyVariantConstructor) ->
currentConstructorNames |> List.mem c.name = false)
in
if List.length missingConstructors > 0 then
let newText =
missingConstructors
|> List.map (fun (c : SharedTypes.polyVariantConstructor) ->
Res_printer.polyVarIdentToString c.name
^
match c.args with
| [] -> ""
| _ -> "(_)")
|> String.concat " | "
in
let range = rangeOfLoc catchAllCase.pc_lhs.ppat_loc in
let codeAction =
CodeActions.make ~title:"Expand catch-all" ~kind:RefactorRewrite
~uri:path ~newText ~range
in
codeActions := codeAction :: !codeActions
else ()
| _ -> ())
end

Expand Down
8 changes: 8 additions & 0 deletions analysis/tests/src/Xform.res
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ let _x = switch variant {
| _ => "other"
// ^xfm
}

let polyvariant: [#first | #second | #"illegal identifier" | #third(int)] = #first

let _y = switch polyvariant {
| #first => "first"
| _ => "other"
// ^xfm
}
17 changes: 17 additions & 0 deletions analysis/tests/src/expected/Xform.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,20 @@ newText:
<--here
Second | Third | Fourth(_)

Xform src/Xform.res 88:4
posCursor:[86:16] posNoWhite:[86:14] Found expr:[86:9->90:1]
Completable: Cpath Value[polyvariant]
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath Value[polyvariant]
Path polyvariant
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
Hit: Expand catch-all

TextDocumentEdit: Xform.res
{"start": {"line": 88, "character": 2}, "end": {"line": 88, "character": 3}}
newText:
<--here
#second | #"illegal identifier" | #third(_)