Skip to content

Commit

Permalink
feat(graindoc): Cache module comments (#2102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer authored Apr 26, 2024
1 parent 6e5b432 commit d45ddfa
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions compiler/graindoc/docblock.re
Original file line number Diff line number Diff line change
Expand Up @@ -319,23 +319,27 @@ let lookup_type_expr = (~idx, type_exprs) => {
Option.bind(type_exprs, te => List.nth_opt(te, idx));
};

let saved_comments = Hashtbl.create(64);

let get_comments_from_loc = (loc: Grain_parsing.Location.t) => {
open Compile;

let comments =
switch (
compile_file(
~is_root_file=true,
~hook=stop_after_parse,
loc.loc_start.pos_fname,
)
) {
| exception exn => []
| {cstate_desc: Parsed(parsed_program)} => parsed_program.comments
| _ => failwith("Invalid compilation state")
};
let file = loc.loc_start.pos_fname;

Comments.to_ordered(comments);
switch (Hashtbl.find_opt(saved_comments, file)) {
| Some(comments) => comments
| None =>
let comments =
switch (compile_file(~is_root_file=true, ~hook=stop_after_parse, file)) {
| exception exn => []
| {cstate_desc: Parsed(parsed_program)} => parsed_program.comments
| _ => failwith("Invalid compilation state")
};

let ordered = Comments.to_ordered(comments);
Hashtbl.add(saved_comments, file, ordered);
ordered;
};
};

let attr_name = attr => {
Expand Down

0 comments on commit d45ddfa

Please sign in to comment.