Skip to content

Commit d1fe878

Browse files
committed
Refactor sharedtypes.
1 parent 65c4c63 commit d1fe878

File tree

6 files changed

+132
-126
lines changed

6 files changed

+132
-126
lines changed

analysis/src/Commands.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ let documentSymbol ~path =
174174
| None -> Protocol.null
175175
| Some {file} ->
176176
let open SharedTypes in
177-
let rec getItems {topLevel} =
177+
let rec getItems {ModuleKind.topLevel} =
178178
let rec getItem = function
179-
| MValue v -> (v |> SharedTypes.variableKind, [])
180-
| MType (t, _) -> (t.decl |> SharedTypes.declarationKind, [])
179+
| ModuleKind.Value v -> (v |> SharedTypes.variableKind, [])
180+
| Type (t, _) -> (t.decl |> SharedTypes.declarationKind, [])
181181
| Module (Structure contents) -> (Module, getItems contents)
182182
| Module (Constraint (_, modTypeItem)) -> getItem (Module modTypeItem)
183183
| Module (Ident _) -> (Module, [])

analysis/src/Hover.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
let codeBlock code = Printf.sprintf "```rescript\n%s\n```" code
22

33
let showModuleTopLevel ~docstring ~name
4-
(topLevel : SharedTypes.moduleItem SharedTypes.declared list) =
4+
(topLevel : SharedTypes.ModuleKind.moduleItem SharedTypes.declared list) =
55
let contents =
66
topLevel
77
|> List.map (fun item ->
88
match item.SharedTypes.item with
99
(* TODO pretty print module contents *)
10-
| SharedTypes.MType ({decl}, recStatus) ->
10+
| SharedTypes.ModuleKind.Type ({decl}, recStatus) ->
1111
" " ^ (decl |> Shared.declToString ~recStatus item.name.txt)
1212
| Module _ -> " module " ^ item.name.txt
13-
| MValue typ ->
13+
| Value typ ->
1414
" let " ^ item.name.txt ^ ": " ^ (typ |> Shared.typeToString))
1515
(* TODO indent *)
1616
|> String.concat "\n"
@@ -24,7 +24,7 @@ let showModuleTopLevel ~docstring ~name
2424
Some (doc ^ full)
2525

2626
let rec showModule ~docstring ~(file : SharedTypes.File.t) ~name
27-
(declared : SharedTypes.moduleKind SharedTypes.declared option) =
27+
(declared : SharedTypes.ModuleKind.t SharedTypes.declared option) =
2828
match declared with
2929
| None -> showModuleTopLevel ~docstring ~name file.contents.topLevel
3030
| Some {item = Structure {topLevel}} ->

analysis/src/NewCompletions.ml

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -496,23 +496,6 @@ let resolveOpens ~env ~previous opens ~package =
496496
(* loop(previous) *)
497497
previous opens
498498

499-
type kind =
500-
| Module of moduleKind
501-
| Value of Types.type_expr
502-
| Type of Type.t
503-
| Constructor of constructor * Type.t declared
504-
| Field of field * Type.t declared
505-
| FileModule of string
506-
507-
let kindToInt kind =
508-
match kind with
509-
| Module _ -> 9
510-
| FileModule _ -> 9
511-
| Constructor (_, _) -> 4
512-
| Field (_, _) -> 5
513-
| Type _ -> 22
514-
| Value _ -> 12
515-
516499
let completionForDeclareds ~pos declareds prefix transformContents =
517500
(* Log.log("completion for declares " ++ prefix); *)
518501
Hashtbl.fold
@@ -528,15 +511,15 @@ let completionForDeclareds ~pos declareds prefix transformContents =
528511

529512
let completionForDeclaredModules ~pos ~env ~suffix =
530513
completionForDeclareds ~pos env.QueryEnv.file.stamps.modules suffix (fun m ->
531-
Module m)
514+
Kind.Module m)
532515

533516
let completionForDeclaredValues ~pos ~env ~suffix =
534517
completionForDeclareds ~pos env.QueryEnv.file.stamps.values suffix (fun m ->
535-
Value m)
518+
Kind.Value m)
536519

537520
let completionForDeclaredTypes ~pos ~env ~suffix =
538521
completionForDeclareds ~pos env.QueryEnv.file.stamps.types suffix (fun m ->
539-
Type m)
522+
Kind.Type m)
540523

