Skip to content

Commit 1d07c69

Browse files
committed
feat(lsp): Better place comments and support modules
1 parent 247d587 commit 1d07c69

File tree

2 files changed

+68
-40
lines changed

2 files changed

+68
-40
lines changed

compiler/src/language_server/code_action.re

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -84,36 +84,7 @@ let named_arg_label = (range, uri, arg_label) => {
8484
};
8585
};
8686

87-
let add_graindoc = (range, uri, expr_type: Types.type_expr) => {
88-
let output = Buffer.create(128);
89-
Buffer.add_string(output, "/**\n");
90-
Buffer.add_string(output, " *\n"); // Blank spot for description entry
91-
switch (expr_type.desc) {
92-
| TTyArrow(args, ret_typ, _) =>
93-
Buffer.add_string(output, " *\n"); // Spacing
94-
List.iteri(
95-
(index, (label, _)) => {
96-
let param_name =
97-
switch (label) {
98-
| Types.Labeled({txt})
99-
| Default({txt}) => txt
100-
| Unlabeled => string_of_int(index)
101-
};
102-
Buffer.add_string(
103-
output,
104-
Printf.sprintf(" * @param %s:\n", param_name),
105-
);
106-
},
107-
args,
108-
);
109-
Buffer.add_string(output, " * @returns \n");
110-
| _ => ()
111-
};
112-
Buffer.add_string(output, " *\n"); // Spacing
113-
Buffer.add_string(output, " * @example\n");
114-
Buffer.add_string(output, " *\n"); // Spacing
115-
Buffer.add_string(output, " * @since\n");
116-
Buffer.add_string(output, " */\n");
87+
let add_graindoc = (range, uri, message) => {
11788
ResponseResult.{
11889
title: "Add Graindoc",
11990
kind: "add-graindoc",
@@ -127,7 +98,7 @@ let add_graindoc = (range, uri, expr_type: Types.type_expr) => {
12798
edits: [
12899
{
129100
range,
130-
new_text: Buffer.contents(output),
101+
new_text: message,
131102
},
132103
],
133104
},
@@ -265,6 +236,34 @@ let rec process_add_or_remove_braces = (uri, results: list(Sourcetree.node)) =>
265236
}
266237
);
267238
};
239+
240+
let get_end_of_last_line = (loc: Location.t) => {
241+
{
242+
...loc,
243+
loc_start: {
244+
...loc.loc_start,
245+
pos_cnum: Int.max_int,
246+
pos_lnum: loc.loc_start.pos_lnum - 1,
247+
},
248+
loc_end: {
249+
...loc.loc_start,
250+
pos_cnum: Int.max_int,
251+
pos_lnum: loc.loc_start.pos_lnum - 1,
252+
},
253+
};
254+
};
255+
let template_graindoc = fn => {
256+
let output = Buffer.create(128);
257+
Buffer.add_string(output, "\n/**\n");
258+
Buffer.add_string(output, " *\n"); // Blank spot for description entry
259+
Buffer.add_string(output, " *\n"); // Spacing
260+
fn(output);
261+
Buffer.add_string(output, " * @example\n");
262+
Buffer.add_string(output, " *\n"); // Spacing
263+
Buffer.add_string(output, " * @since\n");
264+
Buffer.add_string(output, " */");
265+
Buffer.contents(output);
266+
};
268267
let rec process_graindoc =
269268
(
270269
uri,
@@ -273,17 +272,49 @@ let rec process_graindoc =
273272
) => {
274273
switch (results) {
275274
| [LetBind({pat, expr, loc}), ..._] =>
276-
let loc = {
277-
...loc,
278-
loc_end: loc.loc_start,
275+
let ordered = Comments.to_ordered(~extract_attributes=false, comments);
276+
let comment =
277+
Comments.Doc.ending_on(~lnum=loc.loc_start.pos_lnum - 1, ordered);
278+
switch (comment) {
279+
| Some((Doc(_), _, _)) => None
280+
| _ =>
281+
let loc = get_end_of_last_line(loc);
282+
let message =
283+
template_graindoc(output => {
284+
switch (expr.exp_type.desc) {
285+
| TTyArrow(args, ret_typ, _) =>
286+
Buffer.add_string(output, " *\n"); // Spacing
287+
List.iteri(
288+
(index, (label, _)) => {
289+
let param_name =
290+
switch (label) {
291+
| Types.Labeled({txt})
292+
| Default({txt}) => txt
293+
| Unlabeled => string_of_int(index)
294+
};
295+
Buffer.add_string(
296+
output,
297+
Printf.sprintf(" * @param %s:\n", param_name),
298+
);
299+
},
300+
args,
301+
);
302+
Buffer.add_string(output, " * @returns \n");
303+
| _ => ()
304+
}
305+
});
306+
Some(add_graindoc(Utils.loc_to_range(loc), uri, message));
279307
};
280-
// TODO: Better place after and
308+
| [Module({loc}), ...rest] =>
281309
let ordered = Comments.to_ordered(~extract_attributes=false, comments);
282310
let comment =
283311
Comments.Doc.ending_on(~lnum=loc.loc_start.pos_lnum - 1, ordered);
284312
switch (comment) {
285313
| Some((Doc(_), _, _)) => None
286-
| _ => Some(add_graindoc(Utils.loc_to_range(loc), uri, expr.exp_type))
314+
| _ =>
315+
let loc = get_end_of_last_line(loc);
316+
let message = template_graindoc(_ => ());
317+
Some(add_graindoc(Utils.loc_to_range(loc), uri, message));
287318
};
288319
| [_, ...rest] => process_graindoc(uri, rest, comments)
289320
| _ => None

compiler/src/language_server/sourcetree.re

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,7 @@ module Sourcetree: Sourcetree = {
587587
mut_flag,
588588
pat: vb_pat,
589589
expr: vb_expr,
590-
loc: {
591-
...vb_loc,
592-
loc_start: stmt.ttop_loc.loc_start,
593-
},
590+
loc: vb_loc,
594591
}),
595592
),
596593
...segments,

0 commit comments

Comments
 (0)