Explorar o código

[display] generalize removable code as replaceable code

see #7282
Simon Krajewski hai 1 ano
pai
achega
3aeb26b9a1

+ 14 - 9
src/context/display/diagnostics.ml

@@ -4,8 +4,13 @@ open Type
 open Common
 open DisplayTypes
 
-let add_removable_code ctx s p prange =
-	ctx.removable_code <- (s,p,prange) :: ctx.removable_code
+let add_replaceable_code ctx reason replacement display_range replace_range =
+	ctx.replaceable_code <- {
+		reason = reason;
+		replacement = replacement;
+		display_range = display_range;
+		replace_range = replace_range;
+	} :: ctx.replaceable_code
 
 let error_in_diagnostics_run com p =
 	let b = DiagnosticsPrinter.is_diagnostics_file com (com.file_keys#get p.pfile) in
@@ -18,23 +23,23 @@ let find_unused_variables com e =
 	let rec loop e = match e.eexpr with
 		| TVar({v_kind = VUser origin} as v,eo) when v.v_name <> "_" && not (has_var_flag v VUsedByTyper) ->
 			Hashtbl.add pmin_map e.epos.pmin v;
-			let p = match eo with
+			let p,replacement = match eo with
 			| Some e1 when origin <> TVOPatternVariable ->
 				loop e1;
-				{ e.epos with pmax = e1.epos.pmin }
+				{ e.epos with pmax = e1.epos.pmin },""
 			| _ ->
-				e.epos
+				e.epos,"_"
 			in
-			Hashtbl.replace vars v.v_id (v,p);
+			Hashtbl.replace vars v.v_id (v,p,replacement);
 		| TLocal ({v_kind = VUser _} as v) ->
 			Hashtbl.remove vars v.v_id;
 		| _ ->
 			Type.iter loop e
 	in
 	loop e;
-	Hashtbl.iter (fun _ (v,p) ->
+	Hashtbl.iter (fun _ (v,p,replacement) ->
 		let p = match (Hashtbl.find_all pmin_map p.pmin) with [_] -> p | _ -> null_pos in
-		add_removable_code com "Unused variable" v.v_pos p
+		add_replaceable_code com "Unused variable" replacement v.v_pos p
 	) vars
 
 let check_other_things com e =
@@ -135,7 +140,7 @@ let collect_diagnostics dctx com =
 
 let prepare com =
 	let dctx = {
-		removable_code = [];
+		replaceable_code = [];
 		import_positions = PMap.empty;
 		dead_blocks = Hashtbl.create 0;
 		diagnostics_messages = [];

+ 8 - 4
src/context/display/diagnosticsPrinter.ml

@@ -64,7 +64,7 @@ let json_of_diagnostics com dctx =
 	let add dk p sev code args =
 		let append = match dk with
 			| DKUnusedImport
-			| DKRemovableCode
+			| DKReplacableCode
 			| DKDeprecationWarning
 			| DKInactiveBlock ->
 				false
@@ -193,9 +193,13 @@ let json_of_diagnostics com dctx =
 	PMap.iter (fun p r ->
 		if not !r then add DKUnusedImport p MessageSeverity.Warning None (JArray [])
 	) dctx.import_positions;
-	List.iter (fun (s,p,prange) ->
-		add DKRemovableCode p MessageSeverity.Warning None (JObject ["description",JString s;"range",if prange = null_pos then JNull else Genjson.generate_pos_as_range prange])
-	) dctx.removable_code;
+	List.iter (fun rc ->
+		add DKReplacableCode rc.display_range MessageSeverity.Warning None (JObject [
+			"description",JString rc.reason;
+			"newCode",JString rc.replacement;
+			"range",if rc.replace_range = null_pos then JNull else Genjson.generate_pos_as_range rc.replace_range
+		])
+	) dctx.replaceable_code;
 	Hashtbl.iter (fun file ranges ->
 		List.iter (fun (p,e) ->
 			let jo = JObject [

+ 9 - 2
src/core/displayTypes.ml

@@ -328,8 +328,15 @@ and missing_fields_diagnostics = {
 and module_diagnostics =
 	| MissingFields of missing_fields_diagnostics
 
+type replaceable_code = {
+	reason : string;
+	replacement : string;
+	display_range : pos;
+	replace_range : pos;
+}
+
 type diagnostics_context = {
-	mutable removable_code : (string * pos * pos) list;
+	mutable replaceable_code : replaceable_code list;
 	mutable import_positions : (pos,bool ref) PMap.t;
 	mutable dead_blocks : (Path.UniqueKey.t,(pos * expr) list) Hashtbl.t;
 	mutable unresolved_identifiers : (string * pos * (string * CompletionItem.t * int) list) list;
@@ -345,4 +352,4 @@ type display_exception_kind =
 	| DisplayPositions of pos list
 	| DisplayFields of fields_result
 	| DisplayPackage of string list
-	| DisplayNoResult
+	| DisplayNoResult

+ 2 - 2
src/core/globals.ml

@@ -193,7 +193,7 @@ module MessageKind = struct
 		| DKUnusedImport
 		| DKUnresolvedIdentifier
 		| DKCompilerMessage
-		| DKRemovableCode
+		| DKReplacableCode
 		| DKParserError
 		| DKDeprecationWarning
 		| DKInactiveBlock
@@ -203,7 +203,7 @@ module MessageKind = struct
 		| DKUnusedImport -> 0
 		| DKUnresolvedIdentifier -> 1
 		| DKCompilerMessage -> 2
-		| DKRemovableCode -> 3
+		| DKReplacableCode -> 3
 		| DKParserError -> 4
 		| DKDeprecationWarning -> 5
 		| DKInactiveBlock -> 6

+ 7 - 1
std/haxe/display/Diagnostic.hx

@@ -46,11 +46,17 @@ typedef MissingFieldDiagnostics = {
 	var entries:Array<MissingFieldDiagnostic>;
 }
 
+typedef ReplacableCode = {
+	var description:String;
+	var range:Range;
+	var ?newCode:String;
+}
+
 enum abstract DiagnosticKind<T>(Int) from Int to Int {
 	final DKUnusedImport:DiagnosticKind<Void>;
 	final DKUnresolvedIdentifier:DiagnosticKind<Array<{kind:UnresolvedIdentifierSuggestion, name:String}>>;
 	final DKCompilerError:DiagnosticKind<String>;
-	final DKRemovableCode:DiagnosticKind<{description:String, range:Range}>;
+	final ReplacableCode:DiagnosticKind<ReplacableCode>;
 	final DKParserError:DiagnosticKind<String>;
 	final DeprecationWarning:DiagnosticKind<String>;
 	final InactiveBlock:DiagnosticKind<Void>;