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

Always output valid json, even in case of internal error. #32

Merged
merged 1 commit into from
Nov 24, 2020
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
34 changes: 21 additions & 13 deletions src/rescript-editor-support/EditorSupportCommands.re
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let dumpLocations = (state, ~package, ~file, ~extra, ~selectPos, uri) => {
})
|> l;

print_endline(Json.stringify(locationsInfo));
Json.stringify(locationsInfo);
};

let dump = files => {
Expand Down Expand Up @@ -129,11 +129,15 @@ let dump = files => {
};
let filePath = maybeConcat(Unix.getcwd(), filePath);
let uri = Utils.toUri(filePath);
switch (State.getFullFromCmt(uri, state)) {
| Error(message) => print_endline(message)
| Ok((package, {file, extra})) =>
dumpLocations(state, ~package, ~file, ~extra, ~selectPos, uri)
};
let result =
switch (State.getFullFromCmt(uri, state)) {
| Error(message) =>
prerr_endline(message);
"[]";
| Ok((package, {file, extra})) =>
dumpLocations(state, ~package, ~file, ~extra, ~selectPos, uri)
};
print_endline(result);
});
};

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

print_endline(Json.stringify(completions));
Json.stringify(completions);
};

let complete = (~pathWithPos, ~currentFile) => {
Expand All @@ -242,12 +246,16 @@ let complete = (~pathWithPos, ~currentFile) => {
let pos = (line |> int_of_string, char |> int_of_string);
let filePath = maybeConcat(Unix.getcwd(), filePath);
let uri = Utils.toUri(filePath);
switch (State.getFullFromCmt(uri, state)) {
| Error(message) => print_endline(message)
| Ok((package, full)) =>
Hashtbl.replace(state.lastDefinitions, uri, full);
autocomplete(~currentFile, ~full, ~package, ~pos, ~state);
};
let result =
switch (State.getFullFromCmt(uri, state)) {
| Error(message) =>
prerr_endline(message);
"[]";
| Ok((package, full)) =>
Hashtbl.replace(state.lastDefinitions, uri, full);
autocomplete(~currentFile, ~full, ~package, ~pos, ~state);
};
print_endline(result);
| _ => ()
};
};
10 changes: 2 additions & 8 deletions src/rescript-editor-support/Pretty.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ let text ?len string =
single_line = true;
}

let print_indentation n =
print_char '\n';
for _ = 1 to n do
print_char ' ';
done

let rec flatten doc =
match doc.node with
| Append (a, b) -> append (flatten a) (flatten b)
Expand Down Expand Up @@ -118,8 +112,8 @@ let push offset node (stack: stack) =

