Skip to content

Commit

Permalink
[display] generalize fileContents behavior to other json rpc display …
Browse files Browse the repository at this point in the history
…calls
  • Loading branch information
kLabz committed Nov 26, 2023
1 parent 65c6479 commit ad14130
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 67 deletions.
4 changes: 2 additions & 2 deletions src/compiler/displayProcessing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ let process_display_file com actx =
actx.classes <- [];
com.main_class <- None;
begin match com.file_contents with
| Some [_, Some input] ->
com.file_contents <- None;
| [_, Some input] ->
com.file_contents <- [];
DPKInput input
| _ ->
DPKNone
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let parse_file cs com file p =
and fkey = com.file_keys#get file in
let is_display_file = DisplayPosition.display_position#is_in_file (com.file_keys#get ffile) in
match is_display_file, !current_stdin with
| true, Some stdin when (com.file_contents <> None || Common.defined com Define.DisplayStdin) ->
| true, Some stdin when (com.file_contents <> [] || Common.defined com Define.DisplayStdin) ->
TypeloadParse.parse_file_from_string com file p stdin
| _ ->
let ftime = file_time ffile in
Expand Down
4 changes: 2 additions & 2 deletions src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ type context = {
display_information : display_information;
file_lookup_cache : (string,string option) lookup;
file_keys : file_keys;
mutable file_contents : (Path.UniqueKey.t * string option) list option;
mutable file_contents : (Path.UniqueKey.t * string option) list;
readdir_cache : (string * string,(string array) option) lookup;
parser_cache : (string,(type_def * pos) list) lookup;
module_to_file : (path,string) lookup;
Expand Down Expand Up @@ -876,7 +876,7 @@ let create compilation_step cs version args =
};
file_lookup_cache = new hashtbl_lookup;
file_keys = new file_keys;
file_contents = None;
file_contents = [];
readdir_cache = new hashtbl_lookup;
module_to_file = new hashtbl_lookup;
stored_typed_exprs = new hashtbl_lookup;
Expand Down
103 changes: 46 additions & 57 deletions src/context/display/displayJson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,49 @@ class display_handler (jsonrpc : jsonrpc_handler) com (cs : CompilationCache.t)
let file = jsonrpc#get_string_param "file" in
Path.get_full_path file
) file_input_marker in
let pos = if requires_offset then jsonrpc#get_int_param "offset" else (-1) in
com.file_contents <- jsonrpc#get_opt_param (fun () ->
let contents = jsonrpc#get_opt_param (fun () ->
let s = jsonrpc#get_string_param "contents" in
let file_unique = com.file_keys#get file in
Some [file_unique, Some s]
) None;
Some s
) None in

let pos = if requires_offset then jsonrpc#get_int_param "offset" else (-1) in
Parser.was_auto_triggered := was_auto_triggered;
DisplayPosition.display_position#set {
pfile = file;
pmin = pos;
pmax = pos;
}

if file <> file_input_marker then begin
let file_unique = com.file_keys#get file in

DisplayPosition.display_position#set {
pfile = file;
pmin = pos;
pmax = pos;
};

com.file_contents <- [file_unique, contents];
end else begin
let file_contents = jsonrpc#get_opt_param (fun () ->
jsonrpc#get_opt_param (fun () -> jsonrpc#get_array_param "fileContents") []
) [] in

let file_contents = List.map (fun fc -> match fc with
| JObject fl ->
let file = jsonrpc#get_string_field "fileContents" "file" fl in
let file = Path.get_full_path file in
let file_unique = com.file_keys#get file in
let contents = jsonrpc#get_opt_param (fun () ->
let s = jsonrpc#get_string_field "fileContents" "contents" fl in
Some s
) None in
(file_unique, contents)
| _ -> invalid_arg "fileContents"
) file_contents in

let files = (List.map (fun (k, _) -> k) file_contents) in
com.file_contents <- file_contents;

match files with
| [] | [_] -> DisplayPosition.display_position#set { pfile = file; pmin = pos; pmax = pos; };
| _ -> DisplayPosition.display_position#set_files files;
end
end

