Skip to content

Commit 7e4d39b

Browse files
CopilotSimn
andcommitted
Replace ad-hoc Cancelled re-raises with specific exception catches; merge development
Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
1 parent 705c3af commit 7e4d39b

15 files changed

Lines changed: 98 additions & 110 deletions

src/compiler/displayOutput.ml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,10 @@ let handle_syntax_completion com kind subj =
5353

5454
let handle_display_exception_json com dex rh =
5555
match dex with
56-
| DisplayHover _ | DisplayPositions _ | DisplayFields _ | DisplayPackage _ | DisplaySignatures _ ->
56+
| DisplayHover _ | DisplayFields _ ->
5757
DisplayPosition.display_position#reset;
5858
let ctx = DisplayJson.create_json_context (match dex with DisplayFields _ -> true | _ -> false) in
5959
CompilerOutput.send_result_raise rh (DisplayException.to_json ctx dex)
60-
| DisplayNoResult ->
61-
(match com.display.dms_kind with
62-
| DMDefault -> CompilerOutput.send_error_raise rh [jstring "No completion point"]
63-
| _ -> CompilerOutput.send_result_raise rh JNull
64-
)
65-
| ModuleSymbols json ->
66-
DisplayPosition.display_position#reset;
67-
CompilerOutput.send_result_raise rh json
68-
| Metadata _ ->
69-
die "Unexpected Metadata display exception" __LOC__
7060

7161
let handle_display_exception com dex =
7262
handle_display_exception_json com dex com.request_scope.result_handler

src/compiler/displayProcessing.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let process_display_file com actx =
7676
let real = Path.get_real_path (Path.UniqueKey.to_string file_key) in
7777
let dpk = match get_module_path_from_file_path com real with
7878
| Some path ->
79-
if com.display.dms_kind = DMPackage then DisplayException.raise_package (fst path);
79+
if com.display.dms_kind = DMPackage then DisplayException.send_package_raise com (fst path);
8080
let dpk = match ExtString.String.nsplit (snd path) "." with
8181
| [name;"macro"] ->
8282
(* If we have a .macro.hx path, don't add the file to classes because the compiler won't find it.
@@ -135,9 +135,8 @@ let load_display_module_in_macro tctx display_file_dot_path clear = match displa
135135
let _ = MacroContext.load_macro_module (MacroContext.get_macro_context tctx) tctx.com cpath true p in
136136
Finalization.finalize mctx;
137137
Some mctx
138-
with DisplayException.DisplayException _ | Parser.TypePath _ | Globals.Cancelled | DisplayJson.JsonCompleted as exc ->
139-
raise exc
140-
| _ ->
138+
with Error.Fatal_error _ | Error.Error _ | Failure _ | Not_found
139+
| Lexer.Error _ | Parser.Error _ | Typecore.Forbid_package _ | Typecore.WithTypeError _ ->
141140
None
142141
end
143142
| None ->
@@ -204,7 +203,7 @@ let handle_display_after_typing com tctx display_file_dot_path =
204203
(* If we didn't find a completion point, load the display file in macro mode. *)
205204
if com.part_scope.parser_state.display_module_has_macro_defines then
206205
ignore(load_display_module_in_macro tctx display_file_dot_path true);
207-
raise (DisplayException.DisplayException DisplayNoResult)
206+
DisplayException.send_no_result_raise com
208207
end
209208
end else
210209
false
@@ -248,7 +247,7 @@ let process_global_display_mode com tctx =
248247
acc
249248
) [] l
250249
in
251-
DisplayException.raise_module_symbols (DocumentSymbols.Printer.json_of_module_symbols com symbols filter)
250+
DisplayException.send_module_symbols_raise com (DocumentSymbols.Printer.json_of_module_symbols com symbols filter)
252251
| _ -> ()
253252

254253
let handle_display_after_finalization com tctx display_file_dot_path =

src/context/commonCache.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class lib_build_task cs file ftime lib = object(self)
1515
try begin match lib#build path p with
1616
| Some r -> Hashtbl.add h path r
1717
| None -> ()
18-
end with Globals.Cancelled as e -> raise e
19-
| _ ->
18+
end with Error.Fatal_error _ | Error.Error _ | Failure _ | Not_found | Invalid_argument _ ->
2019
()
2120
end
2221
) lib#list_modules;

