Skip to content

Commit a5fd244

Browse files
Marcus Robertsphated
authored andcommitted
Added a hack to complete based on completion kind
1 parent 7b3285c commit a5fd244

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

compiler/src/language_server/completion.re

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ type completion_item_kind =
3434
| CompletionItemKindOperator
3535
| CompletionItemKindTypeParameter;
3636

37+
[@deriving (enum, yojson)]
38+
type completion_trigger_kind =
39+
// Since these are using ppx_deriving enum, order matters
40+
| [@value 1] CompletionTriggerInvoke
41+
| CompletionTriggerCharacter
42+
| CompletionTriggerForIncompleteCompletions;
43+
3744
let completion_item_kind_to_yojson = severity =>
3845
completion_item_kind_to_enum(severity) |> [%to_yojson: int];
3946
let completion_item_kind_of_yojson = json =>
@@ -44,6 +51,16 @@ let completion_item_kind_of_yojson = json =>
4451
}
4552
});
4653

54+
let completion_trigger_kind_to_yojson = kind =>
55+
completion_trigger_kind_to_enum(kind) |> [%to_yojson: int];
56+
let completion_trigger_kind_of_yojson = json =>
57+
Result.bind(json |> [%of_yojson: int], value => {
58+
switch (completion_trigger_kind_of_enum(value)) {
59+
| Some(kind) => Ok(kind)
60+
| None => Result.Error("Invalid enum value")
61+
}
62+
});
63+
4764
[@deriving yojson]
4865
type completion_item = {
4966
label: string,
@@ -52,13 +69,23 @@ type completion_item = {
5269
documentation: string,
5370
};
5471

72+
[@deriving yojson({strict: false})]
73+
type completion_context = {
74+
[@key "triggerKind"]
75+
trigger_kind: completion_trigger_kind,
76+
[@key "triggerCharacter"] [@default None]
77+
trigger_character: option(string),
78+
};
79+
5580
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionParams
5681
module RequestParams = {
5782
[@deriving yojson({strict: false})]
5883
type t = {
5984
[@key "textDocument"]
6085
text_document: Protocol.text_document_identifier,
6186
position: Protocol.position,
87+
[@default None]
88+
context: option(completion_context),
6289
};
6390
};
6491

@@ -103,7 +130,9 @@ let get_original_text = (documents, uri, line, char) =>
103130
| Some(source_code) =>
104131
let lines = String.split_on_char('\n', source_code);
105132
let line = List.nth_opt(lines, line);
106-
Option.bind(line, line => find_completable(line, char));
133+
// UGH, this is really not nice:
134+
let old_char = char > 0 ? char - 1 : char; // the position is against the earlier version of the document so move back 1
135+
Option.bind(line, line => find_completable(line, old_char));
107136
};
108137

109138
// maps Grain types to LSP CompletionItemKind
@@ -173,7 +202,18 @@ let process =
173202
);
174203
switch (completable) {
175204
| None => send_completion(~id, [])
176-
| Some(text) =>
205+
| Some(prior_version_text) =>
206+
let text =
207+
switch (params.context) {
208+
| None => prior_version_text
209+
| Some(ctx) =>
210+
switch (ctx.trigger_kind) {
211+
| CompletionTriggerCharacter =>
212+
prior_version_text
213+
++ Option.value(~default="", ctx.trigger_character)
214+
| _ => prior_version_text
215+
}
216+
};
177217
switch (Hashtbl.find_opt(cached_code, params.text_document.uri)) {
178218
| None => send_completion(~id, [])
179219
| Some(compiled_code) =>
@@ -293,6 +333,6 @@ let process =
293333
};
294334

295335
send_completion(~id, completions);
296-
}
336+
};
297337
};
298338
};

0 commit comments

Comments
 (0)