Skip to content

Commit

Permalink
only look up project files etc when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Jun 3, 2024
1 parent 51aec1c commit bc71f76
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 119 deletions.
6 changes: 3 additions & 3 deletions analysis/src/Cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ let targetFileFromLibBs libBs = Filename.concat libBs ".project-files-cache"
let cacheProject (package : package) =
let cached =
{
projectFiles = package.projectFiles;
dependenciesFiles = package.dependenciesFiles;
pathsForModule = package.pathsForModule;
projectFiles = Lazy.force package.projectFiles;
dependenciesFiles = Lazy.force package.dependenciesFiles;
pathsForModule = Lazy.force package.pathsForModule;
}
in
match BuildSystem.getLibBs package.rootPath with
Expand Down
6 changes: 3 additions & 3 deletions analysis/src/Cmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let fullFromUri ~uri =
if Debug.verbose () then Printf.printf "[cmt] Found incremental cmt\n";
Some cmtInfo
| None -> (
match Hashtbl.find_opt package.pathsForModule moduleName with
match Hashtbl.find_opt (Lazy.force package.pathsForModule) moduleName with
| Some paths ->
let cmt = getCmtPath ~uri paths in
fullForCmt ~moduleName ~package ~uri cmt
Expand All @@ -42,8 +42,8 @@ let fullFromUri ~uri =
None))

