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

feat(ocamllsp): support Pexp_ifthenelse folding ranges #1031

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
called `duneDiganostics` and it may be set to `{ enable: false }` to disable
diagnostics. (#1221)

- Support folding of `ifthenelse` expressions (#1031)

# 1.17.0

## Fixes
Expand Down
8 changes: 7 additions & 1 deletion ocaml-lsp-server/src/folding_range.ml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ let fold_over_parsetree (parsetree : Mreader.parsetree) =
let lident_range = Range.of_loc lident.Asttypes.loc in
let expr_range = Range.of_loc expr.Parsetree.pexp_loc in
push { Range.start = lident_range.end_; end_ = expr_range.end_ })
| Pexp_ifthenelse (if_expr, then_expr, else_expr) ->
let loc =
{ if_expr.pexp_loc with loc_end = then_expr.pexp_loc.loc_end }
in
Range.of_loc loc |> push;
self.expr self then_expr;
Option.iter else_expr ~f:(self.expr self)
| Pexp_apply _
| Pexp_while _
| Pexp_for _
Expand All @@ -202,7 +209,6 @@ let fold_over_parsetree (parsetree : Mreader.parsetree) =
| Pexp_fun _
| Pexp_poly _
| Pexp_sequence _
| Pexp_ifthenelse _
| Pexp_constraint _
| Pexp_function _
| Pexp_newtype _
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,13 @@ describe("textDocument/foldingRange", () => {
"startCharacter": 0,
"startLine": 0,
},
Object {
"endCharacter": 6,
"endLine": 10,
"kind": "region",
"startCharacter": 5,
"startLine": 1,
},
Object {
"endCharacter": 29,
"endLine": 4,
Expand Down Expand Up @@ -1457,4 +1464,59 @@ describe("textDocument/foldingRange", () => {
]
`);
});

it("returns folding ranges for Pexp_ifthenelse", async () => {
openDocument(outdent`
if tool_name = "ocamldep" then
if is_self_reference ~input_name ~loc lid then
{type_decl with ptype_manifest = None}
else {type_decl with ptype_manifest = Some manifest}
else
let x =
let () = Stdlib.print_endline "one" in
let () = Stdlib.print_endline "two" in
()
in
let y =
let () = Stdlib.print_endline "one" in
let () = Stdlib.print_endline "two" in
()
in
()
`);

let result = await foldingRange();
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"endCharacter": 54,
"endLine": 3,
"kind": "region",
"startCharacter": 3,
"startLine": 0,
},
Object {
"endCharacter": 42,
"endLine": 2,
"kind": "region",
"startCharacter": 5,
"startLine": 1,
},
Object {
"endCharacter": 6,
"endLine": 8,
"kind": "region",
"startCharacter": 2,
"startLine": 5,
},
Object {
"endCharacter": 6,
"endLine": 13,
"kind": "region",
"startCharacter": 2,
"startLine": 10,
},
]
`);
});
});
Loading