541524
let completionForExporteds exporteds
542525
(stamps : (int, 'a SharedTypes.declared) Hashtbl.t) prefix transformContents
@@ -552,15 +535,15 @@ let completionForExporteds exporteds
552535

553536
let completionForExportedModules ~env ~suffix =
554537
completionForExporteds env.QueryEnv.exported.modules env.file.stamps.modules
555-
suffix (fun m -> Module m)
538+
suffix (fun m -> Kind.Module m)
556539

557540
let completionForExportedValues ~env ~suffix =
558541
completionForExporteds env.QueryEnv.exported.values env.file.stamps.values
559-
suffix (fun v -> Value v)
542+
suffix (fun v -> Kind.Value v)
560543

561544
let completionForExportedTypes ~env ~suffix =
562545
completionForExporteds env.QueryEnv.exported.types env.file.stamps.types
563-
suffix (fun t -> Type t)
546+
suffix (fun t -> Kind.Type t)
564547

565548
let completionForConstructors ~(env : QueryEnv.t) ~suffix =
566549
Hashtbl.fold
@@ -575,7 +558,7 @@ let completionForConstructors ~(env : QueryEnv.t) ~suffix =
575558
| _ -> results)
576559
env.exported.types []
577560
|> List.map (fun (c, t) ->
578-
{(emptyDeclared c.cname.txt) with item = Constructor (c, t)})
561+
{(emptyDeclared c.cname.txt) with item = Kind.Constructor (c, t)})
579562

580563
let completionForFields ~(env : QueryEnv.t) ~suffix =
581564
Hashtbl.fold
@@ -590,7 +573,7 @@ let completionForFields ~(env : QueryEnv.t) ~suffix =
590573
| _ -> results)
591574
env.exported.types []
592575
|> List.map (fun (f, t) ->
593-
{(emptyDeclared f.fname.txt) with item = Field (f, t)})
576+
{(emptyDeclared f.fname.txt) with item = Kind.Field (f, t)})
594577

595578
let isCapitalized name =
596579
if name = "" then false
@@ -669,8 +652,8 @@ let getEnvWithOpens ~pos ~(env : QueryEnv.t) ~package ~(opens : QueryEnv.t list)
669652
in
670653
loop opens
671654

672-
let detail name contents =
673-
match contents with
655+
let detail name (kind : Kind.t) =
656+
match kind with
674657
| Type {decl} -> decl |> Shared.declToString name
675658
| Value typ -> typ |> Shared.typeToString
676659
| Module _ -> "module"
@@ -827,7 +810,9 @@ let getItems ~full ~rawOpens ~allFiles ~pos ~dotpath =
827810
allFiles |> FileSet.elements
828811
|> Utils.filterMap (fun name ->
829812
if Utils.startsWith name suffix && not (String.contains name '-')
830-
then Some ({(emptyDeclared name) with item = FileModule name}, env)
813+
then
814+
Some
815+
({(emptyDeclared name) with item = Kind.FileModule name}, env)
831816
else None)
832817
in
833818
locallyDefinedValues @ valuesFromOpens @ localModuleNames
@@ -878,7 +863,7 @@ let getItems ~full ~rawOpens ~allFiles ~pos ~dotpath =
878863
Some
879864
( {
880865
(emptyDeclared field.fname.txt) with
881-
item = Field (field, typ);
866+
item = Kind.Field (field, typ);
882867
},
883868
env )
884869
else None))))
@@ -933,7 +918,7 @@ let processCompletable ~processDotPath ~full ~package ~rawOpens
933918
let declareds = processDotPath ~exact:true (componentPath @ ["make"]) in
934919
let labels =
935920
match declareds with
936-
| ({SharedTypes.item = Value typ}, _env) :: _ ->
921+
| ({SharedTypes.item = Kind.Value typ}, _env) :: _ ->
937922
let rec getFields (texp : Types.type_expr) =
938923
match texp.desc with
939924
| Tfield (name, _, t1, t2) ->
@@ -996,7 +981,7 @@ let processCompletable ~processDotPath ~full ~package ~rawOpens
996981
(fun
997982
({SharedTypes.name = {txt = name}; deprecated; docstring; item}, _env)
998983
->
999-
mkItem ~name ~kind:(kindToInt item) ~deprecated
984+
mkItem ~name ~kind:(Kind.toInt item) ~deprecated
1000985
~detail:(detail name item) ~docstring)
1001986
| Cpipe (pipe, partialName) -> (
1002987
let arrayModulePath = ["Js"; "Array2"] in
@@ -1107,13 +1092,13 @@ let processCompletable ~processDotPath ~full ~package ~rawOpens
11071092
let declareds = dotpath |> processDotPath ~exact:false in
11081093
declareds
11091094
|> List.filter (fun ({item}, _env) ->
1110-
match item with Value _ -> true | _ -> false)
1095+
match item with Kind.Value _ -> true | _ -> false)
11111096
|> List.map
11121097
(fun
11131098
( {SharedTypes.name = {txt = name}; deprecated; docstring; item},
11141099
_env )
11151100
->
1116-
mkItem ~name:(completionName name) ~kind:(kindToInt item)
1101+
mkItem ~name:(completionName name) ~kind:(Kind.toInt item)
11171102
~detail:(detail name item) ~deprecated ~docstring)
11181103
| _ -> [])
11191104
| None -> [])

