Skip to content

Commit

Permalink
fix(lsp): Prevent lsp crash when module cannot be found (#2003)
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Mar 4, 2024
1 parent ea26d18 commit e4b97ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
32 changes: 21 additions & 11 deletions compiler/src/language_server/hover.re
Expand Up @@ -126,11 +126,17 @@ let declaration_lens = (ident: Ident.t, decl: Types.type_declaration) => {
};

let include_lens = (env: Env.t, path: Path.t) => {
let module_decl = Env.find_module(path, None, env);
markdown_join(
grain_code_block("module " ++ Path.name(path)),
module_lens(module_decl),
);
let header = grain_code_block("module " ++ Path.name(path));
let decl = Env.find_module(path, None, env);
let module_decl =
switch (Modules.get_provides(decl)) {
| [_, ..._] => Some(module_lens(decl))
| [] => None
};
switch (module_decl) {
| Some(mod_sig) => markdown_join(header, mod_sig)
| None => header
};
};

let exception_declaration_lens =
Expand Down Expand Up @@ -184,12 +190,16 @@ let process =
)
| [Module({decl, loc}), ..._] =>
send_hover(~id, ~range=Utils.loc_to_range(loc), module_lens(decl))
| [Include({env, path, loc}), ..._] =>
send_hover(
~id,
~range=Utils.loc_to_range(loc),
include_lens(env, path),
)
| [Include({path, loc}), ..._] =>
let hover_lens =
try(Some(include_lens(program.env, path))) {
| Not_found => None
};
switch (hover_lens) {
| Some(lens) => send_hover(~id, ~range=Utils.loc_to_range(loc), lens)
| None => send_no_result(~id)
};

| _ => send_no_result(~id)
};
};
Expand Down
8 changes: 1 addition & 7 deletions compiler/src/language_server/sourcetree.re
Expand Up @@ -170,7 +170,6 @@ module type Sourcetree = {
definition: option(Location.t),
})
| Include({
env: Env.t,
path: Path.t,
loc: Location.t,
});
Expand Down Expand Up @@ -260,7 +259,6 @@ module Sourcetree: Sourcetree = {
definition: option(Location.t),
})
| Include({
env: Env.t,
path: Path.t,
loc: Location.t,
});
Expand Down Expand Up @@ -534,11 +532,7 @@ module Sourcetree: Sourcetree = {
[
(
loc_to_interval(stmt.ttop_loc),
Include({
env: stmt.ttop_env,
path: inc.tinc_path,
loc: stmt.ttop_loc,
}),
Include({path: inc.tinc_path, loc: stmt.ttop_loc}),
),
...segments^,
]
Expand Down

0 comments on commit e4b97ea

Please sign in to comment.