src/context/display/display.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,5 @@ let get_import_status ctx path =
9090
try
9191
let mt' = ctx.g.do_load_type_def ctx null_pos (mk_type_path ([],snd path)) in
9292
if path <> (t_infos mt').mt_path then Shadowed else Imported
93-
with Globals.Cancelled as e -> raise e
94-
| _ ->
93+
with Not_found | Error.Fatal_error _ | Error.Error _ | Failure _ ->
9594
Unimported

src/context/display/displayEmitter.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let symbol_of_module_type = function
2020

2121
let display_alias ctx name t p = match ctx.com.display.dms_kind with
2222
| DMDefinition | DMTypeDefinition ->
23-
raise_positions [p];
23+
DisplayException.send_positions_raise ctx.com [p];
2424
| DMUsage _ | DMImplementation ->
2525
ReferencePosition.set (name,p,SKOther)
2626
| DMHover ->
@@ -34,9 +34,9 @@ let display_module_type ctx mt p = match ctx.com.display.dms_kind with
3434
begin match mt with
3535
| TClassDecl c when Meta.has Meta.CoreApi c.cl_meta ->
3636
let c' = ctx.g.do_load_core_class ctx c in
37-
raise_positions [c.cl_name_pos;c'.cl_name_pos]
37+
DisplayException.send_positions_raise ctx.com [c.cl_name_pos;c'.cl_name_pos]
3838
| _ ->
39-
raise_positions [(t_infos mt).mt_name_pos];
39+
DisplayException.send_positions_raise ctx.com [(t_infos mt).mt_name_pos];
4040
end
4141
| DMUsage _ | DMImplementation ->
4242
let infos = t_infos mt in
@@ -81,7 +81,7 @@ let raise_position_of_type ctx t =
8181
let mt =
8282
let rec follow_null t =
8383
match t with
84-
| TMono r -> (match r.tm_type with None -> raise_positions [null_pos] | Some t -> follow_null t)
84+
| TMono r -> (match r.tm_type with None -> DisplayException.send_positions_raise ctx.com [null_pos] | Some t -> follow_null t)
8585
| TLazy f -> follow_null (lazy_type f)
8686
| TAbstract({a_path = [],"Null"},[t]) -> follow_null t
8787
| TDynamic _ -> ctx.g.t_dynamic_def
@@ -90,12 +90,12 @@ let raise_position_of_type ctx t =
9090
try
9191
Type.module_type_of_type (follow_null t)
9292
with
93-
Exit -> raise_positions [null_pos]
93+
Exit -> DisplayException.send_positions_raise ctx.com [null_pos]
9494
in
95-
raise_positions [(t_infos mt).mt_name_pos]
95+
DisplayException.send_positions_raise ctx.com [(t_infos mt).mt_name_pos]
9696

9797
let display_variable ctx v p = match ctx.com.display.dms_kind with
98-
| DMDefinition -> raise_positions [v.v_pos]
98+
| DMDefinition -> DisplayException.send_positions_raise ctx.com [v.v_pos]
9999
| DMTypeDefinition -> raise_position_of_type ctx v.v_type
100100
| DMUsage _ -> ReferencePosition.set (v.v_name,v.v_pos,SKVariable v)
101101
| DMHover ->
@@ -104,7 +104,7 @@ let display_variable ctx v p = match ctx.com.display.dms_kind with
104104
| _ -> ()
105105

106106
let display_field ctx origin scope cf p = match ctx.com.display.dms_kind with
107-
| DMDefinition -> raise_positions [cf.cf_name_pos]
107+
| DMDefinition -> DisplayException.send_positions_raise ctx.com [cf.cf_name_pos]
108108
| DMTypeDefinition -> raise_position_of_type ctx cf.cf_type
109109
| DMUsage _ | DMImplementation ->
110110
let name,kind = match cf.cf_name,origin with
@@ -135,7 +135,7 @@ let maybe_display_field ctx origin scope cf p =
135135
if display_position#enclosed_in p then display_field ctx origin scope cf p
136136

137137
let display_enum_field ctx en ef p = match ctx.com.display.dms_kind with
138-
| DMDefinition -> raise_positions [ef.ef_name_pos]
138+
| DMDefinition -> DisplayException.send_positions_raise ctx.com [ef.ef_name_pos]
139139
| DMTypeDefinition -> raise_position_of_type ctx ef.ef_type
140140
| DMUsage _ -> ReferencePosition.set (ef.ef_name,ef.ef_name_pos,SKEnumField ef)
141141
| DMHover ->

src/context/display/displayException.ml

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
open Globals
22
open Ast
33
open DisplayTypes
4+
open DisplayTypes.DisplayMode
45
open CompletionItem
56
open Type
67
open Genjson
78

89
exception DisplayException of display_exception_kind
910

10-
let raise_module_symbols s = raise (DisplayException(ModuleSymbols s))
11-
let raise_metadata s = raise (DisplayException(Metadata s))
12-
let raise_signatures l isig iarg kind = raise (DisplayException(DisplaySignatures((l,isig,iarg,kind))))
1311
let raise_hover item expected p = raise (DisplayException(DisplayHover({hitem = item;hpos = p;hexpected = expected})))
14-
let raise_positions pl = raise (DisplayException(DisplayPositions pl))
1512
let raise_fields ckl cr subj = raise (DisplayException(DisplayFields({fitems = ckl;fkind = cr;fsubject = subj})))
16-
let raise_package sl = raise (DisplayException(DisplayPackage sl))
1713

1814
(* global state *)
1915
let last_completion_result = ref (Array.make 0 (CompletionItem.make (ITModule ([],"")) None))
@@ -160,26 +156,6 @@ let arg_index signatures signature_index param_index =
160156

161157
let to_json ctx de =
162158
match de with
163-
| ModuleSymbols _
164-
| Metadata _ -> die "" __LOC__
165-
| DisplaySignatures(sigs,isig,iarg,kind) ->
166-
(* We always want full info for signatures *)
167-
let ctx = Genjson.create_context GMFull in
168-
let fsig ((_,signature),doc) =
169-
let fl = CompletionType.generate_function' ctx signature in
170-
let fl = (match doc with None -> fl | Some d -> ("documentation",jstring (gen_doc_text d)) :: fl) in
171-
jobject fl
172-
in
173-
let sigkind = match kind with
174-
| SKCall -> 0
175-
| SKArrayAccess -> 1
176-
in
177-
jobject [
178-
"activeSignature",jint isig;
179-
"activeParameter",jint (arg_index sigs isig iarg);
180-
"signatures",jlist fsig sigs;
181-
"kind",jint sigkind;
182-
]
183159
| DisplayHover hover ->
184160
let named_source_kind = function
185161
| WithType.FunctionArgument name -> (0, name)
@@ -218,11 +194,51 @@ let to_json ctx de =
218194
"item",CompletionItem.to_json ctx None hover.hitem;
219195
"expected",expected;
220196
]
221-
| DisplayPositions pl ->
222-
jarray (List.map generate_pos_as_location pl)
223197
| DisplayFields r ->
224198
fields_to_json ctx r.fitems r.fkind r.fsubject
225-
| DisplayPackage pack ->
226-
jarray (List.map jstring pack)
227-
| DisplayNoResult ->
228-
jnull
199+
200+
let send_json_result_raise (com:Common.context) json =
201+
let rh = com.request_scope.result_handler in
202+
CompilerOutput.send_result_raise rh json
203+
204+
let send_module_symbols_raise com s =
205+
DisplayPosition.display_position#reset;
206+
send_json_result_raise com s
207+
208+
let send_hover_raise com item expected p =
209+
DisplayPosition.display_position#reset;
210+
let ctx = Genjson.create_context GMFull in
211+
send_json_result_raise com (to_json ctx (DisplayHover({hitem = item;hpos = p;hexpected = expected})))
212+
213+
let send_signatures_raise com sigs isig iarg kind =
214+
DisplayPosition.display_position#reset;
215+
let ctx = Genjson.create_context GMFull in
216+
let fsig ((_,signature),doc) =
217+
let fl = CompletionType.generate_function' ctx signature in
218+
let fl = (match doc with None -> fl | Some d -> ("documentation",jstring (gen_doc_text d)) :: fl) in
219+
jobject fl
220+
in
221+
let sigkind = match kind with
222+
| SKCall -> 0
223+
| SKArrayAccess -> 1
224+
in
225+
send_json_result_raise com (jobject [
226+
"activeSignature",jint isig;
227+
"activeParameter",jint (arg_index sigs isig iarg);
228+
"signatures",jlist fsig sigs;
229+
"kind",jint sigkind;
230+
])
231+
232+
let send_positions_raise com pl =
233+
DisplayPosition.display_position#reset;
234+
send_json_result_raise com (jarray (List.map generate_pos_as_location pl))
235+
236+
let send_package_raise com sl =
237+
DisplayPosition.display_position#reset;
238+
send_json_result_raise com (jarray (List.map jstring sl))
239+
240+
let send_no_result_raise (com:Common.context) =
241+
let rh = com.request_scope.result_handler in
242+
match com.display.dms_kind with
243+
| DMDefault -> CompilerOutput.send_error_raise rh [jstring "No completion point"]
244+
| _ -> CompilerOutput.send_result_raise rh jnull

src/context/display/displayFields.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,7 @@ let handle_missing_field_raise ctx tthis i mode with_type pfield =
381381
begin try
382382
let e = type_expr ctx e WithType.value in
383383
e.etype
384-
with Globals.Cancelled as e -> raise e
385-
| _ ->
384+
with Error.Fatal_error _ | Error.Error _ | Typecore.Forbid_package _ | Failure _ ->
386385
mk_mono()
387386
end
388387
| _ -> mk_mono()
@@ -394,8 +393,7 @@ let handle_missing_field_raise ctx tthis i mode with_type pfield =
394393
begin try
395394
let e = type_expr ctx e WithType.value in
396395
e.etype
397-
with Globals.Cancelled as e -> raise e
398-
| _ ->
396+
with Error.Fatal_error _ | Error.Error _ | Typecore.Forbid_package _ | Failure _ ->
399397
raise Exit
400398
end
401399
| _ -> raise Exit
@@ -412,8 +410,7 @@ let handle_missing_field_raise ctx tthis i mode with_type pfield =
412410
(name,false,e.etype)
413411
) el in
414412
(TFun(tl,tret),Method MethNormal)
415-
with Globals.Cancelled as e -> raise e
416-
| _ ->
413+
with Error.Fatal_error _ | Error.Error _ | Typecore.Forbid_package _ | Failure _ ->
417414
raise Exit
418415
end
419416
| MGet ->

src/context/display/displayPath.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,15 @@ module TypePathHandler = struct
155155
) en.e_constrs fields
156156
in
157157
Some fields
158-
with Globals.Cancelled | DisplayException.DisplayException _ | Parser.TypePath _ as e ->
159-
raise e
160-
| _ ->
158+
with Error.Fatal_error _ | Error.Error _ | Failure _ | Not_found
159+
| Lexer.Error _ | Parser.Error _ | Typecore.Forbid_package _ ->
161160
Error.abort ("Could not load module " ^ (s_type_path (p,c))) null_pos
162161
end
163162

