Skip to content

Refactor Debug log in analysis #7276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analysis/src/Cmt.ml
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ let fullFromUri ~uri =
in
match incremental with
| Some cmtInfo ->
if Debug.verbose () then Printf.printf "[cmt] Found incremental cmt\n";
Debug.verbose "[cmt] Found incremental cmt";
Some cmtInfo
| None -> (
match Hashtbl.find_opt package.pathsForModule moduleName with
11 changes: 4 additions & 7 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
@@ -25,15 +25,13 @@ let completionResolve ~path ~modulePath =
let docstring =
match Cmt.loadFullCmtFromPath ~path with
| None ->
if Debug.verbose () then
Printf.printf "[completion_resolve] Could not load cmt\n";
Debug.verbose "[completion_resolve] Could not load cmt";
Protocol.null
| Some full -> (
match ProcessCmt.fileForModule ~package:full.package moduleName with
| None ->
if Debug.verbose () then
Printf.printf "[completion_resolve] Did not find file for module %s\n"
moduleName;
Debug.verbose "[completion_resolve] Did not find file for module %s"
moduleName;
Protocol.null
| Some file ->
file.structure.docstring |> String.concat "\n\n"
@@ -349,8 +347,7 @@ let test ~path =
| "ve+" -> (
let version = String.sub rest 3 (String.length rest - 3) in
let version = String.trim version in
if Debug.verbose () then
Printf.printf "Setting version: %s\n" version;
Debug.verbose "Setting version: %s" version;
match String.split_on_char '.' version with
| [majorRaw; minorRaw] ->
let version = (int_of_string majorRaw, int_of_string minorRaw) in
167 changes: 73 additions & 94 deletions analysis/src/CompletionBackEnd.ml

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions analysis/src/CompletionExpressions.ml
Original file line number Diff line number Diff line change
@@ -98,13 +98,10 @@ let rec traverseExpr (exp : Parsetree.expression) ~exprPath ~pos
([Completable.NFollowRecordField {fieldName = fname}] @ exprPath)
)
| None, None -> (
if Debug.verbose () then (
Printf.printf "[traverse_expr] No field with cursor and no expr hole.\n";

match firstCharBeforeCursorNoWhite with
| None -> ()
| Some c ->
Printf.printf "[traverse_expr] firstCharBeforeCursorNoWhite: %c.\n" c);
Debug.verbose "[traverse_expr] No field with cursor and no expr hole.";
firstCharBeforeCursorNoWhite
|> Option.iter
(Debug.verbose "[traverse_expr] firstCharBeforeCursorNoWhite: %c.");

(* Figure out if we're completing for a new field.
If the cursor is inside of the record body, but no field has the cursor,
172 changes: 71 additions & 101 deletions analysis/src/CompletionFrontEnd.ml

Large diffs are not rendered by default.

39 changes: 15 additions & 24 deletions analysis/src/CompletionJsx.ml
Original file line number Diff line number Diff line change
@@ -328,8 +328,7 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
match props with
| prop :: rest ->
if prop.posStart <= posBeforeCursor && posBeforeCursor < prop.posEnd then (
if Debug.verbose () then
print_endline "[jsx_props_completable]--> Cursor on the prop name";
Debug.verbose "[jsx_props_completable]--> Cursor on the prop name";

Some
(Completable.Cjsx
@@ -340,16 +339,14 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
prop.posEnd <= posBeforeCursor
&& posBeforeCursor < Loc.start prop.exp.pexp_loc
then (
if Debug.verbose () then
print_endline
"[jsx_props_completable]--> Cursor between the prop name and expr \
assigned";
Debug.verbose
"[jsx_props_completable]--> Cursor between the prop name and expr \
assigned";
match (firstCharBeforeCursorNoWhite, prop.exp) with
| Some '=', {pexp_desc = Pexp_ident {txt = Lident txt}} ->
if Debug.verbose () then
Printf.printf
"[jsx_props_completable]--> Heuristic for empty JSX prop expr \
completion.\n";
Debug.verbose
"[jsx_props_completable]--> Heuristic for empty JSX prop expr \
completion.";
Some
(Cexpression
{
@@ -366,8 +363,7 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
})
| _ -> None)
else if prop.exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then (
if Debug.verbose () then
print_endline "[jsx_props_completable]--> Cursor on expr assigned";
Debug.verbose "[jsx_props_completable]--> Cursor on expr assigned";
match
CompletionExpressions.traverseExpr prop.exp ~exprPath:[]
~pos:posBeforeCursor ~firstCharBeforeCursorNoWhite
@@ -389,16 +385,14 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
})
| _ -> None)
else if prop.exp.pexp_loc |> Loc.end_ = (Location.none |> Loc.end_) then (
if Debug.verbose () then
print_endline "[jsx_props_completable]--> Loc is broken";
Debug.verbose "[jsx_props_completable]--> Loc is broken";
if
CompletionExpressions.isExprHole prop.exp
|| isRegexpJsxHeuristicExpr prop.exp
then (
if Debug.verbose () then
print_endline
"[jsx_props_completable]--> Expr was expr hole or regexp literal \
heuristic";
Debug.verbose
"[jsx_props_completable]--> Expr was expr hole or regexp literal \
heuristic";
Some
(Cexpression
{
@@ -422,10 +416,8 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
The completion comes at the end of the component, after the equals sign, but before any
children starts, and '>' marks that it's at the end of the component JSX.
This little heuristic makes sure we pick up this special case. *)
if Debug.verbose () then
print_endline
"[jsx_props_completable]--> Special case: last prop, '>' after \
cursor";
Debug.verbose
"[jsx_props_completable]--> Special case: last prop, '>' after cursor";
Some
(Cexpression
{
@@ -444,8 +436,7 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
| [] ->
let afterCompName = posBeforeCursor >= posAfterCompName in
if afterCompName && beforeChildrenStart then (
if Debug.verbose () then
print_endline "[jsx_props_completable]--> Complete for JSX prop name";
Debug.verbose "[jsx_props_completable]--> Complete for JSX prop name";
Some
(Cjsx
( Utils.flattenLongIdent ~jsx:true jsxProps.compName.txt,
10 changes: 3 additions & 7 deletions analysis/src/CompletionPatterns.ml
Original file line number Diff line number Diff line change
@@ -37,10 +37,8 @@ and traversePattern (pat : Parsetree.pattern) ~patternPath ~locHasCursor
~firstCharBeforeCursorNoWhite ~posBeforeCursor =
let someIfHasCursor v debugId =
if locHasCursor pat.Parsetree.ppat_loc then (
if Debug.verbose () then
Printf.printf
"[traversePattern:someIfHasCursor] '%s' has cursor, returning \n"
debugId;
Debug.verbose
"[traversePattern:someIfHasCursor] '%s' has cursor, returning" debugId;
Some v)
else None
in
@@ -64,9 +62,7 @@ and traversePattern (pat : Parsetree.pattern) ~patternPath ~locHasCursor
in
match orPatWithItem with
| None when isPatternHole p1 || isPatternHole p2 ->
if Debug.verbose () then
Printf.printf
"[traversePattern] found or-pattern that was pattern hole\n";
Debug.verbose "[traversePattern] found or-pattern that was pattern hole";
Some ("", patternPath)
| v -> v)
| Ppat_any ->
23 changes: 17 additions & 6 deletions analysis/src/Debug.ml
Original file line number Diff line number Diff line change
@@ -2,12 +2,23 @@ type debugLevel = Off | Regular | Verbose

let debugLevel = ref Off

let log s =
match !debugLevel with
| Regular | Verbose -> print_endline s
| Off -> ()

let debugPrintEnv (env : SharedTypes.QueryEnv.t) =
env.pathRev @ [env.file.moduleName] |> List.rev |> String.concat "."

let verbose () = !debugLevel = Verbose
(**
Log formatted message to the std_out when the debugLevel is Regular or Verbose.
Automatically appends a trailing newline.
*)
let regular fmt =
match !debugLevel with
| Regular | Verbose -> Format.printf (fmt ^^ "\n")
| Off -> Format.ifprintf Format.std_formatter fmt

(**
Log formatted message to the std_out when the debugLevel is Verbose.
Automatically appends a trailing newline.
*)
let verbose fmt =
match !debugLevel with
| Off | Regular -> Format.ifprintf Format.std_formatter fmt
| Verbose -> Format.printf (fmt ^^ "\n")
3 changes: 1 addition & 2 deletions analysis/src/DotCompletionUtils.ml
Original file line number Diff line number Diff line change
@@ -14,8 +14,7 @@ let fieldCompletionsForDotCompletion ?posOfDot typ ~env ~package ~prefix ~exact
match asObject with
| Some (objEnv, obj) ->
(* Handle obj completion via dot *)
if Debug.verbose () then
Printf.printf "[dot_completion]--> Obj type found:\n";
Debug.verbose "[dot_completion]--> Obj type found:";
obj |> TypeUtils.getObjFields
|> Utils.filterMap (fun (field, _typ) ->
if Utils.checkName field ~prefix ~exact then
44 changes: 17 additions & 27 deletions analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
@@ -48,23 +48,19 @@ let findFunctionType ~currentFile ~debug ~path ~pos =
| None -> []
| Some (docstring, _) -> docstring
in
if Debug.verbose () then
Printf.printf "[sig_help_fn] Found loc item: %s.\n"
(Shared.typeToString typeExpr);
Debug.verbose "[sig_help_fn] Found loc item: %s."
(Shared.typeToString typeExpr);
match
TypeUtils.extractFunctionType2 ~env ~package:full.package typeExpr
with
| args, _tRet, _ when args <> [] ->
Some (args, docstring, typeExpr, package, env, file)
| _ -> None)
| None ->
if Debug.verbose () then
Printf.printf "[sig_help_fn] Found no loc item.\n";
Debug.verbose "[sig_help_fn] Found no loc item.";
None
| Some _ ->
if Debug.verbose () then
Printf.printf
"[sig_help_fn] Found loc item, but not what was expected.\n";
Debug.verbose "[sig_help_fn] Found loc item, but not what was expected.";
None
in
match fnFromLocItem with
@@ -272,25 +268,22 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
| _ -> (
match !result with
| None ->
if Debug.verbose () then
Printf.printf "[sig_help_result] Setting because had none\n";
Debug.verbose "[sig_help_result] Setting because had none";
result := Some (loc, thing)
| Some (currentLoc, currentThing)
when Pos.ofLexing loc.Location.loc_start
> Pos.ofLexing currentLoc.Location.loc_start ->
result := Some (loc, thing);

if Debug.verbose () then
Printf.printf
"[sig_help_result] Setting because loc of %s > then existing \
of %s\n"
(printThing thing) (printThing currentThing)
Debug.verbose
"[sig_help_result] Setting because loc of %s > then existing of \
%s"
(printThing thing) (printThing currentThing)
| Some (_, currentThing) ->
if Debug.verbose () then
Printf.printf
"[sig_help_result] Doing nothing because loc of %s < then \
existing of %s\n"
(printThing thing) (printThing currentThing))
Debug.verbose
"[sig_help_result] Doing nothing because loc of %s < then \
existing of %s"
(printThing thing) (printThing currentThing))
in
let searchForArgWithCursor ~isPipeExpr ~args =
let extractedArgs = extractExpApplyArgs ~args in
@@ -525,12 +518,10 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
| _ -> None)
| Some (_, ((`ConstructorExpr (lid, _) | `ConstructorPat (lid, _)) as cs))
-> (
if Debug.verbose () then
Printf.printf "[signature_help] Found constructor!\n";
Debug.verbose "[signature_help] Found constructor!";
match Cmt.loadFullCmtFromPath ~path with
| None ->
if Debug.verbose () then
Printf.printf "[signature_help] Could not load cmt\n";
Debug.verbose "[signature_help] Could not load cmt";
None
| Some full -> (
let {file} = full in
@@ -541,9 +532,8 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
{lid.loc with loc_start = lid.loc.loc_end}
with
| None ->
if Debug.verbose () then
Printf.printf "[signature_help] Did not find constructor '%s'\n"
constructorName;
Debug.verbose "[signature_help] Did not find constructor '%s'"
constructorName;
None
| Some constructor ->
let argParts =
Loading