Skip to content

Commit e4b97ea

Browse files
authored
fix(lsp): Prevent lsp crash when module cannot be found (#2003)
1 parent ea26d18 commit e4b97ea

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

compiler/src/language_server/hover.re

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,17 @@ let declaration_lens = (ident: Ident.t, decl: Types.type_declaration) => {
126126
};
127127

128128
let include_lens = (env: Env.t, path: Path.t) => {
129-
let module_decl = Env.find_module(path, None, env);
130-
markdown_join(
131-
grain_code_block("module " ++ Path.name(path)),
132-
module_lens(module_decl),
133-
);
129+
let header = grain_code_block("module " ++ Path.name(path));
130+
let decl = Env.find_module(path, None, env);
131+
let module_decl =
132+
switch (Modules.get_provides(decl)) {
133+
| [_, ..._] => Some(module_lens(decl))
134+
| [] => None
135+
};
136+
switch (module_decl) {
137+
| Some(mod_sig) => markdown_join(header, mod_sig)
138+
| None => header
139+
};
134140
};
135141

136142
let exception_declaration_lens =
@@ -184,12 +190,16 @@ let process =
184190
)
185191
| [Module({decl, loc}), ..._] =>
186192
send_hover(~id, ~range=Utils.loc_to_range(loc), module_lens(decl))
187-
| [Include({env, path, loc}), ..._] =>
188-
send_hover(
189-
~id,
190-
~range=Utils.loc_to_range(loc),
191-
include_lens(env, path),
192-
)
193+
| [Include({path, loc}), ..._] =>
194+
let hover_lens =
195+
try(Some(include_lens(program.env, path))) {
196+
| Not_found => None
197+
};
198+
switch (hover_lens) {
199+
| Some(lens) => send_hover(~id, ~range=Utils.loc_to_range(loc), lens)
200+
| None => send_no_result(~id)
201+
};
202+
193203
| _ => send_no_result(~id)
194204
};
195205
};

compiler/src/language_server/sourcetree.re

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ module type Sourcetree = {
170170
definition: option(Location.t),
171171
})
172172
| Include({
173-
env: Env.t,
174173
path: Path.t,
175174
loc: Location.t,
176175
});
@@ -260,7 +259,6 @@ module Sourcetree: Sourcetree = {
260259
definition: option(Location.t),
261260
})
262261
| Include({
263-
env: Env.t,
264262
path: Path.t,
265263
loc: Location.t,
266264
});
@@ -534,11 +532,7 @@ module Sourcetree: Sourcetree = {
534532
[
535533
(
536534
loc_to_interval(stmt.ttop_loc),
537-
Include({
538-
env: stmt.ttop_env,
539-
path: inc.tinc_path,
540-
loc: stmt.ttop_loc,
541-
}),
535+
Include({path: inc.tinc_path, loc: stmt.ttop_loc}),
542536
),
543537
...segments^,
544538
]

0 commit comments

Comments
 (0)