Skip to content

Commit 8b074f7

Browse files
committed
Refactor Debug log in analysis
1 parent c527775 commit 8b074f7

File tree

13 files changed

+266
-367
lines changed

13 files changed

+266
-367
lines changed

analysis/src/Cmt.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let fullFromUri ~uri =
3030
in
3131
match incremental with
3232
| Some cmtInfo ->
33-
if Debug.verbose () then Printf.printf "[cmt] Found incremental cmt\n";
33+
Debug.verbose "[cmt] Found incremental cmt";
3434
Some cmtInfo
3535
| None -> (
3636
match Hashtbl.find_opt package.pathsForModule moduleName with

analysis/src/Commands.ml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ let completionResolve ~path ~modulePath =
2525
let docstring =
2626
match Cmt.loadFullCmtFromPath ~path with
2727
| None ->
28-
if Debug.verbose () then
29-
Printf.printf "[completion_resolve] Could not load cmt\n";
28+
Debug.verbose "[completion_resolve] Could not load cmt";
3029
Protocol.null
3130
| Some full -> (
3231
match ProcessCmt.fileForModule ~package:full.package moduleName with
3332
| None ->
34-
if Debug.verbose () then
35-
Printf.printf "[completion_resolve] Did not find file for module %s\n"
36-
moduleName;
33+
Debug.verbose "[completion_resolve] Did not find file for module %s"
34+
moduleName;
3735
Protocol.null
3836
| Some file ->
3937
file.structure.docstring |> String.concat "\n\n"
@@ -349,8 +347,7 @@ let test ~path =
349347
| "ve+" -> (
350348
let version = String.sub rest 3 (String.length rest - 3) in
351349
let version = String.trim version in
352-
if Debug.verbose () then
353-
Printf.printf "Setting version: %s\n" version;
350+
Debug.verbose "Setting version: %s" version;
354351
match String.split_on_char '.' version with
355352
| [majorRaw; minorRaw] ->
356353
let version = (int_of_string majorRaw, int_of_string minorRaw) in

analysis/src/CompletionBackEnd.ml

Lines changed: 72 additions & 93 deletions
Large diffs are not rendered by default.

analysis/src/CompletionExpressions.ml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,10 @@ let rec traverseExpr (exp : Parsetree.expression) ~exprPath ~pos
9898
([Completable.NFollowRecordField {fieldName = fname}] @ exprPath)
9999
)
100100
| None, None -> (
101-
if Debug.verbose () then (
102-
Printf.printf "[traverse_expr] No field with cursor and no expr hole.\n";
103-
104-
match firstCharBeforeCursorNoWhite with
105-
| None -> ()
106-
| Some c ->
107-
Printf.printf "[traverse_expr] firstCharBeforeCursorNoWhite: %c.\n" c);
101+
Debug.verbose "[traverse_expr] No field with cursor and no expr hole.";
102+
firstCharBeforeCursorNoWhite
103+
|> Option.iter
104+
(Debug.verbose "[traverse_expr] firstCharBeforeCursorNoWhite: %c.");
108105

109106
(* Figure out if we're completing for a new field.
110107
If the cursor is inside of the record body, but no field has the cursor,

analysis/src/CompletionFrontEnd.ml

Lines changed: 71 additions & 101 deletions
Large diffs are not rendered by default.

analysis/src/CompletionJsx.ml

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
328328
match props with
329329
| prop :: rest ->
330330
if prop.posStart <= posBeforeCursor && posBeforeCursor < prop.posEnd then (
331-
if Debug.verbose () then
332-
print_endline "[jsx_props_completable]--> Cursor on the prop name";
331+
Debug.verbose "[jsx_props_completable]--> Cursor on the prop name";
333332

334333
Some
335334
(Completable.Cjsx
@@ -340,16 +339,14 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
340339
prop.posEnd <= posBeforeCursor
341340
&& posBeforeCursor < Loc.start prop.exp.pexp_loc
342341
then (
343-
if Debug.verbose () then
344-
print_endline
345-
"[jsx_props_completable]--> Cursor between the prop name and expr \
346-
assigned";
342+
Debug.verbose
343+
"[jsx_props_completable]--> Cursor between the prop name and expr \
344+
assigned";
347345
match (firstCharBeforeCursorNoWhite, prop.exp) with
348346
| Some '=', {pexp_desc = Pexp_ident {txt = Lident txt}} ->
349-
if Debug.verbose () then
350-
Printf.printf
351-
"[jsx_props_completable]--> Heuristic for empty JSX prop expr \
352-
completion.\n";
347+
Debug.verbose
348+
"[jsx_props_completable]--> Heuristic for empty JSX prop expr \
349+
completion.";
353350
Some
354351
(Cexpression
355352
{
@@ -366,8 +363,7 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
366363
})
367364
| _ -> None)
368365
else if prop.exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then (
369-
if Debug.verbose () then
370-
print_endline "[jsx_props_completable]--> Cursor on expr assigned";
366+
Debug.verbose "[jsx_props_completable]--> Cursor on expr assigned";
371367
match
372368
CompletionExpressions.traverseExpr prop.exp ~exprPath:[]
373369
~pos:posBeforeCursor ~firstCharBeforeCursorNoWhite
@@ -389,16 +385,14 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
389385
})
390386
| _ -> None)
391387
else if prop.exp.pexp_loc |> Loc.end_ = (Location.none |> Loc.end_) then (
392-
if Debug.verbose () then
393-
print_endline "[jsx_props_completable]--> Loc is broken";
388+
Debug.verbose "[jsx_props_completable]--> Loc is broken";
394389
if
395390
CompletionExpressions.isExprHole prop.exp
396391
|| isRegexpJsxHeuristicExpr prop.exp
397392
then (
398-
if Debug.verbose () then
399-
print_endline
400-
"[jsx_props_completable]--> Expr was expr hole or regexp literal \
401-
heuristic";
393+
Debug.verbose
394+
"[jsx_props_completable]--> Expr was expr hole or regexp literal \
395+
heuristic";
402396
Some
403397
(Cexpression
404398
{
@@ -422,10 +416,8 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
422416
The completion comes at the end of the component, after the equals sign, but before any
423417
children starts, and '>' marks that it's at the end of the component JSX.
424418
This little heuristic makes sure we pick up this special case. *)
425-
if Debug.verbose () then
426-
print_endline
427-
"[jsx_props_completable]--> Special case: last prop, '>' after \
428-
cursor";
419+
Debug.verbose
420+
"[jsx_props_completable]--> Special case: last prop, '>' after cursor";
429421
Some
430422
(Cexpression
431423
{
@@ -444,8 +436,7 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
444436
| [] ->
445437
let afterCompName = posBeforeCursor >= posAfterCompName in
446438
if afterCompName && beforeChildrenStart then (
447-
if Debug.verbose () then
448-
print_endline "[jsx_props_completable]--> Complete for JSX prop name";
439+
Debug.verbose "[jsx_props_completable]--> Complete for JSX prop name";
449440
Some
450441
(Cjsx
451442
( Utils.flattenLongIdent ~jsx:true jsxProps.compName.txt,

analysis/src/CompletionPatterns.ml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ and traversePattern (pat : Parsetree.pattern) ~patternPath ~locHasCursor
3737
~firstCharBeforeCursorNoWhite ~posBeforeCursor =
3838
let someIfHasCursor v debugId =
3939
if locHasCursor pat.Parsetree.ppat_loc then (
40-
if Debug.verbose () then
41-
Printf.printf
42-
"[traversePattern:someIfHasCursor] '%s' has cursor, returning \n"
43-
debugId;
40+
Debug.verbose
41+
"[traversePattern:someIfHasCursor] '%s' has cursor, returning" debugId;
4442
Some v)
4543
else None
4644
in
@@ -64,9 +62,7 @@ and traversePattern (pat : Parsetree.pattern) ~patternPath ~locHasCursor
6462
in
6563
match orPatWithItem with
6664
| None when isPatternHole p1 || isPatternHole p2 ->
67-
if Debug.verbose () then
68-
Printf.printf
69-
"[traversePattern] found or-pattern that was pattern hole\n";
65+
Debug.verbose "[traversePattern] found or-pattern that was pattern hole";
7066
Some ("", patternPath)
7167
| v -> v)
7268
| Ppat_any ->

analysis/src/Debug.ml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@ type debugLevel = Off | Regular | Verbose
22

33
let debugLevel = ref Off
44

5-
let log s =
6-
match !debugLevel with
7-
| Regular | Verbose -> print_endline s
8-
| Off -> ()
9-
105
let debugPrintEnv (env : SharedTypes.QueryEnv.t) =
116
env.pathRev @ [env.file.moduleName] |> List.rev |> String.concat "."
127

13-
let verbose () = !debugLevel = Verbose
8+
(**
9+
Log formatted message to the std_out when the debugLevel is Regular or Verbose.
10+
Automatically appends a trailing newline.
11+
*)
12+
let regular fmt =
13+
match !debugLevel with
14+
| Regular | Verbose -> Format.printf (fmt ^^ "\n")
15+
| Off -> Format.ifprintf Format.std_formatter fmt
16+
17+
(**
18+
Log formatted message to the std_out when the debugLevel is Verbose.
19+
Automatically appends a trailing newline.
20+
*)
21+
let verbose fmt =
22+
match !debugLevel with
23+
| Off | Regular -> Format.ifprintf Format.std_formatter fmt
24+
| Verbose -> Format.printf (fmt ^^ "\n")

analysis/src/DotCompletionUtils.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ let fieldCompletionsForDotCompletion ?posOfDot typ ~env ~package ~prefix ~exact
1414
match asObject with
1515
| Some (objEnv, obj) ->
1616
(* Handle obj completion via dot *)
17-
if Debug.verbose () then
18-
Printf.printf "[dot_completion]--> Obj type found:\n";
17+
Debug.verbose "[dot_completion]--> Obj type found:";
1918
obj |> TypeUtils.getObjFields
2019
|> Utils.filterMap (fun (field, _typ) ->
2120
if Utils.checkName field ~prefix ~exact then

analysis/src/SignatureHelp.ml

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,19 @@ let findFunctionType ~currentFile ~debug ~path ~pos =
4848
| None -> []
4949
| Some (docstring, _) -> docstring
5050
in
51-
if Debug.verbose () then
52-
Printf.printf "[sig_help_fn] Found loc item: %s.\n"
53-
(Shared.typeToString typeExpr);
51+
Debug.verbose "[sig_help_fn] Found loc item: %s."
52+
(Shared.typeToString typeExpr);
5453
match
5554
TypeUtils.extractFunctionType2 ~env ~package:full.package typeExpr
5655
with
5756
| args, _tRet, _ when args <> [] ->
5857
Some (args, docstring, typeExpr, package, env, file)
5958
| _ -> None)
6059
| None ->
61-
if Debug.verbose () then
62-
Printf.printf "[sig_help_fn] Found no loc item.\n";
60+
Debug.verbose "[sig_help_fn] Found no loc item.";
6361
None
6462
| Some _ ->
65-
if Debug.verbose () then
66-
Printf.printf
67-
"[sig_help_fn] Found loc item, but not what was expected.\n";
63+
Debug.verbose "[sig_help_fn] Found loc item, but not what was expected.";
6864
None
6965
in
7066
match fnFromLocItem with
@@ -272,25 +268,22 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
272268
| _ -> (
273269
match !result with
274270
| None ->
275-
if Debug.verbose () then
276-
Printf.printf "[sig_help_result] Setting because had none\n";
271+
Debug.verbose "[sig_help_result] Setting because had none";
277272
result := Some (loc, thing)
278273
| Some (currentLoc, currentThing)
279274
when Pos.ofLexing loc.Location.loc_start
280275
> Pos.ofLexing currentLoc.Location.loc_start ->
281276
result := Some (loc, thing);
282277

283-
if Debug.verbose () then
284-
Printf.printf
285-
"[sig_help_result] Setting because loc of %s > then existing \
286-
of %s\n"
287-
(printThing thing) (printThing currentThing)
278+
Debug.verbose
279+
"[sig_help_result] Setting because loc of %s > then existing of \
280+
%s"
281+
(printThing thing) (printThing currentThing)
288282
| Some (_, currentThing) ->
289-
if Debug.verbose () then
290-
Printf.printf
291-
"[sig_help_result] Doing nothing because loc of %s < then \
292-
existing of %s\n"
293-
(printThing thing) (printThing currentThing))
283+
Debug.verbose
284+
"[sig_help_result] Doing nothing because loc of %s < then \
285+
existing of %s"
286+
(printThing thing) (printThing currentThing))
294287
in
295288
let searchForArgWithCursor ~isPipeExpr ~args =
296289
let extractedArgs = extractExpApplyArgs ~args in
@@ -525,12 +518,10 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
525518
| _ -> None)
526519
| Some (_, ((`ConstructorExpr (lid, _) | `ConstructorPat (lid, _)) as cs))
527520
-> (
528-
if Debug.verbose () then
529-
Printf.printf "[signature_help] Found constructor!\n";
521+
Debug.verbose "[signature_help] Found constructor!";
530522
match Cmt.loadFullCmtFromPath ~path with
531523
| None ->
532-
if Debug.verbose () then
533-
Printf.printf "[signature_help] Could not load cmt\n";
524+
Debug.verbose "[signature_help] Could not load cmt";
534525
None
535526
| Some full -> (
536527
let {file} = full in
@@ -541,9 +532,8 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
541532
{lid.loc with loc_start = lid.loc.loc_end}
542533
with
543534
| None ->
544-
if Debug.verbose () then
545-
Printf.printf "[signature_help] Did not find constructor '%s'\n"
546-
constructorName;
535+
Debug.verbose "[signature_help] Did not find constructor '%s'"
536+
constructorName;
547537
None
548538
| Some constructor ->
549539
let argParts =

0 commit comments

Comments
 (0)