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

Commit 70f93d9

Browse files
committed
Encapsulate the uri type to make sure it's not mixed with file paths.
1 parent 6bdd10f commit 70f93d9

17 files changed

+156
-153
lines changed

src/rescript-editor-support/EditorSupportCommands.re

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
8484
"definition",
8585
J.o(
8686
uriIsCurrentFile
87-
? [range] : [("uri", Json.String(uri2)), range],
87+
? [range]
88+
: [("uri", Json.String(Uri2.toString(uri2))), range],
8889
),
8990
),
9091
],
@@ -121,13 +122,12 @@ let splitLineChar = pathWithPos => {
121122
let dump = files => {
122123
Shared.cacheTypeToString := true;
123124
let rootPath = Unix.getcwd();
124-
let emptyState = TopTypes.empty();
125-
let state = {...emptyState, rootUri: Utils.toUri(rootPath)};
125+
let state = TopTypes.empty(~rootUri=Uri2.fromPath(rootPath));
126126
files
127127
|> List.iter(pathWithPos => {
128128
let (filePath, selectPos) = pathWithPos |> splitLineChar;
129129
let filePath = Files.maybeConcat(Unix.getcwd(), filePath);
130-
let uri = Utils.toUri(filePath);
130+
let uri = Uri2.fromPath(filePath);
131131
let result =
132132
switch (State.getFullFromCmt(~state, ~uri)) {
133133
| Error(message) =>
@@ -155,12 +155,11 @@ let autocomplete = (~currentFile, ~full, ~package, ~pos, ~state) => {
155155

156156
let complete = (~pathWithPos, ~currentFile) => {
157157
let rootPath = Unix.getcwd();
158-
let emptyState = TopTypes.empty();
159-
let state = {...emptyState, rootUri: Utils.toUri(rootPath)};
158+
let state = TopTypes.empty(~rootUri=Uri2.fromPath(rootPath));
160159
switch (pathWithPos |> splitLineChar) {
161160
| (filePath, Some(pos)) =>
162161
let filePath = Files.maybeConcat(Unix.getcwd(), filePath);
163-
let uri = Utils.toUri(filePath);
162+
let uri = Uri2.fromPath(filePath);
164163
let result =
165164
switch (State.getFullFromCmt(~state, ~uri)) {
166165
| Error(message) =>

src/rescript-editor-support/Hover.re

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ let newHover = (~rootUri, ~file: SharedTypes.file, ~getModule, loc) => {
139139
let (typeString, docstring) = t |> fromType(~docstring=None);
140140
[typeString, docstring];
141141
| Some((docstring, {uri}, res)) =>
142-
let uri =
143-
Utils.startsWith(uri, rootUri)
144-
? "<root>" ++ Utils.sliceToEnd(uri, String.length(rootUri)) : uri;
142+
let pathFromRoot = uri |> Uri2.pathFromRoot(~rootUri);
145143

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

170-
parts @ [Some(uri)];
168+
parts @ [Some(pathFromRoot)];
171169
};
172170

173171
Some(String.concat("\n\n", parts |> Utils.filterMap(x => x)));

src/rescript-editor-support/MessageHandlers.re

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let handlers:
2727
Ok((
2828
state,
2929
Json.Object([
30-
("uri", Json.String(uri)),
30+
("uri", Json.String(Uri2.toString(uri))),
3131
("range", Protocol.rangeOfLoc(loc)),
3232
]),
3333
)),
@@ -164,7 +164,7 @@ let handlers:
164164
allReferences
165165
|> List.map(((fname, references)) =>
166166
(
167-
fname,
167+
fname |> Uri2.toString,
168168
J.l(
169169
references
170170
|> List.map(loc =>
@@ -195,6 +195,7 @@ let handlers:
195195
|> RJson.get("textDocument")
196196
|?> RJson.get("uri")
197197
|?> RJson.string;
198+
let%try uri = Uri2.parse(uri) |> RResult.orError("Not a uri");
198199

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

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

311313
let%try (file, _extra) = State.fileForUri(state, uri);
312314
open SharedTypes;

src/rescript-editor-support/NewCompletions.re

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ let getEnvWithOpens =
210210
let rec loop = opens =>
211211
switch (opens) {
212212
| [env, ...rest] =>
213-
Log.log("Looking for env in " ++ env.Query.file.uri);
213+
Log.log("Looking for env in " ++ Uri2.toString(env.Query.file.uri));
214214
switch (Query.resolvePath(~env, ~getModule, ~path)) {
215215
| Some(x) => Some(x)
216216
| None => loop(rest)
@@ -313,7 +313,7 @@ let localValueCompletions = (~pos, ~env: Query.queryEnv, suffix) => {
313313
};
314314

315315
let valueCompletions = (~env: Query.queryEnv, suffix) => {
316-
Log.log(" - Completing in " ++ env.file.uri);
316+
Log.log(" - Completing in " ++ Uri2.toString(env.file.uri));
317317
let results = [];
318318
let results =
319319
if (suffix == "" || isCapitalized(suffix)) {
@@ -450,7 +450,10 @@ let getItems =
450450
"Opens nows "
451451
++ string_of_int(List.length(opens))
452452
++ " "
453-
++ String.concat(" ", opens |> List.map(e => e.Query.file.uri)),
453+
++ String.concat(
454+
" ",
455+
opens |> List.map(e => Uri2.toString(e.Query.file.uri)),
456+
),
454457
);
455458

456459
switch (parts) {
@@ -488,7 +491,7 @@ let getItems =
488491
/* Log.log("Checking " ++ name); */
489492
Utils.startsWith(name, suffix) && !String.contains(name, '-')
490493
? Some((
491-
"wait for uri",
494+
env.file.uri,
492495
{...emptyDeclared(name), item: FileModule(name)},
493496
))
494497
: None
@@ -636,7 +639,7 @@ let computeCompletions = (~full, ~maybeText, ~package, ~pos, ~state) => {
636639
J.s(
637640
(docstring |? "No docs")
638641
++ "\n\n"
639-
++ uri
642+
++ Uri2.toString(uri)
640643
++ ":"
641644
++ string_of_int(pos_lnum),
642645
),

src/rescript-editor-support/NotificationHandlers.re

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ open TopTypes;
44
module J = JsonShort;
55

66
let getTextDocument = doc => {
7-
let%opt uri = Json.get("uri", doc) |?> Json.string;
7+
let%opt uri = Json.get("uri", doc) |?> Json.string |?> Uri2.parse;
88
let%opt text = Json.get("text", doc) |?> Json.string;
99
Some((uri, text));
1010
};
1111

12-
let watchedFileContentsMap = Hashtbl.create(100);
13-
1412
let reloadAllState = state => {
1513
Log.log("RELOADING ALL STATE");
16-
{...TopTypes.empty(), documentText: state.documentText};
14+
{
15+
...TopTypes.empty(~rootUri=state.rootUri),
16+
documentText: state.documentText,
17+
};
1718
};
1819

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

30-
let%try path = Utils.parseUri(uri) |> RResult.orError("Invalid uri");
31+
let path = Uri2.toPath(uri);
3132
if (FindFiles.isSourceFile(path)) {
3233
let%try package = Packages.getPackage(uri, state);
3334
/* let name = FindFiles.getName(path); */
@@ -71,6 +72,7 @@ let notificationHandlers:
7172
open InfixResult;
7273
let%try doc = params |> RJson.get("textDocument");
7374
let%try uri = RJson.get("uri", doc) |?> RJson.string;
75+
let%try uri = Uri2.parse(uri) |> RResult.orError("Not a uri");
7476
let%try changes = RJson.get("contentChanges", params) |?> RJson.array;
7577
let%try text =
7678
List.nth(changes, List.length(changes) - 1)
@@ -83,41 +85,8 @@ let notificationHandlers:
8385
),
8486
(
8587
"workspace/didChangeWatchedFiles",
86-
(state, params) => {
87-
Log.log("Got a watched file change");
88-
let%try changes = RJson.get("changes", params);
89-
let%try changes = RJson.array(changes);
90-
open InfixResult;
91-
let shouldReload =
92-
changes
93-
|> List.exists(change =>
94-
{
95-
let%try uri = RJson.get("uri", change) |?> RJson.string;
96-
let isRelevant = Utils.endsWith(uri, "/bsconfig.json");
97-
if (!isRelevant) {
98-
Ok(false);
99-
} else {
100-
let%try path =
101-
Utils.parseUri(uri) |> RResult.orError("Cannot parse URI");
102-
let%try contents = Files.readFileResult(path);
103-
if (!Hashtbl.mem(watchedFileContentsMap, uri)
104-
|| Hashtbl.find(watchedFileContentsMap, uri) == contents) {
105-
Ok(false);
106-
} else {
107-
Hashtbl.replace(watchedFileContentsMap, uri, contents);
108-
Log.log("Reloading because a file changed: " ++ uri);
109-
Ok(true);
110-
};
111-
};
112-
}
113-
|? false
114-
);
115-
116-
if (shouldReload) {
117-
Ok(reloadAllState(state));
118-
} else {
119-
Ok(state);
120-
};
88+
(state, _params) => {
89+
Ok(state);
12190
},
12291
),
12392
];

src/rescript-editor-support/Packages.re

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ let newBsPackage = rootPath => {
149149
};
150150
};
151151

152-
let findRoot = (uri, packagesByRoot) => {
153-
let%opt path = Utils.parseUri(uri);
152+
let findRoot = (~uri, packagesByRoot) => {
153+
let path = Uri2.toPath(uri);
154154
let rec loop = path =>
155155
if (path == "/") {
156156
None;
@@ -164,7 +164,7 @@ let findRoot = (uri, packagesByRoot) => {
164164
loop(Filename.dirname(path));
165165
};
166166

167-
let getPackage = (uri, state) =>
167+
let getPackage = (~uri, state) =>
168168
if (Hashtbl.mem(state.rootForUri, uri)) {
169169
Ok(
170170
Hashtbl.find(
@@ -174,7 +174,7 @@ let getPackage = (uri, state) =>
174174
);
175175
} else {
176176
let%try root =
177-
findRoot(uri, state.packagesByRoot)
177+
findRoot(~uri, state.packagesByRoot)
178178
|> RResult.orError("No root directory found");
179179
let%try package =
180180
switch (root) {

src/rescript-editor-support/ProcessCmt.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ and forStructure = (~env, items) => {
574574
let forCmt =
575575
(
576576
~moduleName,
577-
uri,
577+
~uri,
578578
processDoc,
579579
{cmt_modname, cmt_annots}: Cmt_format.cmt_infos,
580580
) =>

src/rescript-editor-support/Process_406.re

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
open SharedTypes;
22

3-
let fileForCmt = (~moduleName, cmt, uri, processDoc) => {
3+
let fileForCmt = (~moduleName, ~uri, cmt, processDoc) => {
44
let%try infos = Shared.tryReadCmt(cmt);
5-
Ok(ProcessCmt.forCmt(~moduleName, uri, processDoc, infos));
5+
Ok(ProcessCmt.forCmt(~moduleName, ~uri, processDoc, infos));
66
};
77

8-
let fullForCmt = (~moduleName, cmt, uri, processDoc) => {
8+
let fullForCmt = (~moduleName, ~uri, cmt, processDoc) => {
99
let%try infos = Shared.tryReadCmt(cmt);
10-
let file = ProcessCmt.forCmt(~moduleName, uri, processDoc, infos);
10+
let file = ProcessCmt.forCmt(~moduleName, ~uri, processDoc, infos);
1111
let extra = ProcessExtra.forCmt(~file, infos);
1212
Ok({file, extra});
1313
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let fileForCmt:
2-
(~moduleName: string, string, string, string => string) =>
2+
(~moduleName: string, ~uri: Uri2.t, string, string => string) =>
33
result(SharedTypes.file, string);
44

55
let fullForCmt:
6-
(~moduleName: string, string, string, string => string) =>
6+
(~moduleName: string, ~uri: Uri2.t, string, string => string) =>
77
result(SharedTypes.full, string);

src/rescript-editor-support/Protocol.re

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let rPositionParams = params => {
1111
open RResult.InfixResult;
1212
let%try uri =
1313
RJson.get("textDocument", params) |?> RJson.get("uri") |?> RJson.string;
14+
let%try uri = Uri2.parse(uri) |> RResult.orError("Not a uri");
1415
let%try pos = RJson.get("position", params) |?> rgetPosition;
1516
Ok((uri, pos));
1617
};
@@ -28,19 +29,17 @@ let rangeOfLoc = ({Location.loc_start, loc_end}) =>
2829
J.o([("start", posOfLexing(loc_start)), ("end", posOfLexing(loc_end))]);
2930

3031
let locationOfLoc =
31-
(~fname=?, {Location.loc_start: {Lexing.pos_fname}} as loc) =>
32+
(~fname=?, {Location.loc_start: {Lexing.pos_fname}} as loc) => {
33+
let uri =
34+
switch (fname) {
35+
| Some(x) => x
36+
| None => Uri2.fromPath(pos_fname)
37+
};
3238
J.o([
3339
("range", rangeOfLoc(loc)),
34-
(
35-
"uri",
36-
J.s(
37-
switch (fname) {
38-
| Some(x) => x
39-
| None => Utils.toUri(pos_fname)
40-
},
41-
),
42-
),
40+
("uri", J.s(Uri2.toString(uri))),
4341
]);
42+
};
4443

4544
let locationContains = ({Location.loc_start, loc_end}, pos) =>
4645
Utils.tupleOfLexing(loc_start) <= pos

0 commit comments

Comments
 (0)