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

Commit 633c7e3

Browse files
committed
Always output valid json, even in case of internal error.
Fixes #19 Suppress all printing to stdout. Print `[]` in case of internal error. For examople, if the project was never built, or a file cannot be built for the first time because of syntax errors. In that case, there's an internal error as the `.cmt` cannot be found.
1 parent c2f1ade commit 633c7e3

File tree

5 files changed

+33
-214
lines changed

5 files changed

+33
-214
lines changed

src/rescript-editor-support/EditorSupportCommands.re

+21-13
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
101101
})
102102
|> l;
103103

104-
print_endline(Json.stringify(locationsInfo));
104+
Json.stringify(locationsInfo);
105105
};
106106

107107
let dump = files => {
@@ -129,11 +129,15 @@ let dump = files => {
129129
};
130130
let filePath = maybeConcat(Unix.getcwd(), filePath);
131131
let uri = Utils.toUri(filePath);
132-
switch (State.getFullFromCmt(uri, state)) {
133-
| Error(message) => print_endline(message)
134-
| Ok((package, {file, extra})) =>
135-
dumpLocations(state, ~package, ~file, ~extra, ~selectPos, uri)
136-
};
132+
let result =
133+
switch (State.getFullFromCmt(uri, state)) {
134+
| Error(message) =>
135+
prerr_endline(message);
136+
"[]";
137+
| Ok((package, {file, extra})) =>
138+
dumpLocations(state, ~package, ~file, ~extra, ~selectPos, uri)
139+
};
140+
print_endline(result);
137141
});
138142
};
139143

@@ -222,7 +226,7 @@ let autocomplete = (~currentFile, ~full, ~package, ~pos, ~state) => {
222226
})
223227
|> l;
224228

225-
print_endline(Json.stringify(completions));
229+
Json.stringify(completions);
226230
};
227231

228232
let complete = (~pathWithPos, ~currentFile) => {
@@ -242,12 +246,16 @@ let complete = (~pathWithPos, ~currentFile) => {
242246
let pos = (line |> int_of_string, char |> int_of_string);
243247
let filePath = maybeConcat(Unix.getcwd(), filePath);
244248
let uri = Utils.toUri(filePath);
245-
switch (State.getFullFromCmt(uri, state)) {
246-
| Error(message) => print_endline(message)
247-
| Ok((package, full)) =>
248-
Hashtbl.replace(state.lastDefinitions, uri, full);
249-
autocomplete(~currentFile, ~full, ~package, ~pos, ~state);
250-
};
249+
let result =
250+
switch (State.getFullFromCmt(uri, state)) {
251+
| Error(message) =>
252+
prerr_endline(message);
253+
"[]";
254+
| Ok((package, full)) =>
255+
Hashtbl.replace(state.lastDefinitions, uri, full);
256+
autocomplete(~currentFile, ~full, ~package, ~pos, ~state);
257+
};
258+
print_endline(result);
251259
| _ => ()
252260
};
253261
};

src/rescript-editor-support/Pretty.ml

+2-8
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ let text ?len string =
7979
single_line = true;
8080
}
8181

82-
let print_indentation n =
83-
print_char '\n';
84-
for _ = 1 to n do
85-
print_char ' ';
86-
done
87-
8882
let rec flatten doc =
8983
match doc.node with
9084
| Append (a, b) -> append (flatten a) (flatten b)
@@ -118,8 +112,8 @@ let push offset node (stack: stack) =
118112

119113
let print
120114
?width:(width=70)
121-
?output:(output=print_string)
122-
?indent:(indent=print_indentation)
115+
~output
116+
~indent
123117
doc =
124118
let rec loop currentIndent stack =
125119
match stack with

src/rescript-editor-support/Pretty.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ val back : int -> string -> doc
5151
"indent" function that defines how to print a newline and indent
5252
to a desired level. The default arguments print to stdout. *)
5353
val print : ?width: int ->
54-
?output: (string -> unit) ->
55-
?indent: (int -> unit) ->
54+
output: (string -> unit) ->
55+
indent: (int -> unit) ->
5656
doc ->
5757
unit

src/rescript-editor-support/RescriptEditorSupport.re

