Skip to content

Commit 1f5df25

Browse files
committed
Keep information on whether a value definition is type annotated.
1 parent 477a6e0 commit 1f5df25

File tree

5 files changed

+49
-30
lines changed

5 files changed

+49
-30
lines changed

analysis/src/Hover.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ let newHover ~full:{file; package} locItem =
4141
| TypeDefinition (name, decl, _stamp) ->
4242
let typeDef = Shared.declToString name decl in
4343
Some (codeBlock typeDef)
44-
| LModule (Definition (stamp, _tip)) | LModule (LocalReference (stamp, _tip))
45-
-> (
44+
| LModule (Definition (stamp, _tip, _))
45+
| LModule (LocalReference (stamp, _tip)) -> (
4646
match Stamps.findModule file.stamps stamp with
4747
| None -> None
4848
| Some md -> (
@@ -85,7 +85,7 @@ let newHover ~full:{file; package} locItem =
8585
| Some file ->
8686
showModule ~docstring:file.structure.docstring ~name:file.moduleName ~file
8787
None)
88-
| Typed (_, _, Definition (_, (Field _ | Constructor _))) -> None
88+
| Typed (_, _, Definition (_, (Field _ | Constructor _), _)) -> None
8989
| Constant t ->
9090
Some
9191
(codeBlock

analysis/src/ProcessAttributes.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ let rec findDeprecatedAttribute attributes =
3434
| ({Asttypes.txt = "deprecated"}, _) :: _ -> Some ""
3535
| _ :: rest -> findDeprecatedAttribute rest
3636

37-
let newDeclared ~item ~scope ~extent ~name ~stamp ~modulePath isExported
38-
attributes =
37+
let newDeclared ~item ~scope ~extent ~name ~stamp ~modulePath ~isExported
38+
?(isTypeAnnotated = false) attributes =
3939
{
4040
Declared.name;
4141
stamp;
4242
extentLoc = extent;
4343
scopeLoc = scope;
4444
isExported;
45+
isTypeAnnotated;
4546
modulePath;
4647
deprecated = findDeprecatedAttribute attributes;
4748
docstring =

analysis/src/ProcessCmt.ml

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let sigItemsExtent items =
2222
items |> List.map (fun item -> item.Typedtree.sig_loc) |> locsExtent
2323

2424
let addItem ~(name : string Location.loc) ~extent ~stamp ~(env : Env.t) ~item
25-
attributes addExported addStamp =
25+
?(isTypeAnnotated = false) attributes addExported addStamp =
2626
let isExported = addExported name.txt stamp in
2727
let declared =
2828
ProcessAttributes.newDeclared ~item
@@ -32,7 +32,8 @@ let addItem ~(name : string Location.loc) ~extent ~stamp ~(env : Env.t) ~item
3232
loc_end = env.scope.loc_end;
3333
loc_ghost = false;
3434
}
35-
~extent ~name ~stamp ~modulePath:env.modulePath isExported attributes
35+
~extent ~name ~stamp ~modulePath:env.modulePath ~isExported
36+
~isTypeAnnotated attributes
3637
in
3738
addStamp env.stamps stamp declared;
3839
declared
@@ -45,7 +46,8 @@ let rec forTypeSignatureItem ~env ~(exported : Exported.t)
4546
let declared =
4647
addItem
4748
~name:(Location.mknoloc (Ident.name ident))
48-
~extent:loc ~stamp:(Ident.binding_time ident) ~env ~item val_attributes
49+
~extent:loc ~stamp:(Ident.binding_time ident) ~env ~item
50+
~isTypeAnnotated:true val_attributes
4951
(Exported.add exported Exported.Value)
5052
Stamps.addValue
5153
in
@@ -107,7 +109,8 @@ let rec forTypeSignatureItem ~env ~(exported : Exported.t)
107109
}
108110
~name:(Location.mknoloc name)
109111
~stamp (* TODO maybe this needs another child *)
110-
~modulePath:env.modulePath true cd_attributes
112+
~modulePath:env.modulePath ~isExported:true
113+
cd_attributes
111114
in
112115
Stamps.addConstructor env.stamps stamp declared;
113116
item))
@@ -247,7 +250,8 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
247250
let declared =
248251
addItem ~name
249252
~stamp:(Ident.binding_time val_id)
250-
~extent:val_loc ~item:val_desc.ctyp_type ~env val_attributes
253+
~extent:val_loc ~item:val_desc.ctyp_type ~env ~isTypeAnnotated:true
254+
val_attributes
251255
(Exported.add exported Exported.Value)
252256
Stamps.addValue
253257
in
@@ -341,6 +345,10 @@ let rec getModulePath mod_desc =
341345
| Tmod_constraint (expr, _typ, _constraint, _coercion) ->
342346
getModulePath expr.mod_desc
343347