164163
let resolve_position_by_path ctx path p =
165164
let mt = ctx.g.do_load_type_def ctx p path in
166165
let p = (t_infos mt).mt_pos in
167-
raise_positions [p]
166+
DisplayException.send_positions_raise ctx.com [p]
168167

169168
let handle_path_display ctx path p =
170169
let class_field c name =
@@ -192,7 +191,7 @@ let handle_path_display ctx path p =
192191
which might not even exist anyway. *)
193192
let mt = ctx.g.do_load_module ctx (sl,s) p in
194193
let p = file_pos (Path.UniqueKey.lazy_path mt.m_extra.m_file) in
195-
raise_positions [p]
194+
DisplayException.send_positions_raise ctx.com [p]
196195
| (IDKModule(sl,s),_),DMHover ->
197196
let m = ctx.g.do_load_module ctx (sl,s) p in
198197
begin try

src/context/display/displayToplevel.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ let maybe_resolve_macro_field ctx t c cf =
4949
let (tl,tr,c,cf) = ctx.g.do_load_macro ctx false c.cl_path cf.cf_name null_pos in
5050
let t = perform_type_voodoo t tl tr in
5151
t,{cf with cf_type = t}
52-
with Globals.Cancelled as e -> raise e
53-
| _ ->
52+
with Exit | Not_found | Error.Fatal_error _ | Error.Error _ | Failure _ ->
5453
t,cf
5554