+8-173
Original file line numberDiff line numberDiff line change
@@ -117,178 +117,35 @@ let tick = state => {
117117
let orLog = (message, v) =>
118118
switch (v) {
119119
| None =>
120-
print_endline(message);
120+
prerr_endline(message);
121121
None;
122122
| Some(x) => Some(x)
123123
};
124124

125125
let processFile = (~state, ~uri, ~quiet) => {
126126
switch (Packages.getPackage(~reportDiagnostics=(_, _) => (), uri, state)) {
127127
| Error(message) =>
128-
print_endline(" Unable to get package: " ++ uri);
129-
print_endline(message);
128+
prerr_endline(" Unable to get package: " ++ uri);
129+
prerr_endline(message);
130130
None;
131131
| Ok(package) =>
132132
switch (State.getCompilationResult(uri, state, ~package)) {
133133
| Error(message) =>
134-
print_endline(" Invalid compilation result: " ++ message);
134+
prerr_endline(" Invalid compilation result: " ++ message);
135135
Some((package, None));
136136
| Ok(Success(_message, contents)) =>
137137
if (!quiet) {
138-
print_endline(" Good: " ++ uri);
138+
prerr_endline(" Good: " ++ uri);
139139
};
140140
Some((package, Some(contents)));
141141
| Ok(TypeError(message, _) | SyntaxError(message, _, _)) =>
142-
print_endline(" Error compiling: " ++ uri);
143-
print_endline(message);
142+
prerr_endline(" Error compiling: " ++ uri);
143+
prerr_endline(message);
144144
Some((package, None));
145145
}
146146
};
147147
};
148148

149-
let singleDefinition = (~quiet, rootPath, filePath, line, col) => {
150-
Log.log(
151-
"# Reason Langauge Server - checking individual files to ensure they load & process correctly",
152-
);
153-
let rootPath =
154-
rootPath == "." ? Unix.getcwd() : maybeConcat(Unix.getcwd(), rootPath);
155-
let filePath = maybeConcat(Unix.getcwd(), filePath);
156-
let state = {
157-
...TopTypes.empty(),
158-
rootPath,
159-
rootUri: Utils.toUri(rootPath),
160-
};
161-
162-
let uri = Utils.toUri(filePath);
163-
switch (processFile(~state, ~uri, ~quiet)) {
164-
| Some((package, Some({file, extra}))) =>
165-
let _ = {
166-
let%opt_consume (location, loc) =
167-
References.locForPos(~extra, (line, col - 1))
168-
|> orLog(
169-
Printf.sprintf(
170-
"Nothing definable found at %s:%d:%d",
171-
filePath,
172-
line,
173-
col,
174-
),
175-
);
176-
let%opt_consume (fname, dlocation) =
177-
References.definitionForLoc(
178-
~pathsForModule=package.pathsForModule,
179-
~file,
180-
~getUri=State.fileForUri(state, ~package),
181-
~getModule=State.fileForModule(state, ~package),
182-
loc,
183-
)
184-
|> orLog(
185-
Printf.sprintf(
186-
"Unable to resolve a definition for %s:%d:%d",
187-
filePath,
188-
location.loc_start.pos_lnum,
189-
location.loc_start.pos_cnum - location.loc_start.pos_bol + 1,
190-
),
191-
);
192-
let%opt_consume fname = Utils.parseUri(fname);
193-
Printf.printf(
194-
"Definition for %s:%d:%d found at %s:%d:%d\n",
195-
filePath,
196-
location.loc_start.pos_lnum,
197-
location.loc_start.pos_cnum - location.loc_start.pos_bol + 1,
198-
fname,
199-
dlocation.loc_start.pos_lnum,
200-
dlocation.loc_start.pos_cnum - dlocation.loc_start.pos_bol + 1,
201-
);
202-
};
203-
print_endline(" Good: " ++ uri);
204-
| _ => ()
205-
};
206-
};
207-
208-
let check = (~definitions, ~quiet, rootPath, files) => {
209-
Log.log(
210-
"# Reason Langauge Server - checking individual files to ensure they load & process correctly",
211-
);
212-
let rootPath =
213-
rootPath == "." ? Unix.getcwd() : maybeConcat(Unix.getcwd(), rootPath);
214-
let state = {
215-
...TopTypes.empty(),
216-
rootPath,
217-
rootUri: Utils.toUri(rootPath),
218-
};
219-
files
220-
|> List.iter(filePath => {
221-
let filePath = maybeConcat(Unix.getcwd(), filePath);
222-
let uri = Utils.toUri(filePath);
223-
switch (processFile(~state, ~uri, ~quiet)) {
224-
| Some((package, result)) =>
225-
if (!definitions) {
226-
Log.log(State.Show.state(state, package));
227-
} else {
228-
switch (result) {
229-
| None => ()
230-
| Some({file, extra}) =>
231-
let missing = ref([]);
232-
extra.locations
233-
|> List.iter(((location, loc)) => {
234-
switch (loc) {
235-
| SharedTypes.Typed(_, LocalReference(tag, Type))
236-
when tag <= 15 =>
237-
()
238-
| Typed(
239-
_,
240-
GlobalReference(_, _, Constructor("[]" | "::")),
241-
) =>
242-
()
243-
| Typed(
244-
_,
245-
(LocalReference(_, _) | GlobalReference(_, _, _)) as t,
246-
)
247-
when !location.Location.loc_ghost =>
248-
switch (
249-
References.definitionForLoc(
250-
~pathsForModule=package.pathsForModule,
251-
~file,
252-
~getUri=State.fileForUri(state, ~package),
253-
~getModule=State.fileForModule(state, ~package),
254-
loc,
255-
)
256-
) {
257-
| None =>
258-
// missing := 1 + missing^;
259-
missing :=
260-
[
261-
Printf.sprintf(
262-
" - \"%s:%d:%d\" : %s",
263-
filePath,
264-
location.loc_start.pos_lnum,
265-
location.loc_start.pos_cnum
266-
- location.loc_start.pos_bol
267-
+ 1,
268-
SharedTypes.locKindToString(t),
269-
),
270-
...missing^,
271-
]
272-
| Some(_defn) => ()
273-
}
274-
| _ => ()
275-
}
276-
});
277-
if (missing^ != []) {
278-
print_endline(filePath);
279-
print_endline(
280-
" > " ++ string_of_int(List.length(missing^)) ++ " missing",
281-
);
282-
missing^ |> List.iter(text => print_endline(text));
283-
};
284-
};
285-
}
286-
| _ => ()
287-
};
288-
});
289-
Log.log("Ok");
290-
};
291-
292149
let parseArgs = args => {
293150
switch (args) {
294151
| [] => assert(false)
@@ -331,7 +188,7 @@ The dump command can also omit `:line:column`, to show results for every positio
331188
|};
332189

333190
let showHelp = () => {
334-
print_endline(help);
191+
prerr_endline(help);
335192
};
336193

337194
let main = () => {
@@ -353,28 +210,6 @@ let main = () => {
353210
);
354211
Log.log("Finished");
355212
Log.out^ |?< close_out;
356-
| (opts, ["definition", rootPath, file, line, col]) =>
357-
let line = int_of_string(line);
358-
let col = int_of_string(col);
359-
let quiet = hasOpts(opts, ["-q", "--quiet"]);
360-
if (opts |> hasVerbose) {
361-
Log.spamError := true;
362-
References.debugReferences := true;
363-
MerlinFile.debug := true;
364-
};
365-
singleDefinition(~quiet, rootPath, file, line, col);
366-
| (opts, ["check", rootPath, ...files]) =>
367-
let definitions = hasOpts(opts, ["-d", "--definitions"]);
368-
let quiet = hasOpts(opts, ["-q", "--quiet"]);
369-
if (opts |> hasVerbose) {
370-
Log.spamError := true;
371-
// if (!definitions) {
372-
MerlinFile.debug := true;
373-
// }
374-
} else {
375-
Log.spamError := false;
376-
};
377-
check(~definitions, ~quiet, rootPath, files);
378213
| (_opts, ["dump", ...files]) => EditorSupportCommands.dump(files)
379214
| (_opts, ["complete", pathWithPos, currentFile]) =>
380215
EditorSupportCommands.complete(~pathWithPos, ~currentFile)

src/rescript-editor-support/vendor/omd/omd_parser.ml

-18
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,6 @@ struct
154154
extraction a portion of another lexing tree. *)
155155
let fix l =
156156
let rec loop accu = function
157-
(* code to generate what follows...
158-
List.iter (fun e ->
159-
Printf.printf "
160-
| %s::%s::tl ->
161-
if trackfix then eprintf \"%s 1\\n%!\";
162-
loop accu (%ss 0::tl)
163-
| %ss n::%s::tl ->
164-
if trackfix then eprintf \"%s 2\\n%!\";
165-
loop accu (%ss(n+1)::tl)
166-
| %s::%ss n::tl ->
167-
if trackfix then eprintf \"%s 3\\n%!\";
168-
loop accu (%ss(n+1)::tl)
169-
| %ss a::%ss b::tl ->
170-
if trackfix then eprintf \"%s 4\\n%!\";
171-
loop accu (%ss(a+b+2)::tl)"
172-
e e e e e e e e e e e e e e e e)
173-
["Ampersand"; "At"; "Backquote"; "Backslash"; "Bar"; "Caret"; "Cbrace"; "Colon"; "Comma"; "Cparenthesis"; "Cbracket"; "Dollar"; "Dot"; "Doublequote"; "Exclamation"; "Equal"; "Greaterthan"; "Hash"; "Lessthan"; "Minus"; "Newline"; "Obrace"; "Oparenthesis"; "Obracket"; "Percent"; "Plus"; "Question"; "Quote"; "Semicolon"; "Slash"; "Space"; "Star"; "Tab"; "Tilde"; "Underscore"];
174-
print_string "| x::tl -> loop (x::accu) tl\n| [] -> List.rev accu\n"; *)
175157
| Ampersand::Ampersand::tl ->
176158
if trackfix then eprintf "(OMD) Ampersand 1\n";
177159
loop accu (Ampersands 0::tl)

0 commit comments

Comments
 (0)