let print
?width:(width=70)
?output:(output=print_string)
?indent:(indent=print_indentation)
~output
~indent
doc =
let rec loop currentIndent stack =
match stack with
Expand Down
4 changes: 2 additions & 2 deletions src/rescript-editor-support/Pretty.mli
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ val back : int -> string -> doc
"indent" function that defines how to print a newline and indent
to a desired level. The default arguments print to stdout. *)
val print : ?width: int ->
?output: (string -> unit) ->
?indent: (int -> unit) ->
output: (string -> unit) ->
indent: (int -> unit) ->
doc ->
unit
181 changes: 8 additions & 173 deletions src/rescript-editor-support/RescriptEditorSupport.re
Original file line number Diff line number Diff line change
Expand Up @@ -117,178 +117,35 @@ let tick = state => {
let orLog = (message, v) =>
switch (v) {
| None =>
print_endline(message);
prerr_endline(message);
None;
| Some(x) => Some(x)
};

let processFile = (~state, ~uri, ~quiet) => {
switch (Packages.getPackage(~reportDiagnostics=(_, _) => (), uri, state)) {
| Error(message) =>
print_endline(" Unable to get package: " ++ uri);
print_endline(message);
prerr_endline(" Unable to get package: " ++ uri);
prerr_endline(message);
None;
| Ok(package) =>
switch (State.getCompilationResult(uri, state, ~package)) {
| Error(message) =>
print_endline(" Invalid compilation result: " ++ message);
prerr_endline(" Invalid compilation result: " ++ message);
Some((package, None));
| Ok(Success(_message, contents)) =>
if (!quiet) {
print_endline(" Good: " ++ uri);
prerr_endline(" Good: " ++ uri);
};
Some((package, Some(contents)));
| Ok(TypeError(message, _) | SyntaxError(message, _, _)) =>
print_endline(" Error compiling: " ++ uri);
print_endline(message);
prerr_endline(" Error compiling: " ++ uri);
prerr_endline(message);
Some((package, None));
}
};
};

let singleDefinition = (~quiet, rootPath, filePath, line, col) => {
Log.log(
"# Reason Langauge Server - checking individual files to ensure they load & process correctly",
);
let rootPath =
rootPath == "." ? Unix.getcwd() : maybeConcat(Unix.getcwd(), rootPath);
let filePath = maybeConcat(Unix.getcwd(), filePath);
let state = {
...TopTypes.empty(),
rootPath,
rootUri: Utils.toUri(rootPath),
};

let uri = Utils.toUri(filePath);
switch (processFile(~state, ~uri, ~quiet)) {
| Some((package, Some({file, extra}))) =>
let _ = {
let%opt_consume (location, loc) =
References.locForPos(~extra, (line, col - 1))
|> orLog(
Printf.sprintf(
"Nothing definable found at %s:%d:%d",
filePath,
line,
col,
),
);
let%opt_consume (fname, dlocation) =
References.definitionForLoc(
~pathsForModule=package.pathsForModule,
~file,
~getUri=State.fileForUri(state, ~package),
~getModule=State.fileForModule(state, ~package),
loc,
)
|> orLog(
Printf.sprintf(
"Unable to resolve a definition for %s:%d:%d",
filePath,
location.loc_start.pos_lnum,
location.loc_start.pos_cnum - location.loc_start.pos_bol + 1,
),
);
let%opt_consume fname = Utils.parseUri(fname);
Printf.printf(
"Definition for %s:%d:%d found at %s:%d:%d\n",
filePath,
location.loc_start.pos_lnum,
location.loc_start.pos_cnum - location.loc_start.pos_bol + 1,
fname,
dlocation.loc_start.pos_lnum,
dlocation.loc_start.pos_cnum - dlocation.loc_start.pos_bol + 1,
);
};
print_endline(" Good: " ++ uri);
| _ => ()
};
};

let check = (~definitions, ~quiet, rootPath, files) => {
Log.log(
"# Reason Langauge Server - checking individual files to ensure they load & process correctly",
);
let rootPath =
rootPath == "." ? Unix.getcwd() : maybeConcat(Unix.getcwd(), rootPath);
let state = {
...TopTypes.empty(),
rootPath,
rootUri: Utils.toUri(rootPath),
};
files
|> List.iter(filePath => {
let filePath = maybeConcat(Unix.getcwd(), filePath);
let uri = Utils.toUri(filePath);
switch (processFile(~state, ~uri, ~quiet)) {
| Some((package, result)) =>
if (!definitions) {
Log.log(State.Show.state(state, package));
} else {
switch (result) {
| None => ()
| Some({file, extra}) =>
let missing = ref([]);
extra.locations
|> List.iter(((location, loc)) => {
switch (loc) {
| SharedTypes.Typed(_, LocalReference(tag, Type))
when tag <= 15 =>
()
| Typed(
_,
GlobalReference(_, _, Constructor("[]" | "::")),
) =>
()
| Typed(
_,
(LocalReference(_, _) | GlobalReference(_, _, _)) as t,
)
when !location.Location.loc_ghost =>
switch (
References.definitionForLoc(
~pathsForModule=package.pathsForModule,
~file,
~getUri=State.fileForUri(state, ~package),
~getModule=State.fileForModule(state, ~package),
loc,
)
) {
| None =>
// missing := 1 + missing^;
missing :=
[
Printf.sprintf(
" - \"%s:%d:%d\" : %s",
filePath,
location.loc_start.pos_lnum,
location.loc_start.pos_cnum
- location.loc_start.pos_bol
+ 1,
SharedTypes.locKindToString(t),
),
...missing^,
]
| Some(_defn) => ()
}
| _ => ()
}
});
if (missing^ != []) {
print_endline(filePath);
print_endline(
" > " ++ string_of_int(List.length(missing^)) ++ " missing",
);
missing^ |> List.iter(text => print_endline(text));
};
};
}
| _ => ()
};
});
Log.log("Ok");
};

let parseArgs = args => {
switch (args) {
| [] => assert(false)
Expand Down Expand Up @@ -331,7 +188,7 @@ The dump command can also omit `:line:column`, to show results for every positio
|};

let showHelp = () => {
print_endline(help);
prerr_endline(help);
};

let main = () => {
Expand All @@ -353,28 +210,6 @@ let main = () => {
);
Log.log("Finished");
Log.out^ |?< close_out;
| (opts, ["definition", rootPath, file, line, col]) =>
let line = int_of_string(line);
let col = int_of_string(col);
let quiet = hasOpts(opts, ["-q", "--quiet"]);
if (opts |> hasVerbose) {
Log.spamError := true;
References.debugReferences := true;
MerlinFile.debug := true;
};
singleDefinition(~quiet, rootPath, file, line, col);
| (opts, ["check", rootPath, ...files]) =>
let definitions = hasOpts(opts, ["-d", "--definitions"]);
let quiet = hasOpts(opts, ["-q", "--quiet"]);
if (opts |> hasVerbose) {
Log.spamError := true;
// if (!definitions) {
MerlinFile.debug := true;
// }
} else {
Log.spamError := false;
};
check(~definitions, ~quiet, rootPath, files);
| (_opts, ["dump", ...files]) => EditorSupportCommands.dump(files)
| (_opts, ["complete", pathWithPos, currentFile]) =>
EditorSupportCommands.complete(~pathWithPos, ~currentFile)
Expand Down
18 changes: 0 additions & 18 deletions src/rescript-editor-support/vendor/omd/omd_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,6 @@ struct
extraction a portion of another lexing tree. *)
let fix l =
let rec loop accu = function
(* code to generate what follows...
List.iter (fun e ->
Printf.printf "
| %s::%s::tl ->
if trackfix then eprintf \"%s 1\\n%!\";
loop accu (%ss 0::tl)
| %ss n::%s::tl ->
if trackfix then eprintf \"%s 2\\n%!\";
loop accu (%ss(n+1)::tl)
| %s::%ss n::tl ->
if trackfix then eprintf \"%s 3\\n%!\";
loop accu (%ss(n+1)::tl)
| %ss a::%ss b::tl ->
if trackfix then eprintf \"%s 4\\n%!\";
loop accu (%ss(a+b+2)::tl)"
e e e e e e e e e e e e e e e e)
["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"];
print_string "| x::tl -> loop (x::accu) tl\n| [] -> List.rev accu\n"; *)
| Ampersand::Ampersand::tl ->
if trackfix then eprintf "(OMD) Ampersand 1\n";
loop accu (Ampersands 0::tl)
Expand Down