analysis/src/ProcessCmt.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let rec forTypeSignatureItem ~env ~(exported : SharedTypes.exported)
5050
~extent:loc ~stamp:(Ident.binding_time ident) ~env ~item val_attributes
5151
exported.values env.stamps.values
5252
in
53-
[{declared with item = MValue declared.item}]
53+
[{declared with item = ModuleKind.Value declared.item}]
5454
| Sig_type
5555
( ident,
5656
({type_loc; type_kind; type_manifest; type_attributes} as decl),
@@ -122,7 +122,7 @@ let rec forTypeSignatureItem ~env ~(exported : SharedTypes.exported)
122122
~stamp:(Ident.binding_time ident) ~env type_attributes exported.types
123123
env.stamps.types
124124
in
125-
[{declared with item = MType (declared.item, recStatus)}]
125+
[{declared with item = Type (declared.item, recStatus)}]
126126
| Sig_module (ident, {md_type; md_attributes; md_loc}, _) ->
127127
let declared =
128128
addItem ~extent:md_loc
@@ -141,7 +141,7 @@ and forTypeSignature env signature =
141141
(fun item items -> forTypeSignatureItem ~env ~exported item @ items)
142142
signature []
143143
in
144-
{docstring = []; exported; topLevel}
144+
{ModuleKind.docstring = []; exported; topLevel}
145145

146146
and forTypeModule env moduleType =
147147
match moduleType with
@@ -213,7 +213,7 @@ let forTypeDeclaration ~env ~(exported : exported)
213213
}
214214
~name ~stamp ~env typ_attributes exported.types env.stamps.types
215215
in
216-
{declared with item = MType (declared.item, recStatus)}
216+
{declared with item = ModuleKind.Type (declared.item, recStatus)}
217217

218218
let rec forSignatureItem ~env ~(exported : exported)
219219
(item : Typedtree.signature_item) =
@@ -225,7 +225,7 @@ let rec forSignatureItem ~env ~(exported : exported)
225225
~extent:val_loc ~item:val_desc.ctyp_type ~env val_attributes
226226
exported.values env.stamps.values
227227
in
228-
[{declared with item = MValue declared.item}]
228+
[{declared with item = ModuleKind.Value declared.item}]
229229
| Tsig_type (recFlag, decls) ->
230230
decls
231231
|> List.mapi (fun i decl ->
@@ -281,14 +281,14 @@ let forSignature ~env items =
281281
| None -> []
282282
| Some d -> [d]
283283
in
284-
{docstring; exported; topLevel}
284+
{ModuleKind.docstring; exported; topLevel}
285285

286286
let forTreeModuleType ~env {mty_desc} =
287287
match mty_desc with
288288
| Tmty_ident _ -> None
289289
| Tmty_signature {sig_items} ->
290290
let contents = forSignature ~env sig_items in
291-
Some (Structure contents)
291+
Some (ModuleKind.Structure contents)
292292
| _ -> None
293293

294294
let rec getModulePath mod_desc =
@@ -315,7 +315,8 @@ let rec forStructureItem ~env ~(exported : exported) item =
315315
~extent:pat.pat_loc ~item attributes exported.values
316316
env.stamps.values
317317
in
318-
declareds := {declared with item = MValue declared.item} :: !declareds
318+
declareds :=
319+
{declared with item = ModuleKind.Value declared.item} :: !declareds
319320
| Tpat_tuple pats | Tpat_array pats | Tpat_construct (_, _, pats) ->
320321
pats |> List.iter (fun p -> handlePattern [] p)
321322
| Tpat_or (p, _, _) -> handlePattern [] p
@@ -382,7 +383,7 @@ let rec forStructureItem ~env ~(exported : exported) item =
382383
~stamp:(Ident.binding_time val_id)
383384
~env val_attributes exported.values env.stamps.values
384385
in
385-
[{declared with item = MValue declared.item}]
386+
[{declared with item = Value declared.item}]
386387
| Tstr_type (recFlag, decls) ->
387388
decls
388389
|> List.mapi (fun i decl ->
@@ -1319,7 +1320,7 @@ let rec getSourceUri ~(env : QueryEnv.t) ~package path =
13191320
| Some (env, _declared) -> env.file.uri)
13201321
| ExportedModule (_, inner) -> getSourceUri ~env ~package inner
13211322

1322-
let exportedForTip ~(env : QueryEnv.t) name tip =
1323+
let exportedForTip ~(env : QueryEnv.t) name (tip : Tip.t) =
13231324
match tip with
13241325
| Value -> Hashtbl.find_opt env.exported.values name
13251326
| Field _ | Constructor _ | Type -> Hashtbl.find_opt env.exported.types name

0 commit comments

Comments
 (0)