Skip to content

Commit 71aa83f

Browse files
committed
Move completable to own module.
1 parent 3bd76e4 commit 71aa83f

File tree

4 files changed

+59
-56
lines changed

4 files changed

+59
-56
lines changed

analysis/src/Commands.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let completion ~debug ~path ~pos ~currentFile =
1313
| Some (completable, scope) -> (
1414
if debug then
1515
Printf.printf "Completable: %s\n"
16-
(SharedTypes.completableToString completable);
16+
(SharedTypes.Completable.toString completable);
1717
(* Only perform expensive ast operations if there are completables *)
1818
match Cmt.fromPath ~path with
1919
| None -> []

analysis/src/Completion.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor ~posAfterCompName
4747
match props with
4848
| prop :: rest ->
4949
if prop.posStart <= posBeforeCursor && posBeforeCursor < prop.posEnd then
50-
Some (Cjsx (jsxProps.componentPath, prop.name, allLabels))
50+
Some (Completable.Cjsx (jsxProps.componentPath, prop.name, allLabels))
5151
else if
5252
prop.posEnd <= posBeforeCursor
5353
&& posBeforeCursor < Loc.start prop.exp.pexp_loc
@@ -183,7 +183,7 @@ let findExpApplyCompletable ~(args : arg list) ~endPos ~posBeforeCursor
183183
if
184184
labelled.posStart <= posBeforeCursor
185185
&& posBeforeCursor < labelled.posEnd
186-
then Some (Clabel (funPath, labelled.name, allNames))
186+
then Some (Completable.Clabel (funPath, labelled.name, allNames))
187187
else if exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then None
188188
else loop rest
189189
| {label = None; exp} :: rest ->
@@ -266,7 +266,7 @@ let extractExpApplyArgs ~text ~(funName : Longident.t Location.loc) ~args =
266266

267267
let rec exprToContextPath (e : Parsetree.expression) =
268268
match e.pexp_desc with
269-
| Pexp_constant (Pconst_string _) -> Some CPString
269+
| Pexp_constant (Pconst_string _) -> Some Completable.CPString
270270
| Pexp_array _ -> Some CPArray
271271
| Pexp_ident {txt} -> Some (CPId (Utils.flattenLongIdent txt, Value))
272272
| Pexp_field (e1, {txt = Lident name}) -> (
@@ -459,7 +459,7 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
459459
if debug then
460460
Printf.printf "Attribute id:%s:%s label:%s\n" id.txt
461461
(Loc.toString id.loc) label;
462-
setResult (Cdecorator label)
462+
setResult (Completable.Cdecorator label)
463463
| _ -> ());
464464
Ast_iterator.default_iterator.attribute iterator (id, payload)
465465
in
@@ -540,13 +540,13 @@ let completionWithParser ~debug ~path ~posCursor ~currentFile ~text =
540540
| Lident name -> (
541541
match exprToContextPath e with
542542
| Some contextPath ->
543-
let contextPath = CPField (contextPath, name) in
543+
let contextPath = Completable.CPField (contextPath, name) in
544544
setResult (Cpath contextPath)
545545
| None -> ())
546546
| Ldot (id, name) ->
547547
(* Case x.M.field ignore the x part *)
548548
let contextPath =
549-
CPField
549+
Completable.CPField
550550
( CPId (Utils.flattenLongIdent id, Module),
551551
if name = "_" then "" else name )
552552
in

analysis/src/NewCompletions.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ let detail name (kind : Completion.kind) =
665665
| Constructor (c, s) -> showConstructor c ^ "\n\n" ^ s
666666

667667
let findAllCompletions ~(env : QueryEnv.t) ~prefix ~exact ~namesUsed
668-
~(completionContext : completionContext) =
668+
~(completionContext : Completable.completionContext) =
669669
Log.log ("findAllCompletions uri:" ^ Uri2.toString env.file.uri);
670670
match completionContext with
671671
| Value ->
@@ -846,7 +846,7 @@ let findLocalCompletionsForModules ~env ~prefix ~exact ~opens ~scope =
846846
List.rev_append !resultRev valuesFromOpens
847847

848848
let findLocalCompletionsWithOpens ~pos ~(env : QueryEnv.t) ~prefix ~exact ~opens
849-
~scope ~(completionContext : completionContext) =
849+
~scope ~(completionContext : Completable.completionContext) =
850850
(* TODO: handle arbitrary interleaving of opens and local bindings correctly *)
851851
Log.log
852852
("findLocalCompletionsWithOpens uri:" ^ Uri2.toString env.file.uri ^ " pos:"
@@ -963,7 +963,7 @@ let completionsGetTypeEnv = function
963963
| _ -> None
964964

965965
let rec getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos
966-
~env ~exact ~scope (contextPath : contextPath) =
966+
~env ~exact ~scope (contextPath : Completable.contextPath) =
967967
match contextPath with
968968
| CPString ->
969969
[
@@ -1148,7 +1148,7 @@ let getOpens ~rawOpens ~package ~env =
11481148
(* Last open takes priority *)
11491149
List.rev resolvedOpens
11501150

1151-
let processCompletable ~package ~scope ~env ~pos (completable : completable) =
1151+
let processCompletable ~package ~scope ~env ~pos (completable : Completable.t) =
11521152
let rawOpens = Scope.getRawOpens scope in
11531153
let opens = getOpens ~rawOpens ~package ~env in
11541154
let allFiles = FileSet.union package.projectFiles package.dependenciesFiles in
@@ -1306,5 +1306,6 @@ let processCompletable ~package ~scope ~env ~pos (completable : completable) =
13061306
Utils.startsWith name prefix && not (List.mem name identsSeen))
13071307
|> List.map mkLabel
13081308

1309-
let computeCompletions ~(completable : completable) ~package ~pos ~scope ~env =
1309+
let computeCompletions ~(completable : Completable.t) ~package ~pos ~scope ~env
1310+
=
13101311
completable |> processCompletable ~package ~scope ~env ~pos

analysis/src/SharedTypes.ml

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -396,47 +396,49 @@ let locItemToString {loc = {Location.loc_start; loc_end}; locType} =
396396
(* needed for debugging *)
397397
let _ = locItemToString
398398

399-
(* Completion context *)
400-
type completionContext = Type | Value | Module | Field
401-
402-
type contextPath =
403-
| CPString
404-
| CPArray
405-
| CPId of string list * completionContext
406-
| CPField of contextPath * string
407-
| CPObj of contextPath * string
408-
| CPPipe of contextPath * string
409-
410-
type completable =
411-
| Cdecorator of string (** e.g. @module *)
412-
| Clabel of string list * string * string list
413-
(** e.g. (["M", "foo"], "label", ["l1", "l2"]) for M.foo(...~l1...~l2...~label...) *)
414-
| Cpath of contextPath
415-
| Cjsx of string list * string * string list
416-
(** E.g. (["M", "Comp"], "id", ["id1", "id2"]) for <M.Comp id1=... id2=... ... id *)
417-
418-
let completableToString =
419-
let str s = if s = "" then "\"\"" else s in
420-
let list l = "[" ^ (l |> List.map str |> String.concat ", ") ^ "]" in
421-
let completionContextToString = function
422-
| Value -> "Value"
423-
| Type -> "Type"
424-
| Module -> "Module"
425-
| Field -> "Field"
426-
in
427-
let rec contextPathToString = function
428-
| CPString -> "string"
429-
| CPArray -> "array"
430-
| CPId (sl, completionContext) ->
431-
completionContextToString completionContext ^ list sl
432-
| CPField (cp, s) -> contextPathToString cp ^ "." ^ str s
433-
| CPObj (cp, s) -> contextPathToString cp ^ "[\"" ^ s ^ "\"]"
434-
| CPPipe (cp, s) -> contextPathToString cp ^ "->" ^ s
435-
in
436-
function
437-
| Cpath cp -> "Cpath " ^ contextPathToString cp
438-
| Cdecorator s -> "Cdecorator(" ^ str s ^ ")"
439-
| Clabel (sl1, s, sl2) ->
440-
"Clabel(" ^ (sl1 |> list) ^ ", " ^ str s ^ ", " ^ (sl2 |> list) ^ ")"
441-
| Cjsx (sl1, s, sl2) ->
442-
"Cjsx(" ^ (sl1 |> list) ^ ", " ^ str s ^ ", " ^ (sl2 |> list) ^ ")"
399+
module Completable = struct
400+
(* Completion context *)
401+
type completionContext = Type | Value | Module | Field
402+
403+
type contextPath =
404+
| CPString
405+
| CPArray
406+
| CPId of string list * completionContext
407+
| CPField of contextPath * string
408+
| CPObj of contextPath * string
409+
| CPPipe of contextPath * string
410+
411+
type t =
412+
| Cdecorator of string (** e.g. @module *)
413+
| Clabel of string list * string * string list
414+
(** e.g. (["M", "foo"], "label", ["l1", "l2"]) for M.foo(...~l1...~l2...~label...) *)
415+
| Cpath of contextPath
416+
| Cjsx of string list * string * string list
417+
(** E.g. (["M", "Comp"], "id", ["id1", "id2"]) for <M.Comp id1=... id2=... ... id *)
418+
419+
let toString =
420+
let str s = if s = "" then "\"\"" else s in
421+
let list l = "[" ^ (l |> List.map str |> String.concat ", ") ^ "]" in
422+
let completionContextToString = function
423+
| Value -> "Value"
424+
| Type -> "Type"
425+
| Module -> "Module"
426+
| Field -> "Field"
427+
in
428+
let rec contextPathToString = function
429+
| CPString -> "string"
430+
| CPArray -> "array"
431+
| CPId (sl, completionContext) ->
432+
completionContextToString completionContext ^ list sl
433+
| CPField (cp, s) -> contextPathToString cp ^ "." ^ str s
434+
| CPObj (cp, s) -> contextPathToString cp ^ "[\"" ^ s ^ "\"]"
435+
| CPPipe (cp, s) -> contextPathToString cp ^ "->" ^ s
436+
in
437+
function
438+
| Cpath cp -> "Cpath " ^ contextPathToString cp
439+
| Cdecorator s -> "Cdecorator(" ^ str s ^ ")"
440+
| Clabel (sl1, s, sl2) ->
441+
"Clabel(" ^ (sl1 |> list) ^ ", " ^ str s ^ ", " ^ (sl2 |> list) ^ ")"
442+
| Cjsx (sl1, s, sl2) ->
443+
"Cjsx(" ^ (sl1 |> list) ^ ", " ^ str s ^ ", " ^ (sl2 |> list) ^ ")"
444+
end

0 commit comments

Comments
 (0)