5655
let exclude : string list ref = ref []
@@ -158,7 +157,7 @@ let init_or_update_server cs com timer_name =
158157
try
159158
ignore(cc#find_file file_key);
160159
with Not_found ->
161-
try ignore(TypeloadParse.parse_module_file com file_path null_pos) with Globals.Cancelled as e -> raise e | _ -> ()
160+
try ignore(TypeloadParse.parse_module_file com file_path null_pos) with Lexer.Error _ | Parser.Error _ | Failure _ | Error.Error _ | Error.Fatal_error _ -> ()
162161
) removed_files;
163162
DynArray.iter (Hashtbl.remove removed_files) removed_removed_files
164163

src/context/display/findReferences.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ let find_references com with_definition =
133133
if c <> 0 then c else compare p1.pmin p2.pmin
134134
) usages
135135
in
136-
DisplayException.raise_positions usages
136+
DisplayException.send_positions_raise com usages
137137

138138
let find_implementations com name pos kind =
139139
let symbols,relations = Timer.time com.timer_ctx ["display";"implementations";"collect"] (Statistics.collect_statistics com [SFPos pos]) false in
@@ -153,9 +153,9 @@ let find_implementations com name pos kind =
153153
) usages
154154
) () in
155155
Display.ReferencePosition.reset();
156-
DisplayException.raise_positions usages
156+
DisplayException.send_positions_raise com usages
157157

158158
let find_implementations com =
159159
let name,pos,kind = Display.ReferencePosition.get () in
160160
if pos <> null_pos then find_implementations com name pos kind
161-
else DisplayException.raise_positions []
161+
else DisplayException.send_positions_raise com []

0 commit comments

Comments
 (0)