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

Disable redundant type annotation #1037

Merged
merged 7 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Fixes

- Fix a document syncing issue when utf-16 is the position encoding (#1004)
- Disable "Type-annotate" action for code that is already annotated.
([#1037](https://github.com/ocaml/ocaml-lsp/pull/1037)), fixes
[#1036](https://github.com/ocaml/ocaml-lsp/issues/1036)

# 1.15.1

Expand Down
16 changes: 15 additions & 1 deletion ocaml-lsp-server/src/code_actions/action_type_annotate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ let check_typeable_context pipeline pos_start =
let pos_start = Mpipeline.get_lexing_pos pipeline pos_start in
let typer = Mpipeline.typer_result pipeline in
let browse = Mbrowse.of_typedtree (Mtyper.get_typedtree typer) in
let is_exp_constrained = function
| ((Typedtree.Texp_constraint _), _, _) -> true
| _ -> false in
let is_pat_constrained = function
| ((Typedtree.Tpat_constraint _), _, _) -> true
| _ -> false in
let is_valid p extras =
if List.exists ~f:p extras then `Invalid else `Valid in
match Mbrowse.enclosing pos_start [ browse ] with
| (_, (Expression _ | Pattern _)) :: _ -> `Valid
| (_, (Expression e)) :: _ ->
is_valid is_exp_constrained e.exp_extra
| (_, (Pattern { pat_desc = Typedtree.Tpat_any; _ })) ::
(_, (Pattern { pat_desc = Typedtree.Tpat_alias _ ; pat_extra; _ })) :: _ ->
is_valid is_pat_constrained pat_extra
| (_, (Pattern p)) :: _ ->
is_valid is_pat_constrained p.pat_extra
| _ :: _ | [] -> `Invalid

let get_source_text doc (loc : Loc.t) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,76 @@ let f (x : t) = x
`);
});

it("does not type-annotate already annotated argument", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try porting this test to OCaml in e2e-new?

The typescript suite is our legacy test suite. We haven't yet ported it to all to OCaml, but we'd like to stop adding new tests at least.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgrinberg , sure! I have ported them. After this PR gets merged I will port other type-annotate as well, so that they're all in one place. I already have the filtering functionality, so it should be a simple refactor.

openDocument(
outdent`
let f (x : int) = 1
`,
"file:///test.ml",
);
let start = Types.Position.create(1, 7);
let end = Types.Position.create(1, 8);
let actions = await codeAction("file:///test.ml", start, end);
expect(actions).toMatchInlineSnapshot(`
Array [
Object {
"command": Object {
"arguments": Array [
"file:///test.mli",
],
"command": "ocamllsp/open-related-source",
"title": "Create test.mli",
},
"edit": Object {
"documentChanges": Array [
Object {
"kind": "create",
"uri": "file:///test.mli",
},
],
},
"kind": "switch",
"title": "Create test.mli",
},
]
`);
});

it("does not type-annotate already annotated value", async () => {
openDocument(
outdent`
let f x = (1 : int)
`,
"file:///test.ml",
);
let start = Types.Position.create(1, 11);
let end = Types.Position.create(1, 12);
let actions = await codeAction("file:///test.ml", start, end);
expect(actions).toMatchInlineSnapshot(`
Array [
Object {
"command": Object {
"arguments": Array [
"file:///test.mli",
],
"command": "ocamllsp/open-related-source",
"title": "Create test.mli",
},
"edit": Object {
"documentChanges": Array [
Object {
"kind": "create",
"uri": "file:///test.mli",
},
],
},
"kind": "switch",
"title": "Create test.mli",
},
]
`);
});

it("does not type-annotate in a non expression context", async () => {
openDocument(
outdent`
Expand Down