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

fix(ocamllsp): Disable type annotation for functions #1054

Merged
merged 2 commits into from
Mar 20, 2023
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 @@ -2,6 +2,8 @@

## Fixes

- Disable type annotation for functions (#1054)

- Respect codeActionLiteralSupport capability (#1046)

- Fix a document syncing issue when utf-16 is the position encoding (#1004)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ let check_typeable_context pipeline pos_start =
| None -> `Invalid
in
match Mbrowse.enclosing pos_start [ browse ] with
| (_, Pattern { pat_desc = Tpat_var _; _ })
:: (_, Value_binding { vb_expr = { exp_desc = Texp_function _; _ }; _ })
:: _ -> `Invalid
| (_, Expression e) :: _ -> is_valid e.exp_loc is_exp_constrained e.exp_extra
| (_, Pattern { pat_desc = Typedtree.Tpat_any; pat_loc; _ })
:: (_, Pattern { pat_desc = Typedtree.Tpat_alias _; pat_extra; _ })
Expand Down
3 changes: 3 additions & 0 deletions ocaml-lsp-server/src/code_actions/action_type_annotate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ let check_typeable_context pipeline pos_start =
if List.exists ~f:p extras then `Invalid else `Valid
in
match Mbrowse.enclosing pos_start [ browse ] with
| (_, Pattern { pat_desc = Tpat_var _; _ })
:: (_, Value_binding { vb_expr = { exp_desc = Texp_function _; _ }; _ })
:: _ -> `Invalid
| (_, Expression e) :: _ -> is_valid is_exp_constrained e.exp_extra
| (_, Pattern { pat_desc = Typedtree.Tpat_any; _ })
:: (_, Pattern { pat_desc = Typedtree.Tpat_alias _; pat_extra; _ })
Expand Down
24 changes: 24 additions & 0 deletions ocaml-lsp-server/test/e2e-new/code_actions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ let iiii = 3 + 4
}
|}]

let%expect_test "does not type-annotate function" =
let source = {ocaml|
let my_fun x y = 1
|ocaml} in
let range =
let start = Position.create ~line:1 ~character:5 in
let end_ = Position.create ~line:1 ~character:6 in
Range.create ~start ~end_
in
print_code_actions source range ~filter:find_annotate_action;
[%expect {| No code actions |}]

let%expect_test "can type-annotate an argument in a function call" =
let source =
{ocaml|
Expand Down Expand Up @@ -487,3 +499,15 @@ let x = (7 : int :> int)
"kind": "remove type annotation",
"title": "Remove type annotation"
} |}]

let%expect_test "does not remove type annotation from function" =
let source = {ocaml|
let my_fun x y : int = 1
|ocaml} in
let range =
let start = Position.create ~line:1 ~character:5 in
let end_ = Position.create ~line:1 ~character:6 in
Range.create ~start ~end_
in
print_code_actions source range ~filter:find_remove_annotation_action;
[%expect {| No code actions |}]