فهرست منبع

[display] generalize fileContents behavior to other json rpc display calls

Rudy Ges 1 سال پیش
والد
کامیت
19cc8a50d3

+ 2 - 2
src/compiler/displayProcessing.ml

@@ -144,8 +144,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

+ 1 - 1
src/compiler/server.ml

@@ -47,7 +47,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

+ 2 - 2
src/context/common.ml

@@ -401,7 +401,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;
@@ -877,7 +877,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;

+ 46 - 57
src/context/display/displayJson.ml

@@ -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 = {
@@ -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;

+ 5 - 5
src/typing/typeloadParse.ml

@@ -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

+ 27 - 0
tests/server/src/cases/ServerTests.hx

@@ -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() {