348+
let patIsTypeAnnotated pat =
349+
pat.pat_extra
350+
|> List.exists (function Tpat_constraint _, _, _ -> true | _ -> false)
351+
344352
let rec forStructureItem ~env ~(exported : Exported.t) item =
345353
match item.str_desc with
346354
| Tstr_value (_isRec, bindings) ->
@@ -352,7 +360,8 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
352360
let item = pat.pat_type in
353361
let declared =
354362
addItem ~name ~stamp:(Ident.binding_time ident) ~env
355-
~extent:pat.pat_loc ~item attributes
363+
~extent:pat.pat_loc ~item ~isTypeAnnotated:(patIsTypeAnnotated pat)
364+
attributes
356365
(Exported.add exported Exported.Value)
357366
Stamps.addValue
358367
in
@@ -443,7 +452,7 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
443452
let declared =
444453
addItem ~extent:val_loc ~item:val_type ~name
445454
~stamp:(Ident.binding_time val_id)
446-
~env val_attributes
455+
~env ~isTypeAnnotated:true val_attributes
447456
(Exported.add exported Exported.Value)
448457
Stamps.addValue
449458
in
@@ -495,7 +504,8 @@ and forModule env mod_desc moduleName =
495504
loc_end = env.scope.loc_end;
496505
loc_ghost = false;
497506
}
498-
~extent:t.Typedtree.mty_loc ~stamp ~modulePath:NotVisible false []
507+
~extent:t.Typedtree.mty_loc ~stamp ~modulePath:NotVisible
508+
~isExported:false []
499509
in
500510
Stamps.addModule env.stamps stamp declared));
501511
forModule env resultExpr.mod_desc moduleName
@@ -629,12 +639,14 @@ let extraForFile ~(file : File.t) =
629639
in
630640
file.stamps
631641
|> Stamps.iterModules (fun stamp (d : Module.t Declared.t) ->
632-
addLocItem extra d.name.loc (LModule (Definition (stamp, Module)));
642+
addLocItem extra d.name.loc
643+
(LModule (Definition (stamp, Module, false)));
633644
addReference stamp d.name.loc);
634645
file.stamps
635646
|> Stamps.iterValues (fun stamp (d : Types.type_expr Declared.t) ->
636647
addLocItem extra d.name.loc
637-
(Typed (d.name.txt, d.item, Definition (stamp, Value)));
648+
(Typed
649+
(d.name.txt, d.item, Definition (stamp, Value, d.isTypeAnnotated)));
638650
addReference stamp d.name.loc);
639651
file.stamps
640652
|> Stamps.iterTypes (fun stamp (d : Type.t Declared.t) ->
@@ -648,7 +660,9 @@ let extraForFile ~(file : File.t) =
648660
addReference stamp fname.loc;
649661
addLocItem extra fname.loc
650662
(Typed
651-
(d.name.txt, typ, Definition (d.stamp, Field fname.txt))))
663+
( d.name.txt,
664+
typ,
665+
Definition (d.stamp, Field fname.txt, false) )))
652666
| Variant constructors ->
653667
constructors
654668
|> List.iter (fun {Constructor.stamp; cname} ->
@@ -669,7 +683,7 @@ let extraForFile ~(file : File.t) =
669683
(Typed
670684
( d.name.txt,
671685
t,
672-
Definition (d.stamp, Constructor cname.txt) )))
686+
Definition (d.stamp, Constructor cname.txt, false) )))
673687
| _ -> ());
674688
extra
675689

@@ -1002,12 +1016,13 @@ struct
10021016
loc_start = val_loc.loc_end;
10031017
loc_end = (currentScopeExtent ()).loc_end;
10041018
}
1005-
~modulePath:NotVisible ~item:val_desc.ctyp_type false val_attributes
1019+
~modulePath:NotVisible ~item:val_desc.ctyp_type ~isExported:false
1020+
~isTypeAnnotated:true val_attributes
10061021
in
10071022
Stamps.addValue Collector.file.stamps stamp declared;
10081023
addReference stamp name.loc;
10091024
addLocItem extra name.loc
1010-
(Typed (name.txt, val_desc.ctyp_type, Definition (stamp, Value))))
1025+
(Typed (name.txt, val_desc.ctyp_type, Definition (stamp, Value, true))))
10111026
| _ -> ()
10121027

10131028
let enter_core_type {ctyp_type; ctyp_desc} =
@@ -1016,7 +1031,7 @@ struct
10161031
addForLongident (Some (ctyp_type, Type)) path txt loc
10171032
| _ -> ()
10181033

