diff --git a/src/context/display/diagnostics.ml b/src/context/display/diagnostics.ml index d1fbbcf10b2..5a01397dda9 100644 --- a/src/context/display/diagnostics.ml +++ b/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 = []; diff --git a/src/context/display/diagnosticsPrinter.ml b/src/context/display/diagnosticsPrinter.ml index f0933a85227..82bb755366c 100644 --- a/src/context/display/diagnosticsPrinter.ml +++ b/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 [ diff --git a/src/core/displayTypes.ml b/src/core/displayTypes.ml index 919572b279d..b9bacf7f75e 100644 --- a/src/core/displayTypes.ml +++ b/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 \ No newline at end of file diff --git a/src/core/globals.ml b/src/core/globals.ml index 4292add776d..be184e3ecff 100644 --- a/src/core/globals.ml +++ b/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 diff --git a/std/haxe/display/Diagnostic.hx b/std/haxe/display/Diagnostic.hx index 4c6369ecada..461a8c1f508 100644 --- a/std/haxe/display/Diagnostic.hx +++ b/std/haxe/display/Diagnostic.hx @@ -46,11 +46,17 @@ typedef MissingFieldDiagnostics = { var entries:Array; } +typedef ReplacableCode = { + var description:String; + var range:Range; + var ?newCode:String; +} + enum abstract DiagnosticKind(Int) from Int to Int { final DKUnusedImport:DiagnosticKind; final DKUnresolvedIdentifier:DiagnosticKind>; final DKCompilerError:DiagnosticKind; - final DKRemovableCode:DiagnosticKind<{description:String, range:Range}>; + final ReplacableCode:DiagnosticKind; final DKParserError:DiagnosticKind; final DeprecationWarning:DiagnosticKind; final InactiveBlock:DiagnosticKind;