Skip to content

Treat package opens the same way local opens are. #616

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 2 commits into from
Oct 29, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

## master

#### :bug: Bug Fix

- Fix issue where `-open Some.Path` in `"bsc-flags"` would sometimes be treated differently from `open Some.Path` locally in a file https://github.com/rescript-lang/rescript-vscode/pull/616

## v1.8.2

#### :rocket: New Feature
Expand Down
55 changes: 26 additions & 29 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ let showConstructor {Constructor.cname = {txt}; args; res} =
| Some typ -> "\n" ^ (typ |> Shared.typeToString)

(* TODO: local opens *)
let resolveOpens ~env ~previous opens ~package =
let resolveOpens ~env opens ~package =
List.fold_left
(fun previous path ->
(* Finding an open, first trying to find it in previoulsly resolved opens *)
Expand Down Expand Up @@ -540,7 +540,7 @@ let resolveOpens ~env ~previous opens ~package =
Log.log "Was local";
previous @ [env])
(* loop(previous) *)
previous opens
[] opens

let checkName name ~prefix ~exact =
if exact then name = prefix else Utils.startsWith name prefix
Expand Down Expand Up @@ -1319,11 +1319,6 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
| Some path -> Some (getModulePath path)
in
let lhsPath = fromType typ in
let removePackageOpens modulePath =
match modulePath with
| toplevel :: rest when package.opens |> List.mem toplevel -> rest
| _ -> modulePath
in
let rec removeRawOpen rawOpen modulePath =
match (rawOpen, modulePath) with
| [_], _ -> Some modulePath
Expand All @@ -1345,8 +1340,9 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
match modulePath with
| _ :: _ ->
let modulePathMinusOpens =
modulePath |> removePackageOpens |> removeRawOpens rawOpens
|> String.concat "."
modulePath
|> removeRawOpens package.opens
|> removeRawOpens rawOpens |> String.concat "."
in
let completionName name =
if modulePathMinusOpens = "" then name
Expand All @@ -1369,35 +1365,36 @@ let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
| None -> [])
| None -> [])

let getOpens ~rawOpens ~package ~env =
Log.log
("Raw ppens: "
^ string_of_int (List.length rawOpens)
^ " "
^ String.concat " ... " (rawOpens |> List.map pathToString));
let getOpens ~debug ~rawOpens ~package ~env =
if debug && rawOpens <> [] then
Printf.printf "%s\n"
("Raw opens: "
^ string_of_int (List.length rawOpens)
^ " "
^ String.concat " ... " (rawOpens |> List.map pathToString));
let packageOpens = package.opens in
Log.log ("Package opens " ^ String.concat " " packageOpens);
if debug && packageOpens <> [] then
Printf.printf "%s\n"
("Package opens "
^ String.concat " " (packageOpens |> List.map pathToString));
let resolvedOpens =
resolveOpens ~env
~previous:
(List.map QueryEnv.fromFile
(packageOpens |> Utils.filterMap (ProcessCmt.fileForModule ~package)))
(List.rev rawOpens) ~package
resolveOpens ~env (List.rev (packageOpens @ rawOpens)) ~package
in
Log.log
("Resolved opens "
^ string_of_int (List.length resolvedOpens)
^ " "
^ String.concat " "
(resolvedOpens
|> List.map (fun (e : QueryEnv.t) -> Uri.toString e.file.uri)));
if debug && resolvedOpens <> [] then
Printf.printf "%s\n"
("Resolved opens "
^ string_of_int (List.length resolvedOpens)
^ " "
^ String.concat " "
(resolvedOpens
|> List.map (fun (e : QueryEnv.t) -> Uri.toString e.file.uri)));
(* Last open takes priority *)
List.rev resolvedOpens

let processCompletable ~debug ~package ~scope ~env ~pos ~forHover
(completable : Completable.t) =
let rawOpens = Scope.getRawOpens scope in
let opens = getOpens ~rawOpens ~package ~env in
let opens = getOpens ~debug ~rawOpens ~package ~env in
let allFiles = FileSet.union package.projectFiles package.dependenciesFiles in
let findTypeOfValue path =
path
Expand Down
15 changes: 10 additions & 5 deletions analysis/src/Packages.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ let newBsPackage ~rootPath =
Log.log
("############ Namespaced as " ^ namespace ^ " at " ^ cmt);
Hashtbl.add pathsForModule namespace (Namespace {cmt});
[FindFiles.nameSpaceToName namespace]
let path = [FindFiles.nameSpaceToName namespace] in
[path]
in
Log.log
("Dependency dirs: "
Expand All @@ -69,16 +70,20 @@ let newBsPackage ~rootPath =
let parts = String.split_on_char ' ' s in
match parts with
| "-open" :: name :: _ ->
let names = name |> String.split_on_char '.' in
names @ opens
let path = name |> String.split_on_char '.' in
path :: opens
| _ -> opens))
[] l
| None -> []
in
let opens =
List.rev_append opens_from_bsc_flags opens_from_namespace
opens_from_namespace
|> List.rev_append opens_from_bsc_flags
|> List.map (fun path -> path @ ["place holder"])
in
Log.log ("Opens from bsconfig: " ^ (opens |> String.concat " "));
Log.log
("Opens from bsconfig: "
^ (opens |> List.map pathToString |> String.concat " "));
{
rootPath;
projectFiles =
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ type package = {
dependenciesFiles: FileSet.t;
pathsForModule: (file, paths) Hashtbl.t;
namespace: string option;
opens: string list;
opens: path list;
}

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