1019-
let enter_pattern {pat_desc; pat_loc; pat_type; pat_attributes} =
1034+
let enter_pattern ({pat_desc; pat_loc; pat_type; pat_attributes} as pat) =
10201035
let addForPattern stamp name =
10211036
if Stamps.findValue Collector.file.stamps stamp = None then (
10221037
let declared =
@@ -1027,13 +1042,14 @@ struct
10271042
loc_start = pat_loc.loc_end;
10281043
loc_end = (currentScopeExtent ()).loc_end;
10291044
}
1030-
~modulePath:NotVisible ~extent:pat_loc ~item:pat_type false
1045+
~modulePath:NotVisible ~extent:pat_loc ~item:pat_type
1046+
~isExported:false ~isTypeAnnotated:(patIsTypeAnnotated pat)
10311047
pat_attributes
10321048
in
10331049
Stamps.addValue Collector.file.stamps stamp declared;
10341050
addReference stamp name.loc;
10351051
addLocItem extra name.loc
1036-
(Typed (name.txt, pat_type, Definition (stamp, Value))))
1052+
(Typed (name.txt, pat_type, Definition (stamp, Value, false))))
10371053
in
10381054
(* Log.log("Entering pattern " ++ Utils.showLocation(pat_loc)); *)
10391055
match pat_desc with

analysis/src/References.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let getLocItem ~full ~line ~col =
6767
Some li3
6868
| [
6969
{locType = Typed (_, _, LocalReference (_, Value))};
70-
({locType = Typed (_, _, Definition (_, Value))} as li2);
70+
({locType = Typed (_, _, Definition (_, Value, _))} as li2);
7171
] ->
7272
(* JSX on type-annotated labeled (~arg:t):
7373
(~arg:t) becomes Props#arg
@@ -138,7 +138,7 @@ let definedForLoc ~file ~package locKind =
138138
in
139139
match locKind with
140140
| NotFound -> None
141-
| LocalReference (stamp, tip) | Definition (stamp, tip) ->
141+
| LocalReference (stamp, tip) | Definition (stamp, tip, _) ->
142142
inner ~file stamp tip
143143
| GlobalReference (moduleName, path, tip) -> (
144144
maybeLog ("Getting global " ^ moduleName);
@@ -306,7 +306,7 @@ let definition ~file ~package stamp (tip : Tip.t) =
306306

307307
let definitionForLocItem ~full:{file; package} locItem =
308308
match locItem.locType with
309-
| Typed (_, _, Definition (stamp, tip)) -> (
309+
| Typed (_, _, Definition (stamp, tip, _)) -> (
310310
maybeLog
311311
("Typed Definition stamp:" ^ string_of_int stamp ^ " tip:"
312312
^ Tip.toString tip);
@@ -323,7 +323,7 @@ let definitionForLocItem ~full:{file; package} locItem =
323323
Some (file.uri, loc))
324324
else None)
325325
| Typed (_, _, NotFound)
326-
| LModule (NotFound | Definition (_, _))
326+
| LModule (NotFound | Definition _)
327327
| TypeDefinition (_, _, _)
328328
| Constant _ ->
329329
None
@@ -530,8 +530,8 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem =
530530
List.append targetModuleReferences otherModulesReferences
531531
| Typed (_, _, NotFound) | LModule NotFound | Constant _ -> []
532532
| TypeDefinition (_, _, stamp) -> forLocalStamp ~full stamp Type
533-
| Typed (_, _, (LocalReference (stamp, tip) | Definition (stamp, tip)))
534-
| LModule (LocalReference (stamp, tip) | Definition (stamp, tip)) ->
533+
| Typed (_, _, (LocalReference (stamp, tip) | Definition (stamp, tip, _)))
534+
| LModule (LocalReference (stamp, tip) | Definition (stamp, tip, _)) ->
535535
maybeLog
536536
("Finding references for " ^ Uri2.toString file.uri ^ " and stamp "
537537
^ string_of_int stamp ^ " and tip " ^ Tip.toString tip);

analysis/src/SharedTypes.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ module Declared = struct
132132
stamp : int;
133133
modulePath : modulePath;
134134
isExported : bool;
135+
isTypeAnnotated : bool;
135136
deprecated : string option;
136137
docstring : string list;
137138
item : 'item;
@@ -297,7 +298,7 @@ type locKind =
297298
| LocalReference of int * Tip.t
298299
| GlobalReference of string * string list * Tip.t
299300
| NotFound
300-
| Definition of int * Tip.t
301+
| Definition of int * Tip.t * (* whether it is type annotated *) bool
301302

302303
type locType =
303304
| Typed of string * Types.type_expr * locKind
@@ -370,7 +371,8 @@ let locKindToString = function
370371
| LocalReference (_, tip) -> "(LocalReference " ^ Tip.toString tip ^ ")"
371372
| GlobalReference _ -> "GlobalReference"
372373
| NotFound -> "NotFound"
373-
| Definition (_, tip) -> "(Definition " ^ Tip.toString tip ^ ")"
374+
| Definition (_, tip, _hasTypeAnnotation) ->
375+
"(Definition " ^ Tip.toString tip ^ ")"
374376

375377
let locTypeToString = function
376378
| Typed (name, e, locKind) ->

0 commit comments

Comments
 (0)