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 completion for symbols passed as named arg #512

Merged
merged 4 commits into from
Sep 25, 2021
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Unreleased

## Fixes

- Fix debouncing of document updates. It was essentially complicated broken in
all but the most trivial cases. (#509 fixes #504)

- Fix completion when passing named and functional arguments (#512)

# 1.8.2 (09/14/2021)

- Disable experimental dune support. It was accidentally left enabled.
Expand Down
14 changes: 13 additions & 1 deletion ocaml-lsp-server/src/compl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ let completion_kind kind : CompletionItemKind.t option =
| `MethodCall -> Some Method
| `Keyword -> Some Keyword

(** @see https://ocaml.org/manual/lex.html reference *)
let prefix_of_position ~short_path source position =
match Msource.text source with
| "" -> ""
Expand Down Expand Up @@ -87,7 +88,18 @@ let prefix_of_position ~short_path source position =
| Some pos -> pos + 1
in
let len = from - pos + 1 in
String.sub text ~pos ~len
let reconstructed_prefix = String.sub text ~pos ~len in
(* if we reconstructed [~f:ignore] or [?f:ignore], we should take only
[ignore], so: *)
if
String.is_prefix reconstructed_prefix ~prefix:"~"
|| String.is_prefix reconstructed_prefix ~prefix:"?"
then
match String.lsplit2 reconstructed_prefix ~on:':' with
| Some (_, s) -> s
| None -> reconstructed_prefix
else
reconstructed_prefix

let suffix_of_position source position =
match Msource.text source with
Expand Down
118 changes: 118 additions & 0 deletions ocaml-lsp-server/test/e2e/__tests__/textDocument-completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,124 @@ describe_opt("textDocument/completion", () => {
expect(items).toMatchObject([{ label: "StringLabels" }]);
});

it("can complete symbol passed as a named argument", async () => {
await openDocument(outdent`
let g ~f = f 0 in
g ~f:ig
`);

let items = await queryCompletion(Types.Position.create(1, 7));
expect(items).toMatchInlineSnapshot(`
Array [
Object {
"label": "ignore",
"textEdit": Object {
"newText": "ignore",
"range": Object {
"end": Object {
"character": 7,
"line": 1,
},
"start": Object {
"character": 5,
"line": 1,
},
},
},
},
]
`);
});

it("can complete symbol passed as a named argument - 2", async () => {
await openDocument(outdent`
module M = struct let igfoo _x = () end
let g ~f = f 0 in
g ~f:M.ig
`);

let items = await queryCompletion(Types.Position.create(2, 9));
expect(items).toMatchInlineSnapshot(`
Array [
Object {
"label": "igfoo",
"textEdit": Object {
"newText": "igfoo",
"range": Object {
"end": Object {
"character": 9,
"line": 2,
},
"start": Object {
"character": 7,
"line": 2,
},
},
},
},
]
`);
});

it("can complete symbol passed as an optional argument", async () => {
await openDocument(outdent`
let g ?f = f in
g ?f:ig
`);

let items = await queryCompletion(Types.Position.create(1, 7));
expect(items).toMatchInlineSnapshot(`
Array [
Object {
"label": "ignore",
"textEdit": Object {
"newText": "ignore",
"range": Object {
"end": Object {
"character": 7,
"line": 1,
},
"start": Object {
"character": 5,
"line": 1,
},
},
},
},
]
`);
});

it("can complete symbol passed as a optional argument - 2", async () => {
await openDocument(outdent`
module M = struct let igfoo _x = () end
let g ?f = f in
g ?f:M.ig
`);

let items = await queryCompletion(Types.Position.create(2, 9));
expect(items).toMatchInlineSnapshot(`
Array [
Object {
"label": "igfoo",
"textEdit": Object {
"newText": "igfoo",
"range": Object {
"end": Object {
"character": 9,
"line": 2,
},
"start": Object {
"character": 7,
"line": 2,
},
},
},
},
]
`);
});

it("completes identifier at top level", async () => {
await openDocument(outdent`
let somenum = 42
Expand Down