diff --git a/analysis/src/Cli.ml b/analysis/src/Cli.ml index 4728a2478..a49326d92 100644 --- a/analysis/src/Cli.ml +++ b/analysis/src/Cli.ml @@ -72,18 +72,22 @@ let main () = ~pos:(int_of_string line, int_of_string col) ~currentFile | [_; "definition"; path; line; col] -> - Commands.definition ~path ~line:(int_of_string line) - ~col:(int_of_string col) + Commands.definition ~path + ~pos:(int_of_string line, int_of_string col) + ~debug:false | [_; "typeDefinition"; path; line; col] -> - Commands.typeDefinition ~path ~line:(int_of_string line) - ~col:(int_of_string col) + Commands.typeDefinition ~path + ~pos:(int_of_string line, int_of_string col) + ~debug:false | [_; "documentSymbol"; path] -> DocumentSymbol.command ~path | [_; "hover"; path; line; col; currentFile] -> - Commands.hover ~path ~line:(int_of_string line) ~col:(int_of_string col) + Commands.hover ~path + ~pos:(int_of_string line, int_of_string col) ~currentFile ~debug:false | [_; "codeAction"; path; line; col; currentFile] -> - Commands.codeAction ~path ~line:(int_of_string line) - ~col:(int_of_string col) ~currentFile + Commands.codeAction ~path + ~pos:(int_of_string line, int_of_string col) + ~currentFile ~debug:false | _ :: "reanalyze" :: _ -> let len = Array.length Sys.argv in for i = 1 to len - 2 do @@ -92,11 +96,13 @@ let main () = Sys.argv.(len - 1) <- ""; Reanalyze.cli () | [_; "references"; path; line; col] -> - Commands.references ~path ~line:(int_of_string line) - ~col:(int_of_string col) + Commands.references ~path + ~pos:(int_of_string line, int_of_string col) + ~debug:false | [_; "rename"; path; line; col; newName] -> - Commands.rename ~path ~line:(int_of_string line) ~col:(int_of_string col) - ~newName + Commands.rename ~path + ~pos:(int_of_string line, int_of_string col) + ~newName ~debug:false | [_; "semanticTokens"; currentFile] -> SemanticTokens.semanticTokens ~currentFile | [_; "createInterface"; path; cmiFile] -> diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index 5ea11a58f..d6fdcc56c 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -28,19 +28,18 @@ let completion ~debug ~path ~pos ~currentFile = |> List.map Protocol.stringifyCompletionItem |> Protocol.array) -let hover ~path ~line ~col ~currentFile ~debug = +let hover ~path ~pos ~currentFile ~debug = let result = match Cmt.fullFromPath ~path with | None -> Protocol.null | Some full -> ( - match References.getLocItem ~full ~line ~col with + match References.getLocItem ~full ~pos ~debug with | None -> ( if debug then Printf.printf "Nothing at that position. Now trying to use completion.\n"; let completions = - getCompletions ~debug ~path ~pos:(line, col) ~currentFile - ~forHover:true + getCompletions ~debug ~path ~pos ~currentFile ~forHover:true in match completions with | {kind = Label typString; docstring} :: _ -> @@ -81,16 +80,16 @@ let hover ~path ~line ~col ~currentFile ~debug = in print_endline result -let codeAction ~path ~line ~col ~currentFile = - Xform.extractCodeActions ~path ~pos:(line, col) ~currentFile +let codeAction ~path ~pos ~currentFile ~debug = + Xform.extractCodeActions ~path ~pos ~currentFile ~debug |> CodeActions.stringifyCodeActions |> print_endline -let definition ~path ~line ~col = +let definition ~path ~pos ~debug = let locationOpt = match Cmt.fullFromPath ~path with | None -> None | Some full -> ( - match References.getLocItem ~full ~line ~col with + match References.getLocItem ~full ~pos ~debug with | None -> None | Some locItem -> ( match References.definitionForLocItem ~full locItem with @@ -123,12 +122,12 @@ let definition ~path ~line ~col = | None -> Protocol.null | Some location -> location |> Protocol.stringifyLocation) -let typeDefinition ~path ~line ~col = +let typeDefinition ~path ~pos ~debug = let maybeLocation = match Cmt.fullFromPath ~path with | None -> None | Some full -> ( - match References.getLocItem ~full ~line ~col with + match References.getLocItem ~full ~pos ~debug with | None -> None | Some locItem -> ( match References.typeDefinitionForLocItem ~full locItem with @@ -143,12 +142,12 @@ let typeDefinition ~path ~line ~col = | None -> Protocol.null | Some location -> location |> Protocol.stringifyLocation) -let references ~path ~line ~col = +let references ~path ~pos ~debug = let allLocs = match Cmt.fullFromPath ~path with | None -> [] | Some full -> ( - match References.getLocItem ~full ~line ~col with + match References.getLocItem ~full ~pos ~debug with | None -> [] | Some locItem -> let allReferences = References.allReferencesForLocItem ~full locItem in @@ -169,12 +168,12 @@ let references ~path ~line ~col = (if allLocs = [] then Protocol.null else "[\n" ^ (allLocs |> String.concat ",\n") ^ "\n]") -let rename ~path ~line ~col ~newName = +let rename ~path ~pos ~newName ~debug = let result = match Cmt.fullFromPath ~path with | None -> Protocol.null | Some full -> ( - match References.getLocItem ~full ~line ~col with + match References.getLocItem ~full ~pos ~debug with | None -> Protocol.null | Some locItem -> let allReferences = References.allReferencesForLocItem ~full locItem in @@ -305,24 +304,24 @@ let test ~path = print_endline ("Definition " ^ path ^ " " ^ string_of_int line ^ ":" ^ string_of_int col); - definition ~path ~line ~col + definition ~path ~pos:(line, col) ~debug:true | "typ" -> print_endline ("TypeDefinition " ^ path ^ " " ^ string_of_int line ^ ":" ^ string_of_int col); - typeDefinition ~path ~line ~col + typeDefinition ~path ~pos:(line, col) ~debug:true | "hov" -> print_endline ("Hover " ^ path ^ " " ^ string_of_int line ^ ":" ^ string_of_int col); let currentFile = createCurrentFile () in - hover ~path ~line ~col ~currentFile ~debug:true; + hover ~path ~pos:(line, col) ~currentFile ~debug:true; Sys.remove currentFile | "ref" -> print_endline ("References " ^ path ^ " " ^ string_of_int line ^ ":" ^ string_of_int col); - references ~path ~line ~col + references ~path ~pos:(line, col) ~debug:true | "doc" -> print_endline ("DocumentSymbol " ^ path); DocumentSymbol.command ~path @@ -333,7 +332,7 @@ let test ~path = ("Rename " ^ path ^ " " ^ string_of_int line ^ ":" ^ string_of_int col ^ " " ^ newName) in - rename ~path ~line ~col ~newName + rename ~path ~pos:(line, col) ~newName ~debug:true | "com" -> print_endline ("Complete " ^ path ^ " " ^ string_of_int line ^ ":" @@ -362,6 +361,7 @@ let test ~path = ^ string_of_int col); let codeActions = Xform.extractCodeActions ~path ~pos:(line, col) ~currentFile:path + ~debug:true in codeActions |> List.iter (fun {Protocol.title; edit = {documentChanges}} -> diff --git a/analysis/src/References.ml b/analysis/src/References.ml index 33f29439e..5c4916dcf 100644 --- a/analysis/src/References.ml +++ b/analysis/src/References.ml @@ -1,6 +1,7 @@ open SharedTypes let debugReferences = ref true + let maybeLog m = if !debugReferences then Log.log ("[ref] " ^ m) let checkPos (line, char) @@ -16,43 +17,56 @@ let checkPos (line, char) let locItemsForPos ~extra pos = extra.locItems |> List.filter (fun {loc; locType = _} -> checkPos pos loc) -let lineColToCmtLoc ~line ~col = (line + 1, col) +let lineColToCmtLoc ~pos:(line, col) = (line + 1, col) -let getLocItem ~full ~line ~col = - let pos = lineColToCmtLoc ~line ~col in +let getLocItem ~full ~pos ~debug = + let log n msg = if debug then Printf.printf "getLocItem #%d: %s\n" n msg in + let pos = lineColToCmtLoc ~pos in let locItems = locItemsForPos ~extra:full.extra pos in if !Log.verbose then print_endline ("locItems:\n " ^ (locItems |> List.map locItemToString |> String.concat "\n ")); + let nameOf li = + match li.locType with Typed (n, _, _) -> n | _ -> "NotFound" + in match locItems with - | _ :: _ :: _ :: ({locType = Typed ("makeProps", _, _)} as li) :: _ + | li1 :: li2 :: li3 :: ({locType = Typed ("makeProps", _, _)} as li4) :: _ when full.file.uri |> Uri2.isInterface -> - (* heuristic for makeProps in interface files *) - Some li + log 1 "heuristic for makeProps in interface files"; + if debug then + Printf.printf "n1:%s n2:%s n3:%s\n" (nameOf li1) (nameOf li2) (nameOf li3); + Some li4 | [ {locType = Typed ("fragment", _, _)}; {locType = Typed ("createElement", _, _)}; ] -> - (* heuristic for within a fragment *) + log 2 "heuristic for within a fragment"; None | [ {locType = Constant _}; ({locType = Typed ("createDOMElementVariadic", _, _)} as li2); ] -> - (* heuristic for