Skip to content

Commit a4da482

Browse files
Will Pittsfredemmott
Will Pitts
authored andcommitted
Restrict autocomplete after "use" to only traits when in a class.
Summary: When autocompleting after the "use" keyword, if it's in a class we shoud only return trait names. Differential Revision: D7999875
1 parent 2760d86 commit a4da482

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

hphp/hack/src/server/autocompleteService.ml

+30-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type autocomplete_type =
3131
| Actype
3232
| Acclass_get
3333
| Acprop
34+
| Actrait_only
3435

3536
let (argument_global_type: autocomplete_type option ref) = ref None
3637
let auto_complete_for_global = ref ""
@@ -118,6 +119,8 @@ let autocomplete_id id env = autocomplete_token Acid (Some env) id
118119

119120
let autocomplete_hint = autocomplete_token Actype None
120121

122+
let autocomplete_trait_only = autocomplete_token Actrait_only None
123+
121124
let autocomplete_new cid env =
122125
match cid with
123126
| Nast.CI (sid, _) -> autocomplete_token Acnew (Some env) sid
@@ -164,6 +167,7 @@ let should_complete_class completion_type class_kind =
164167
| Some Acid, Some Ast.Cnormal
165168
| Some Acid, Some Ast.Cabstract
166169
| Some Acnew, Some Ast.Cnormal
170+
| Some Actrait_only, Some Ast.Ctrait
167171
| Some Actype, Some _ -> true
168172
| _ -> false
169173

@@ -529,7 +533,7 @@ let autocomplete_typed_member ~is_static env class_ty cid mid =
529533
let autocomplete_static_member env (ty, cid) mid =
530534
autocomplete_typed_member ~is_static:true env ty (Some cid) mid
531535

532-
class ['self] visitor = object (_ : 'self)
536+
class ['self] visitor = object (self : 'self)
533537
inherit [_] Tast_visitor.iter as super
534538

535539
method! on_Id env id =
@@ -579,6 +583,18 @@ class ['self] visitor = object (_ : 'self)
579583
end
580584
end;
581585
super#on_Xml env sid attrs el
586+
587+
method! on_class_ env cls =
588+
List.iter cls.Tast.c_uses ~f:begin fun hint ->
589+
match snd hint with
590+
| Aast.Happly (sid, params) ->
591+
autocomplete_trait_only sid;
592+
List.iter params (self#on_hint env)
593+
| _ -> ()
594+
end;
595+
(* If we don't clear out c_uses we'll end up overwriting the trait
596+
completion as soon as we get to on_Happly. *)
597+
super#on_class_ env {cls with Tast.c_uses = []}
582598
end
583599

584600
class ['self] auto_complete_suffix_finder = object (_ : 'self)
@@ -667,21 +683,32 @@ let go
667683
let completion_type = !argument_global_type in
668684
if completion_type = Some Acid ||
669685
completion_type = Some Acnew ||
670-
completion_type = Some Actype
686+
completion_type = Some Actype ||
687+
completion_type = Some Actrait_only
671688
then compute_complete_global
672689
~tcopt ~delimit_on_namespaces ~autocomplete_context ~content_funs ~content_classes;
673690
if completion_type = Some Acprop then compute_complete_local tast;
674691
let env = match !ac_env with
675692
| Some e -> e
676693
| None -> Typing_env.empty tcopt Relative_path.default ~droot:None
677694
in
695+
let filter_results (result: autocomplete_result) : bool =
696+
let kind = match result with
697+
| Partial res -> res.kind_
698+
| Complete res -> res.res_kind
699+
in
700+
match completion_type, kind with
701+
| Some Actrait_only, Trait_kind -> true
702+
| Some Actrait_only, _ -> false
703+
| _ -> true
704+
in
678705
let resolve (result: autocomplete_result) : complete_autocomplete_result =
679706
match result with
680707
| Partial res -> resolve_ty env autocomplete_context res ~delimit_on_namespaces
681708
| Complete res -> res
682709
in
683710
{
684711
With_complete_flag.is_complete = !autocomplete_is_complete;
685-
value = !autocomplete_results |> List.map ~f:resolve;
712+
value = !autocomplete_results |> List.filter ~f:filter_results |> List.map ~f:resolve;
686713
}
687714
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?hh // strict
2+
class MyAutocompleteAfterUse {
3+
use MyAUTO332
4+
}
5+
6+
trait MyTrait {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MyTrait trait

0 commit comments

Comments
 (0)
  翻译: