Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Encapsulate the uri type to make sure it's not mixed with file paths. #58

Merged
merged 1 commit into from
Feb 3, 2021
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
13 changes: 6 additions & 7 deletions src/rescript-editor-support/EditorSupportCommands.re
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
"definition",
J.o(
uriIsCurrentFile
? [range] : [("uri", Json.String(uri2)), range],
? [range]
: [("uri", Json.String(Uri2.toString(uri2))), range],
),
),
],
Expand Down Expand Up @@ -121,13 +122,12 @@ let splitLineChar = pathWithPos => {
let dump = files => {
Shared.cacheTypeToString := true;
let rootPath = Unix.getcwd();
let emptyState = TopTypes.empty();
let state = {...emptyState, rootUri: Utils.toUri(rootPath)};
let state = TopTypes.empty(~rootUri=Uri2.fromPath(rootPath));
files
|> List.iter(pathWithPos => {
let (filePath, selectPos) = pathWithPos |> splitLineChar;
let filePath = Files.maybeConcat(Unix.getcwd(), filePath);
let uri = Utils.toUri(filePath);
let uri = Uri2.fromPath(filePath);
let result =
switch (State.getFullFromCmt(~state, ~uri)) {
| Error(message) =>
Expand Down Expand Up @@ -155,12 +155,11 @@ let autocomplete = (~currentFile, ~full, ~package, ~pos, ~state) => {

let complete = (~pathWithPos, ~currentFile) => {
let rootPath = Unix.getcwd();
let emptyState = TopTypes.empty();
let state = {...emptyState, rootUri: Utils.toUri(rootPath)};
let state = TopTypes.empty(~rootUri=Uri2.fromPath(rootPath));
switch (pathWithPos |> splitLineChar) {
| (filePath, Some(pos)) =>
let filePath = Files.maybeConcat(Unix.getcwd(), filePath);
let uri = Utils.toUri(filePath);
let uri = Uri2.fromPath(filePath);
let result =
switch (State.getFullFromCmt(~state, ~uri)) {
| Error(message) =>
Expand Down
6 changes: 2 additions & 4 deletions src/rescript-editor-support/Hover.re
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
let (typeString, docstring) = t |> fromType(~docstring=None);
[typeString, docstring];
| Some((docstring, {uri}, res)) =>
let uri =
Utils.startsWith(uri, rootUri)
? "<root>" ++ Utils.sliceToEnd(uri, String.length(rootUri)) : uri;
let pathFromRoot = uri |> Uri2.pathFromRoot(~rootUri);

let parts =
switch (res) {
Expand All @@ -167,7 +165,7 @@ let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
[typeString, docstring];
};

parts @ [Some(uri)];
parts @ [Some(pathFromRoot)];
};

Some(String.concat("\n\n", parts |> Utils.filterMap(x => x)));
Expand Down
6 changes: 4 additions & 2 deletions src/rescript-editor-support/MessageHandlers.re
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let handlers:
Ok((
state,
Json.Object([
("uri", Json.String(uri)),
("uri", Json.String(Uri2.toString(uri))),
("range", Protocol.rangeOfLoc(loc)),
]),
)),
Expand Down Expand Up @@ -164,7 +164,7 @@ let handlers:
allReferences
|> List.map(((fname, references)) =>
(
fname,
fname |> Uri2.toString,
J.l(
references
|> List.map(loc =>
Expand Down Expand Up @@ -195,6 +195,7 @@ let handlers:
|> RJson.get("textDocument")
|?> RJson.get("uri")
|?> RJson.string;
let%try uri = Uri2.parse(uri) |> RResult.orError("Not a uri");

/* Log.log("<< codleens me please"); */

Expand Down Expand Up @@ -307,6 +308,7 @@ let handlers:
|> RJson.get("textDocument")
|?> RJson.get("uri")
|?> RJson.string;
let%try uri = Uri2.parse(uri) |> RResult.orError("Not a uri");

let%try (file, _extra) = State.fileForUri(state, uri);
open SharedTypes;
Expand Down
13 changes: 8 additions & 5 deletions src/rescript-editor-support/NewCompletions.re
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ let getEnvWithOpens =
let rec loop = opens =>
switch (opens) {
| [env, ...rest] =>
Log.log("Looking for env in " ++ env.Query.file.uri);
Log.log("Looking for env in " ++ Uri2.toString(env.Query.file.uri));
switch (Query.resolvePath(~env, ~getModule, ~path)) {
| Some(x) => Some(x)
| None => loop(rest)
Expand Down Expand Up @@ -313,7 +313,7 @@ let localValueCompletions = (~pos, ~env: Query.queryEnv, suffix) => {
};

let valueCompletions = (~env: Query.queryEnv, suffix) => {
Log.log(" - Completing in " ++ env.file.uri);
Log.log(" - Completing in " ++ Uri2.toString(env.file.uri));
let results = [];
let results =
if (suffix == "" || isCapitalized(suffix)) {
Expand Down Expand Up @@ -450,7 +450,10 @@ let getItems =
"Opens nows "
++ string_of_int(List.length(opens))
++ " "
++ String.concat(" ", opens |> List.map(e => e.Query.file.uri)),
++ String.concat(
" ",
opens |> List.map(e => Uri2.toString(e.Query.file.uri)),
),
);

switch (parts) {
Expand Down Expand Up @@ -488,7 +491,7 @@ let getItems =
/* Log.log("Checking " ++ name); */
Utils.startsWith(name, suffix) && !String.contains(name, '-')
? Some((
"wait for uri",
env.file.uri,
{...emptyDeclared(name), item: FileModule(name)},
))
: None
Expand Down Expand Up @@ -636,7 +639,7 @@ let computeCompletions = (~full, ~maybeText, ~package, ~pos, ~state) => {
J.s(
(docstring |? "No docs")
++ "\n\n"
++ uri
++ Uri2.toString(uri)
++ ":"
++ string_of_int(pos_lnum),
),
Expand Down
49 changes: 9 additions & 40 deletions src/rescript-editor-support/NotificationHandlers.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ open TopTypes;
module J = JsonShort;

let getTextDocument = doc => {
let%opt uri = Json.get("uri", doc) |?> Json.string;
let%opt uri = Json.get("uri", doc) |?> Json.string |?> Uri2.parse;
let%opt text = Json.get("text", doc) |?> Json.string;
Some((uri, text));
};

let watchedFileContentsMap = Hashtbl.create(100);

let reloadAllState = state => {
Log.log("RELOADING ALL STATE");
{...TopTypes.empty(), documentText: state.documentText};
{
...TopTypes.empty(~rootUri=state.rootUri),
documentText: state.documentText,
};
};

let notificationHandlers:
Expand All @@ -27,7 +28,7 @@ let notificationHandlers:
|> RResult.orError("Invalid params");
Hashtbl.replace(state.documentText, uri, text);

let%try path = Utils.parseUri(uri) |> RResult.orError("Invalid uri");
let path = Uri2.toPath(uri);
if (FindFiles.isSourceFile(path)) {
let%try package = Packages.getPackage(uri, state);
/* let name = FindFiles.getName(path); */
Expand Down Expand Up @@ -71,6 +72,7 @@ let notificationHandlers:
open InfixResult;
let%try doc = params |> RJson.get("textDocument");
let%try uri = RJson.get("uri", doc) |?> RJson.string;
let%try uri = Uri2.parse(uri) |> RResult.orError("Not a uri");
let%try changes = RJson.get("contentChanges", params) |?> RJson.array;
let%try text =
List.nth(changes, List.length(changes) - 1)
Expand All @@ -83,41 +85,8 @@ let notificationHandlers:
),
(
"workspace/didChangeWatchedFiles",
(state, params) => {
Log.log("Got a watched file change");
let%try changes = RJson.get("changes", params);
let%try changes = RJson.array(changes);
open InfixResult;
let shouldReload =
changes
|> List.exists(change =>
{
let%try uri = RJson.get("uri", change) |?> RJson.string;
let isRelevant = Utils.endsWith(uri, "/bsconfig.json");
if (!isRelevant) {
Ok(false);
} else {
let%try path =
Utils.parseUri(uri) |> RResult.orError("Cannot parse URI");
let%try contents = Files.readFileResult(path);
if (!Hashtbl.mem(watchedFileContentsMap, uri)
|| Hashtbl.find(watchedFileContentsMap, uri) == contents) {
Ok(false);
} else {
Hashtbl.replace(watchedFileContentsMap, uri, contents);
Log.log("Reloading because a file changed: " ++ uri);
Ok(true);
};
};
}
|? false
);

if (shouldReload) {
Ok(reloadAllState(state));
} else {
Ok(state);
};
(state, _params) => {
Ok(state);
},
),
];
8 changes: 4 additions & 4 deletions src/rescript-editor-support/Packages.re
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ let newBsPackage = rootPath => {
};
};

let findRoot = (uri, packagesByRoot) => {
let%opt path = Utils.parseUri(uri);
let findRoot = (~uri, packagesByRoot) => {
let path = Uri2.toPath(uri);
let rec loop = path =>
if (path == "/") {
None;
Expand All @@ -164,7 +164,7 @@ let findRoot = (uri, packagesByRoot) => {
loop(Filename.dirname(path));
};

let getPackage = (uri, state) =>
let getPackage = (~uri, state) =>
if (Hashtbl.mem(state.rootForUri, uri)) {
Ok(
Hashtbl.find(
Expand All @@ -174,7 +174,7 @@ let getPackage = (uri, state) =>
);
} else {
let%try root =
findRoot(uri, state.packagesByRoot)
findRoot(~uri, state.packagesByRoot)
|> RResult.orError("No root directory found");
let%try package =
switch (root) {
Expand Down
2 changes: 1 addition & 1 deletion src/rescript-editor-support/ProcessCmt.re
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ and forStructure = (~env, items) => {
let forCmt =
(
~moduleName,
uri,
~uri,
processDoc,
{cmt_modname, cmt_annots}: Cmt_format.cmt_infos,
) =>
Expand Down
8 changes: 4 additions & 4 deletions src/rescript-editor-support/Process_406.re
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
open SharedTypes;

let fileForCmt = (~moduleName, cmt, uri, processDoc) => {
let fileForCmt = (~moduleName, ~uri, cmt, processDoc) => {
let%try infos = Shared.tryReadCmt(cmt);
Ok(ProcessCmt.forCmt(~moduleName, uri, processDoc, infos));
Ok(ProcessCmt.forCmt(~moduleName, ~uri, processDoc, infos));
};

let fullForCmt = (~moduleName, cmt, uri, processDoc) => {
let fullForCmt = (~moduleName, ~uri, cmt, processDoc) => {
let%try infos = Shared.tryReadCmt(cmt);
let file = ProcessCmt.forCmt(~moduleName, uri, processDoc, infos);
let file = ProcessCmt.forCmt(~moduleName, ~uri, processDoc, infos);
let extra = ProcessExtra.forCmt(~file, infos);
Ok({file, extra});
};
Expand Down
4 changes: 2 additions & 2 deletions src/rescript-editor-support/Process_406.rei
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let fileForCmt:
(~moduleName: string, string, string, string => string) =>
(~moduleName: string, ~uri: Uri2.t, string, string => string) =>
result(SharedTypes.file, string);

let fullForCmt:
(~moduleName: string, string, string, string => string) =>
(~moduleName: string, ~uri: Uri2.t, string, string => string) =>
result(SharedTypes.full, string);
19 changes: 9 additions & 10 deletions src/rescript-editor-support/Protocol.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let rPositionParams = params => {
open RResult.InfixResult;
let%try uri =
RJson.get("textDocument", params) |?> RJson.get("uri") |?> RJson.string;
let%try uri = Uri2.parse(uri) |> RResult.orError("Not a uri");
let%try pos = RJson.get("position", params) |?> rgetPosition;
Ok((uri, pos));
};
Expand All @@ -28,19 +29,17 @@ let rangeOfLoc = ({Location.loc_start, loc_end}) =>
J.o([("start", posOfLexing(loc_start)), ("end", posOfLexing(loc_end))]);

let locationOfLoc =
(~fname=?, {Location.loc_start: {Lexing.pos_fname}} as loc) =>
(~fname=?, {Location.loc_start: {Lexing.pos_fname}} as loc) => {
let uri =
switch (fname) {
| Some(x) => x
| None => Uri2.fromPath(pos_fname)
};
J.o([
("range", rangeOfLoc(loc)),
(
"uri",
J.s(
switch (fname) {
| Some(x) => x
| None => Utils.toUri(pos_fname)
},
),
),
("uri", J.s(Uri2.toString(uri))),
]);
};

let locationContains = ({Location.loc_start, loc_end}, pos) =>
Utils.tupleOfLexing(loc_start) <= pos
Expand Down
Loading