Skip to content

Commit cb19b8e

Browse files
committed
Refactor: clean up error handling for commands.
1 parent 3a857d9 commit cb19b8e

File tree

3 files changed

+33
-45
lines changed

3 files changed

+33
-45
lines changed

analysis/src/Commands.ml

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,18 @@ let dump files =
2626
let uri = Uri2.fromLocalPath path in
2727
let result =
2828
match ProcessCmt.getFullFromCmt ~uri with
29-
| Error message ->
30-
prerr_endline message;
31-
"[]"
32-
| Ok (package, {file; extra}) -> dumpLocations ~package ~file ~extra
29+
| None -> "[]"
30+
| Some (package, {file; extra}) ->
31+
dumpLocations ~package ~file ~extra
3332
in
3433
print_endline result)
3534

3635
let completion ~path ~line ~col ~currentFile =
3736
let uri = Uri2.fromLocalPath path in
3837
let result =
3938
match ProcessCmt.getFullFromCmt ~uri with
40-
| Error message ->
41-
prerr_endline message;
42-
"[]"
43-
| Ok (package, full) ->
39+
| None -> "[]"
40+
| Some (package, full) ->
4441
let maybeText = Files.readFile currentFile in
4542
NewCompletions.computeCompletions ~full ~maybeText ~package
4643
~pos:(line, col)
@@ -53,10 +50,8 @@ let hover ~path ~line ~col =
5350
let uri = Uri2.fromLocalPath path in
5451
let result =
5552
match ProcessCmt.getFullFromCmt ~uri with
56-
| Error message ->
57-
prerr_endline message;
58-
Protocol.null
59-
| Ok (package, {file; extra}) -> (
53+
| None -> Protocol.null
54+
| Some (package, {file; extra}) -> (
6055
let pos = Utils.protocolLineColToCmtLoc ~line ~col in
6156
match References.locItemForPos ~extra pos with
6257
| None -> Protocol.null
@@ -93,10 +88,8 @@ let definition ~path ~line ~col =
9388
let uri = Uri2.fromLocalPath path in
9489
let result =
9590
match ProcessCmt.getFullFromCmt ~uri with
96-
| Error message ->
97-
prerr_endline message;
98-
Protocol.null
99-
| Ok (package, {file; extra}) -> (
91+
| None -> Protocol.null
92+
| Some (package, {file; extra}) -> (
10093
let pos = Utils.protocolLineColToCmtLoc ~line ~col in
10194

10295
match References.locItemForPos ~extra pos with
@@ -132,10 +125,8 @@ let references ~path ~line ~col =
132125
let uri = Uri2.fromLocalPath path in
133126
let result =
134127
match ProcessCmt.getFullFromCmt ~uri with
135-
| Error message ->
136-
prerr_endline message;
137-
Protocol.null
138-
| Ok (package, {file; extra}) -> (
128+
| None -> Protocol.null
129+
| Some (package, {file; extra}) -> (
139130
let pos = Utils.protocolLineColToCmtLoc ~line ~col in
140131
match References.locItemForPos ~extra pos with
141132
| None -> Protocol.null
@@ -164,10 +155,8 @@ let references ~path ~line ~col =
164155
let documentSymbol ~path =
165156
let uri = Uri2.fromLocalPath path in
166157
match ProcessCmt.fileForUri uri with
167-
| Error message ->
168-
prerr_endline message;
169-
print_endline Protocol.null
170-
| Ok (file, _extra) ->
158+
| None -> print_endline Protocol.null
159+
| Some {file} ->
171160
let open SharedTypes in
172161
let rec getItems {topLevel} =
173162
let rec getItem = function

analysis/src/ProcessCmt.ml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,9 @@ let newDocsForCmt ~moduleName cmtCache changed cmt src =
11561156
let getFullFromCmt ~uri =
11571157
let path = Uri2.toPath uri in
11581158
match Packages.getPackage uri with
1159-
| Error e -> Error e
1159+
| Error message ->
1160+
prerr_endline message;
1161+
None
11601162
| Ok package -> (
11611163
let moduleName =
11621164
BuildSystem.namespacedName package.namespace (FindFiles.getName path)
@@ -1165,27 +1167,28 @@ let getFullFromCmt ~uri =
11651167
| Some paths -> (
11661168
let cmt = SharedTypes.getCmt ~interface:(Utils.endsWith path "i") paths in
11671169
match fullForCmt ~moduleName ~uri cmt with
1168-
| Error e -> Error e
1170+
| Error message ->
1171+
prerr_endline message;
1172+
None
11691173
| Ok full ->
11701174
Hashtbl.replace package.interModuleDependencies moduleName
11711175
(SharedTypes.hashList full.extra.externalReferences |> List.map fst);
1172-
Ok (package, full))
1173-
| None -> Error ("can't find module " ^ moduleName))
1176+
Some (package, full))
1177+
| None ->
1178+
prerr_endline ("can't find module " ^ moduleName);
1179+
None)
11741180

11751181
let fileForUri uri =
11761182
match getFullFromCmt ~uri with
1177-
| Error e -> Error e
1178-
| Ok (_package, {extra; file}) -> Ok (file, extra)
1183+
| None -> None
1184+
| Some (_package, full) -> Some full
11791185

11801186
let extraForModule ~package modname =
11811187
if Hashtbl.mem package.TopTypes.pathsForModule modname then
11821188
let paths = Hashtbl.find package.pathsForModule modname in
11831189
match SharedTypes.getSrc paths with
11841190
| None -> None
1185-
| Some src -> (
1186-
match getFullFromCmt ~uri:(Uri2.fromPath src) with
1187-
| Ok (_package, {extra}) -> Some extra
1188-
| Error _ -> None)
1191+
| Some src -> getFullFromCmt ~uri:(Uri2.fromPath src)
11891192
else None
11901193

11911194
let docsForCmt ~moduleName cmt src state =

analysis/src/References.ml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,8 @@ let alternateDeclared ~file ~package declared tip =
170170
let implUri = Uri2.fromPath impl in
171171
if intfUri = file.uri then
172172
match ProcessCmt.fileForUri implUri with
173-
| Error e ->
174-
Log.log e;
175-
None
176-
| Ok (file, extra) -> (
173+
| None -> None
174+
| Some {file; extra} -> (
177175
match
178176
declaredForExportedTip ~stamps:file.stamps
179177
~exported:file.contents.exported declared.name.txt tip
@@ -182,10 +180,8 @@ let alternateDeclared ~file ~package declared tip =
182180
| Some declared -> Some (file, extra, declared))
183181
else
184182
match ProcessCmt.fileForUri intfUri with
185-
| Error e ->
186-
Log.log e;
187-
None
188-
| Ok (file, extra) -> (
183+
| None -> None
184+
| Some {file; extra} -> (
189185
match
190186
declaredForExportedTip ~stamps:file.stamps
191187
~exported:file.contents.exported declared.name.txt tip
@@ -418,7 +414,7 @@ let forLocalStamp ~package ~file ~extra stamp tip =
418414
| Some file -> (
419415
match ProcessCmt.extraForModule ~package name with
420416
| None -> None
421-
| Some extra -> (
417+
| Some (_, {extra}) -> (
422418
match
423419
Hashtbl.find_opt extra.externalReferences
424420
thisModuleName
@@ -464,8 +460,8 @@ let allReferencesForLocItem ~package ~file ~extra locItem =
464460
| None -> []
465461
| Some stamp -> (
466462
match ProcessCmt.fileForUri env.qFile.uri with
467-
| Error _ -> []
468-
| Ok (file, extra) ->
463+
| None -> []
464+
| Some {file; extra} ->
469465
maybeLog
470466
("Finding references for (global) "
471467
^ Uri2.toString env.qFile.uri

0 commit comments

Comments
 (0)