let fullsFromModule ~package ~moduleName =
if Hashtbl.mem package.pathsForModule moduleName then
let paths = Hashtbl.find package.pathsForModule moduleName in
if Hashtbl.mem (Lazy.force package.pathsForModule) moduleName then
let paths = Hashtbl.find (Lazy.force package.pathsForModule) moduleName in
let uris = getUris paths in
uris |> List.filter_map (fun uri -> fullFromUri ~uri)
else []
Expand Down
7 changes: 4 additions & 3 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
resultModulePath;
regexpModulePath;
} =
package.builtInCompletionModules
Lazy.force package.builtInCompletionModules
in
Some
(match builtin with
Expand Down Expand Up @@ -1316,7 +1316,7 @@ let getOpens ~debug ~rawOpens ~package ~env =
^ string_of_int (List.length rawOpens)
^ " "
^ String.concat " ... " (rawOpens |> List.map pathToString));
let packageOpens = package.opens in
let packageOpens = Lazy.force package.opens in
if debug && packageOpens <> [] then
Printf.printf "%s\n"
("Package opens "
Expand Down Expand Up @@ -1812,7 +1812,8 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
if Debug.verbose () then print_endline "[complete_typed_value]--> Texn";
[
create
(full.package.builtInCompletionModules.exnModulePath @ ["Error(error)"]
((Lazy.force full.package.builtInCompletionModules).exnModulePath
@ ["Error(error)"]
|> ident)
~kind:(Label "Catches errors from JavaScript errors.")
~docstring:
Expand Down
214 changes: 118 additions & 96 deletions analysis/src/Packages.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,70 +74,91 @@ let newBsPackage ~rootPath =
let projectFiles, dependenciesFiles, pathsForModule =
match cached with
| Some cached ->
( cached.projectFiles,
cached.dependenciesFiles,
cached.pathsForModule )
( Lazy.from_val cached.projectFiles,
Lazy.from_val cached.dependenciesFiles,
Lazy.from_val cached.pathsForModule )
| None ->
let dependenciesFilesAndPaths =
match FindFiles.findDependencyFiles rootPath config with
| None -> []
| Some (_dependencyDirectories, dependenciesFilesAndPaths) ->
dependenciesFilesAndPaths
Lazy.from_fun (fun () ->
match FindFiles.findDependencyFiles rootPath config with
| None -> []
| Some (_dependencyDirectories, dependenciesFilesAndPaths) ->
dependenciesFilesAndPaths)
in
let sourceDirectories =
FindFiles.getSourceDirectories ~includeDev:true ~baseDir:rootPath
config
Lazy.from_fun (fun () ->
FindFiles.getSourceDirectories ~includeDev:true
~baseDir:rootPath config)
in
let projectFilesAndPaths =
FindFiles.findProjectFiles
~public:(FindFiles.getPublic config)
~namespace ~path:rootPath ~sourceDirectories ~libBs
Lazy.from_fun (fun () ->
FindFiles.findProjectFiles
~public:(FindFiles.getPublic config)
~namespace ~path:rootPath
~sourceDirectories:(Lazy.force sourceDirectories)
~libBs)
in
let pathsForModule =
makePathsForModule ~projectFilesAndPaths
~dependenciesFilesAndPaths
Lazy.from_fun (fun () ->
makePathsForModule
~projectFilesAndPaths:(Lazy.force projectFilesAndPaths)
~dependenciesFilesAndPaths:
(Lazy.force dependenciesFilesAndPaths))
in
let projectFiles =
projectFilesAndPaths |> List.map fst |> FileSet.of_list
Lazy.from_fun (fun () ->
projectFilesAndPaths |> Lazy.force |> List.map fst
|> FileSet.of_list)
in
let dependenciesFiles =
dependenciesFilesAndPaths |> List.map fst |> FileSet.of_list
Lazy.from_fun (fun () ->
dependenciesFilesAndPaths |> Lazy.force |> List.map fst
|> FileSet.of_list)
in
(projectFiles, dependenciesFiles, pathsForModule)
in
Some
(let opens_from_namespace =
match namespace with
| None -> []
| Some namespace ->
let cmt = Filename.concat libBs namespace ^ ".cmt" in
Hashtbl.add pathsForModule namespace (Namespace {cmt});
let path = [FindFiles.nameSpaceToName namespace] in
[path]
Lazy.from_fun (fun () ->
match namespace with
| None -> []
| Some namespace ->
let cmt = Filename.concat libBs namespace ^ ".cmt" in
Hashtbl.add
(Lazy.force pathsForModule)
namespace
(Namespace {cmt});
let path = [FindFiles.nameSpaceToName namespace] in
[path])
in
let opens_from_bsc_flags =
let bind f x = Option.bind x f in
match Json.get "bsc-flags" config |> bind Json.array with
| Some l ->
List.fold_left
(fun opens item ->
match item |> Json.string with
| None -> opens
| Some s -> (
let parts = String.split_on_char ' ' s in
match parts with
| "-open" :: name :: _ ->
let path = name |> String.split_on_char '.' in
path :: opens
| _ -> opens))
[] l
| None -> []
Lazy.from_fun (fun () ->
let bind f x = Option.bind x f in
match Json.get "bsc-flags" config |> bind Json.array with
| Some l ->
List.fold_left
(fun opens item ->
match item |> Json.string with
| None -> opens
| Some s -> (
let parts = String.split_on_char ' ' s in
match parts with
| "-open" :: name :: _ ->
let path = name |> String.split_on_char '.' in
path :: opens
| _ -> opens))
[] l
| None -> [])
in
let opens =
[(if uncurried then "PervasivesU" else "Pervasives"); "JsxModules"]
:: opens_from_namespace
|> List.rev_append opens_from_bsc_flags
|> List.map (fun path -> path @ ["place holder"])
Lazy.from_fun (fun () ->
[
(if uncurried then "PervasivesU" else "Pervasives");
"JsxModules";
]
:: Lazy.force opens_from_namespace
|> List.rev_append (Lazy.force opens_from_bsc_flags)
|> List.map (fun path -> path @ ["place holder"]))
in
{
genericJsxModule;
Expand All @@ -150,59 +171,60 @@ let newBsPackage ~rootPath =
opens;
namespace;
builtInCompletionModules =
(if
opens_from_bsc_flags
|> List.find_opt (fun opn ->
match opn with
| ["RescriptCore"] -> true
| _ -> false)
|> Option.is_some
then
{
arrayModulePath = ["Array"];
optionModulePath = ["Option"];
stringModulePath = ["String"];
intModulePath = ["Int"];
floatModulePath = ["Float"];
promiseModulePath = ["Promise"];
listModulePath = ["List"];
resultModulePath = ["Result"];
exnModulePath = ["Exn"];
regexpModulePath = ["RegExp"];
}
else if
opens_from_bsc_flags
|> List.find_opt (fun opn ->
match opn with
| ["Belt"] -> true
| _ -> false)
|> Option.is_some
then
{
arrayModulePath = ["Array"];
optionModulePath = ["Option"];
stringModulePath = ["Js"; "String2"];
intModulePath = ["Int"];
floatModulePath = ["Float"];
promiseModulePath = ["Js"; "Promise"];
listModulePath = ["List"];
resultModulePath = ["Result"];
exnModulePath = ["Js"; "Exn"];
regexpModulePath = ["Js"; "Re"];
}
else
{
arrayModulePath = ["Js"; "Array2"];
optionModulePath = ["Belt"; "Option"];
stringModulePath = ["Js"; "String2"];
intModulePath = ["Belt"; "Int"];
floatModulePath = ["Belt"; "Float"];
promiseModulePath = ["Js"; "Promise"];
listModulePath = ["Belt"; "List"];
resultModulePath = ["Belt"; "Result"];
exnModulePath = ["Js"; "Exn"];
regexpModulePath = ["Js"; "Re"];
});
Lazy.from_fun (fun () ->
if
opens_from_bsc_flags |> Lazy.force
|> List.find_opt (fun opn ->
match opn with
| ["RescriptCore"] -> true
| _ -> false)
|> Option.is_some
then
{
arrayModulePath = ["Array"];
optionModulePath = ["Option"];
stringModulePath = ["String"];
intModulePath = ["Int"];
floatModulePath = ["Float"];
promiseModulePath = ["Promise"];
listModulePath = ["List"];
resultModulePath = ["Result"];
exnModulePath = ["Exn"];
regexpModulePath = ["RegExp"];
}
else if
opens_from_bsc_flags |> Lazy.force
|> List.find_opt (fun opn ->
match opn with
| ["Belt"] -> true
| _ -> false)
|> Option.is_some
then
{
arrayModulePath = ["Array"];
optionModulePath = ["Option"];
stringModulePath = ["Js"; "String2"];
intModulePath = ["Int"];
floatModulePath = ["Float"];
promiseModulePath = ["Js"; "Promise"];
listModulePath = ["List"];
resultModulePath = ["Result"];
exnModulePath = ["Js"; "Exn"];
regexpModulePath = ["Js"; "Re"];
}
else
{
arrayModulePath = ["Js"; "Array2"];
optionModulePath = ["Belt"; "Option"];
stringModulePath = ["Js"; "String2"];
intModulePath = ["Belt"; "Int"];
floatModulePath = ["Belt"; "Float"];
promiseModulePath = ["Js"; "Promise"];
listModulePath = ["Belt"; "List"];
resultModulePath = ["Belt"; "Result"];
exnModulePath = ["Js"; "Exn"];
regexpModulePath = ["Js"; "Re"];
});
uncurried;
}))
| None -> None
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/ProcessCmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ let fileForCmt ~moduleName ~cmt ~uri =
Some file)

let fileForModule moduleName ~package =
match Hashtbl.find_opt package.pathsForModule moduleName with
match Hashtbl.find_opt (Lazy.force package.pathsForModule) moduleName with
| Some paths ->
let uri = getUri paths in
let cmt = getCmtPath ~uri paths in
Expand Down
16 changes: 10 additions & 6 deletions analysis/src/References.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ let getLocItem ~full ~pos ~debug =
| [
({locType = Typed (_, _, LocalReference _)} as li1);
({locType = Typed (_, _, GlobalReference ("Js_OO", ["unsafe_downgrade"], _))}
as li2);
as li2);
li3;
]
(* For older compiler 9.0 or earlier *)
Expand Down Expand Up @@ -211,7 +211,9 @@ let definedForLoc ~file ~package locKind =

