Skip to content

Commit

Permalink
[display] generalize removable code as replaceable code
Browse files Browse the repository at this point in the history
see #7282
  • Loading branch information
Simn committed Nov 22, 2023
1 parent 2fbb4bf commit 3aeb26b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
23 changes: 14 additions & 9 deletions src/context/display/diagnostics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -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 = [];
Expand Down
12 changes: 8 additions & 4 deletions src/context/display/diagnosticsPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 [
Expand Down
11 changes: 9 additions & 2 deletions src/core/displayTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -345,4 +352,4 @@ type display_exception_kind =
| DisplayPositions of pos list
| DisplayFields of fields_result
| DisplayPackage of string list
| DisplayNoResult
| DisplayNoResult
4 changes: 2 additions & 2 deletions src/core/globals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ module MessageKind = struct
| DKUnusedImport
| DKUnresolvedIdentifier
| DKCompilerMessage
| DKRemovableCode
| DKReplacableCode
| DKParserError
| DKDeprecationWarning
| DKInactiveBlock
Expand All @@ -203,7 +203,7 @@ module MessageKind = struct
| DKUnusedImport -> 0
| DKUnresolvedIdentifier -> 1
| DKCompilerMessage -> 2
| DKRemovableCode -> 3
| DKReplacableCode -> 3
| DKParserError -> 4
| DKDeprecationWarning -> 5
| DKInactiveBlock -> 6
Expand Down
8 changes: 7 additions & 1 deletion std/haxe/display/Diagnostic.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down

0 comments on commit 3aeb26b

Please sign in to comment.