Repository navigation
Expand file tree
/
Copy pathserverFormat.ml
More file actions
64 lines (60 loc) · 2.15 KB
/
Copy pathserverFormat.ml
File metadata and controls
64 lines (60 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "hack" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)
(* TODO t14922604: Further improve error handling *)
let call_external_formatter path content from to_ =
let args = [|"hackfmt"; "--range"; string_of_int from; string_of_int to_|] in
let lines = ref [] in
let reader timeout ic oc =
output_string oc content;
close_out oc;
try while true do lines := Timeout.input_line ~timeout ic :: !lines done
with End_of_file -> lines := "" :: !lines;
in
try
Timeout.read_process
~timeout:2
~on_timeout:(fun _ -> Hh_logger.log "Format timed out")
~reader
path
args;
Format_hack.Success (String.concat "\n" @@ List.rev !lines)
with Timeout.Timeout -> begin
Hh_logger.log "Formatter timeout";
Format_hack.Internal_error
end
let go genv content from to_ =
if genv.ServerEnv.local_config.ServerLocalConfig.use_hackfmt then begin
Hh_logger.log "--range %d %d" from to_;
let path = match ServerConfig.formatter_override genv.ServerEnv.config with
| None -> Path.make "/usr/local/bin/hackfmt"
| Some p -> p
in
let path = Path.to_string path in
if Sys.file_exists path
then call_external_formatter path content from to_
else begin
Hh_logger.log "Formatter not found";
Format_hack.Internal_error
end
end else
let modes = [Some FileInfo.Mstrict; Some FileInfo.Mpartial] in
Format_hack.region modes Path.dummy_path from to_ content
let format_result_to_ide_response x =
let open Format_hack in
match x with
| Disabled_mode -> Result.Error ("Not a Hack file")
| Parsing_error _ -> Result.Error ("File has parse errors")
| Internal_error -> Result.Error ("Formatter internal error")
| Success s -> Result.Ok s
let go_ide genv content range =
let open Ide_api_types in
let from, to_ = File_content.get_offsets content (range.st, range.ed) in
go genv content from to_ |>
format_result_to_ide_response