(** Find alternative declaration: from res in case of interface, or from resi in case of implementation *)
let alternateDeclared ~(file : File.t) ~package (declared : _ Declared.t) tip =
match Hashtbl.find_opt package.pathsForModule file.moduleName with
match
Hashtbl.find_opt (Lazy.force package.pathsForModule) file.moduleName
with
| None -> None
| Some paths -> (
match paths with
Expand Down Expand Up @@ -362,7 +364,7 @@ let definitionForLocItem ~full:{file; package} locItem =
None
| TopLevelModule name -> (
maybeLog ("Toplevel " ^ name);
match Hashtbl.find_opt package.pathsForModule name with
match Hashtbl.find_opt (Lazy.force package.pathsForModule) name with
| None -> None
| Some paths ->
let uri = getUri paths in
Expand Down Expand Up @@ -486,7 +488,8 @@ let forLocalStamp ~full:{file; extra; package} stamp (tip : Tip.t) =
maybeLog ("Now checking path " ^ pathToString path);
let thisModuleName = file.moduleName in
let externals =
package.projectFiles |> FileSet.elements
Lazy.force package.projectFiles
|> FileSet.elements
|> List.filter (fun name -> name <> file.moduleName)
|> List.map (fun moduleName ->
Cmt.fullsFromModule ~package ~moduleName
Expand Down Expand Up @@ -521,7 +524,8 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem =
match locItem.locType with
| TopLevelModule moduleName ->
let otherModulesReferences =
package.projectFiles |> FileSet.elements
Lazy.force package.projectFiles
|> FileSet.elements
|> Utils.filterMap (fun name ->
match ProcessCmt.fileForModule ~package name with
| None -> None
Expand All @@ -539,7 +543,7 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem =
|> List.flatten
in
let targetModuleReferences =
match Hashtbl.find_opt package.pathsForModule moduleName with
match Hashtbl.find_opt (Lazy.force package.pathsForModule) moduleName with
| None -> []
| Some paths ->
let moduleSrcToRef src = {uri = Uri.fromPath src; locOpt = None} in
Expand Down
Loading

0 comments on commit bc71f76

Please sign in to comment.