type handler_context = {
Expand Down Expand Up @@ -126,56 +157,14 @@ let handler =
hctx.display#enable_display DMDefinition;
);
"display/diagnostics", (fun hctx ->
hctx.display#set_display_file false false;
hctx.display#enable_display DMNone;
hctx.com.report_mode <- RMDiagnostics (List.map (fun (f,_) -> f) hctx.com.file_contents);

let file = hctx.jsonrpc#get_opt_param (fun () ->
let file = hctx.jsonrpc#get_string_param "file" in
Path.get_full_path file
) file_input_marker in

if file <> file_input_marker then begin
let file_unique = hctx.com.file_keys#get file in

let contents = hctx.jsonrpc#get_opt_param (fun () ->
let s = hctx.jsonrpc#get_string_param "contents" in
Some s
) None in

DisplayPosition.display_position#set {
pfile = file;
pmin = -1;
pmax = -1;
};

hctx.com.file_contents <- Some [file_unique, contents];
hctx.com.report_mode <- RMDiagnostics [file_unique];
(match hctx.com.file_contents with
| [file, None] ->
hctx.com.display <- { hctx.com.display with dms_display_file_policy = DFPAlso; dms_per_file = true; dms_populate_cache = !ServerConfig.populate_cache_from_display};
end else begin
let file_contents = hctx.jsonrpc#get_opt_param (fun () ->
hctx.jsonrpc#get_opt_param (fun () -> hctx.jsonrpc#get_array_param "fileContents") []
) [] in

if (List.length file_contents) = 0 then begin
hctx.com.report_mode <- RMDiagnostics []
end else
let file_contents = List.map (fun fc -> match fc with
| JObject fl ->
let file = hctx.jsonrpc#get_string_field "fileContents" "file" fl in
let file = Path.get_full_path file in
let file_unique = hctx.com.file_keys#get file in
let contents = hctx.jsonrpc#get_opt_param (fun () ->
let s = hctx.jsonrpc#get_string_field "fileContents" "contents" fl in
Some s
) None in
(file_unique, contents)
| _ -> invalid_arg "fileContents"
) file_contents in

let files = (List.map (fun (k, _) -> k) file_contents) in
DisplayPosition.display_position#set_files files;
hctx.com.file_contents <- Some file_contents;
hctx.com.report_mode <- RMDiagnostics files
end
| _ -> ());
);
"display/implementation", (fun hctx ->
hctx.display#set_display_file false true;
Expand Down
10 changes: 5 additions & 5 deletions src/typing/typeloadParse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ let parse_file_from_string com file p string =
let parse_file com file p =
let file_key = com.file_keys#get file in
let contents = match com.file_contents with
| Some files ->
(try List.assoc file_key files with Not_found -> None)
| None when (Common.defined com Define.DisplayStdin) && DisplayPosition.display_position#is_in_file file_key ->
| [] when (Common.defined com Define.DisplayStdin) && DisplayPosition.display_position#is_in_file file_key ->
let s = Std.input_all stdin in
close_in stdin;
com.file_contents <- Some [file_key, Some s];
com.file_contents <- [file_key, Some s];
Some s
| None -> None
| [] -> None
| files ->
(try List.assoc file_key files with Not_found -> None)
in

match contents with
Expand Down
27 changes: 27 additions & 0 deletions tests/server/src/cases/ServerTests.hx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,33 @@ class ServerTests extends TestCase {
Assert.equals("Unused variable", (cast arg).description);
}
});

// Currently, haxe compilation server will have this content anyway
// because of diagnostics with file contents, but that behavior may not
// be obvious in tests
vfs.putContent("Other.hx", getTemplate("issues/Issue9134/Other2.hx"));
runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Other.hx")});

// Running project wide diagnostics; checks here aren't great since
// results will depend on haxe std which may change without updating
// this test everytime..
runHaxeJsonCb(args, DisplayMethods.Diagnostics, {}, res -> {
var hasMain = false;
var hasOther = false;

for (result in res) {
var file = result.file.toString();
if (StringTools.endsWith(file, "Main.hx")) hasMain = true;
else if (StringTools.endsWith(file, "Other.hx")) hasOther = true;
else continue;

var arg = result.diagnostics[0].args;
Assert.equals("Unused variable", (cast arg).description);
}

Assert.isTrue(hasMain);
Assert.isTrue(hasOther);
});
}

function testDiagnosticsRecache() {
Expand Down

0 comments on commit ad14130

Please sign in to comment.