Skip to content

refactor and remove allFiles where not necessary to pass explicitly #751

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

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
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
127 changes: 59 additions & 68 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,10 @@ let getComplementaryCompletionsForTypedValue ~opens ~allFiles ~scope ~env prefix
in
localCompletionsWithOpens @ fileModules

let getCompletionsForPath ~debug ~package ~opens ~allFiles ~pos ~exact ~scope
let getCompletionsForPath ~debug ~package ~opens ~full ~pos ~exact ~scope
~completionContext ~env path =
if debug then Printf.printf "Path %s\n" (path |> String.concat ".");
let allFiles = allFilesInPackage full.package in
match path with
| [] -> []
| [prefix] ->
Expand Down Expand Up @@ -621,18 +622,17 @@ let completionsGetCompletionType ~full = function
| {Completion.kind = ExtractedType (typ, _); env} :: _ -> Some (typ, env)
| _ -> None

let rec completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~scope = function
let rec completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos ~scope
= function
| {Completion.kind = Value typ; env} :: _
| {Completion.kind = ObjLabel typ; env} :: _
| {Completion.kind = Field ({typ}, _); env} :: _ ->
Some (TypeExpr typ, env)
| {Completion.kind = FollowContextPath ctxPath; env} :: _ ->
ctxPath
|> getCompletionsForContextPath ~debug ~full ~env ~exact:true ~opens
~rawOpens ~allFiles ~pos ~scope
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~scope
~rawOpens ~pos ~scope
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos ~scope
| {Completion.kind = Type typ; env} :: _ -> (
match TypeUtils.extractTypeFromResolvedType typ ~env ~full with
| None -> None
Expand All @@ -642,21 +642,20 @@ let rec completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~allFiles
| _ -> None

and completionsGetTypeEnv2 ~debug (completions : Completion.t list) ~full ~opens
~rawOpens ~allFiles ~pos ~scope =
~rawOpens ~pos ~scope =
match completions with
| {Completion.kind = Value typ; env} :: _ -> Some (typ, env)
| {Completion.kind = ObjLabel typ; env} :: _ -> Some (typ, env)
| {Completion.kind = Field ({typ}, _); env} :: _ -> Some (typ, env)
| {Completion.kind = FollowContextPath ctxPath; env} :: _ ->
ctxPath
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~env ~exact:true ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~scope
| _ -> None

and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~env ~exact ~scope ?(mode = Regular) contextPath =
and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
~scope ?(mode = Regular) contextPath =
if debug then
Printf.printf "ContextPath %s\n"
(Completable.contextPathToString contextPath);
Expand Down Expand Up @@ -702,8 +701,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
| Regular -> (
match
cp
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetCompletionType ~full
with
| None -> []
Expand All @@ -725,8 +724,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
| CPOption cp -> (
match
cp
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetCompletionType ~full
with
| None -> []
Expand All @@ -738,15 +737,14 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
])
| CPId (path, completionContext) ->
path
|> getCompletionsForPath ~debug ~package ~opens ~allFiles ~pos ~exact
|> getCompletionsForPath ~debug ~package ~opens ~full ~pos ~exact
~completionContext ~env ~scope
| CPApply (cp, labels) -> (
match
cp
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos ~scope
with
| Some ((TypeExpr typ | ExtractedType (Tfunction {typ})), env) -> (
let rec reconstructFunctionType args tRet =
Expand Down Expand Up @@ -785,19 +783,19 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
| CPField (CPId (path, Module), fieldName) ->
(* M.field *)
path @ [fieldName]
|> getCompletionsForPath ~debug ~package ~opens ~allFiles ~pos ~exact
|> getCompletionsForPath ~debug ~package ~opens ~full ~pos ~exact
~completionContext:Field ~env ~scope
| CPField (cp, fieldName) -> (
let completionsForCtxPath =
cp
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
in
let extracted =
match
completionsForCtxPath
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~scope
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos
~scope
with
| Some (TypeExpr typ, env) -> (
match typ |> TypeUtils.extractRecordType ~env ~package with
Expand Down Expand Up @@ -829,10 +827,9 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
(* TODO: Also needs to support ExtractedType *)
match
cp
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~scope
with
| Some (typ, env) -> (
match typ |> TypeUtils.extractObjectType ~env ~package with
Expand All @@ -857,10 +854,9 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
| CPPipe {contextPath = cp; id = funNamePrefix; lhsLoc; inJsx} -> (
match
cp
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope ~mode:Pipe
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope ~mode:Pipe
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~scope
with
| None -> []
| Some (typ, envFromCompletionItem) -> (
Expand Down Expand Up @@ -960,7 +956,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
let completions =
completionPath @ [funNamePrefix]
|> getCompletionsForPath ~debug ~completionContext:Value ~exact:false
~package ~opens ~allFiles ~pos ~env ~scope
~package ~opens ~full ~pos ~env ~scope
in
let completions =
completions
Expand Down Expand Up @@ -1007,8 +1003,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
ctxPaths
|> List.map (fun contextPath ->
contextPath
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens
~allFiles ~pos ~env ~exact:true ~scope)
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos
~env ~exact:true ~scope)
|> List.filter_map (fun completionItems ->
match completionItems with
| {Completion.kind = Value typ} :: _ -> Some typ
Expand All @@ -1024,9 +1020,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
let findTypeOfValue path =
path
|> getCompletionsForPath ~debug ~completionContext:Value ~exact:true
~package ~opens ~allFiles ~pos ~env ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~scope
~package ~opens ~full ~pos ~env ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~scope
in
let lowercaseComponent =
match pathToComponent with
Expand All @@ -1039,7 +1034,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
match
path
|> getCompletionsForPath ~debug ~completionContext:Type ~exact:true
~package ~opens ~allFiles ~pos ~env ~scope
~package ~opens ~full ~pos ~env ~scope
with
| {kind = Type {kind = Abstract (Some (p, _))}} :: _ ->
(* This case happens when what we're looking for is a type alias.
Expand Down Expand Up @@ -1070,10 +1065,10 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
let labels, env =
match
functionContextPath
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos
~scope
with
| Some ((TypeExpr typ | ExtractedType (Tfunction {typ})), env) ->
(typ |> TypeUtils.getArgs ~full ~env, env)
Expand Down Expand Up @@ -1108,10 +1103,9 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
| CPatternPath {rootCtxPath; nested} -> (
match
rootCtxPath
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetCompletionType2 ~debug ~full ~opens ~rawOpens ~pos ~scope
with
| Some (typ, env) -> (
match typ |> TypeUtils.resolveNestedPatternPath ~env ~full ~nested with
Expand Down Expand Up @@ -1416,20 +1410,19 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
let package = full.package in
let rawOpens = Scope.getRawOpens scope in
let opens = getOpens ~debug ~rawOpens ~package ~env in
let allFiles = FileSet.union package.projectFiles package.dependenciesFiles in
let allFiles = allFilesInPackage package in
let findTypeOfValue path =
path
|> getCompletionsForPath ~debug ~completionContext:Value ~exact:true
~package ~opens ~allFiles ~pos ~env ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~scope
~package ~opens ~full ~pos ~env ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~scope
in
match completable with
| Cnone -> []
| Cpath contextPath ->
contextPath
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~env ~exact:forHover ~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:forHover ~scope
| Cjsx ([id], prefix, identsSeen) when String.uncapitalize_ascii id = id ->
(* Lowercase JSX tag means builtin *)
let mkLabel (name, typString) =
Expand Down Expand Up @@ -1486,10 +1479,9 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
let labels =
match
cp
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~scope
with
| Some (typ, _env) ->
if debug then
Expand Down Expand Up @@ -1523,10 +1515,9 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
in
match
contextPath
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~allFiles ~pos
~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetTypeEnv2 ~debug ~full ~opens ~rawOpens ~pos ~scope
with
| Some (typ, env) -> (
match
Expand Down Expand Up @@ -1557,8 +1548,8 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
in
match
contextPath
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:true ~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:true ~scope
|> completionsGetCompletionType ~full
with
| None -> regularCompletions
Expand Down Expand Up @@ -1637,8 +1628,8 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover completable =
in
let completionsForContextPath =
contextPath
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~allFiles
~pos ~env ~exact:forHover ~scope
|> getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env
~exact:forHover ~scope
in
completionsForContextPath
|> List.map (fun (c : Completion.t) ->
Expand Down
7 changes: 2 additions & 5 deletions analysis/src/Hover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
| None -> None
| Some (completions, ({file; package} as full), scope) -> (
let rawOpens = Scope.getRawOpens scope in
let allFiles =
FileSet.union package.projectFiles package.dependenciesFiles
in
match completions with
| {kind = Label typString; docstring} :: _ ->
let parts =
Expand All @@ -141,7 +138,7 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
let opens = CompletionBackEnd.getOpens ~debug ~rawOpens ~package ~env in
match
CompletionBackEnd.completionsGetTypeEnv2 ~debug ~full ~rawOpens ~opens
~allFiles ~pos ~scope completions
~pos ~scope completions
with
| Some (typ, _env) ->
let typeString =
Expand All @@ -154,7 +151,7 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
let opens = CompletionBackEnd.getOpens ~debug ~rawOpens ~package ~env in
match
CompletionBackEnd.completionsGetTypeEnv2 ~debug ~full ~rawOpens ~opens
~allFiles ~pos ~scope completions
~pos ~scope completions
with
| Some (typ, _env) ->
let typeString =
Expand Down
3 changes: 3 additions & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ type package = {
opens: path list;
}

let allFilesInPackage package =
FileSet.union package.projectFiles package.dependenciesFiles

type full = {extra: extra; file: File.t; package: package}

let initExtra () =
Expand Down