Skip to content

Commit 0fecd7e

Browse files
dlreevesfacebook-github-bot
authored andcommitted
Evaluate method calls within attribute
Summary: Takes advantage of D72272575 by allowing the SimpliHack interpreter to directly evaluate the first argument instead of building a call itself. I'll add support for top level function calls in a subsequent diff. Reviewed By: patriciamckenzie Differential Revision: D72281549 fbshipit-source-id: e19e2c83f050379296acfee5fef48d18922215ce
1 parent fc260b8 commit 0fecd7e

File tree

8 files changed

+30
-55
lines changed

8 files changed

+30
-55
lines changed

hphp/hack/src/client/ide_service/code_actions_services/code_actions_commands.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ let generate_simplihack_commands ctx tast pos =
222222
Simplihack_prompt.find ctx tast.Tast_with_dynamic.under_normal_assumptions
223223
in
224224
let create_code_action
225-
Simplihack_prompt.{ attribute_pos; derive_prompt; edit_span } =
225+
Simplihack_prompt.{ param_pos; derive_prompt; edit_span } =
226226
let open Option.Let_syntax in
227227
(* Convert the relative position to an absolute position *)
228-
let attribute_pos = Pos.to_absolute attribute_pos in
228+
let param_pos = Pos.to_absolute param_pos in
229229
let* derived_prompt = derive_prompt () in
230230
let user_prompt =
231231
Format.sprintf
@@ -251,7 +251,7 @@ let generate_simplihack_commands ctx tast pos =
251251
}
252252
in
253253
(* Calculate the webview start line (LSP uses 0-based line numbers) *)
254-
let webview_start_line = Pos.line attribute_pos - 1 in
254+
let webview_start_line = Pos.line param_pos - 1 in
255255
let command_args =
256256
Code_action_types.(
257257
Show_inline_chat
@@ -272,7 +272,7 @@ let generate_simplihack_commands ctx tast pos =
272272
~f:(fun prompt ->
273273
if
274274
Pos.inside_one_based
275-
prompt.Simplihack_prompt.attribute_pos
275+
prompt.Simplihack_prompt.param_pos
276276
cursor_line
277277
cursor_col
278278
then

hphp/hack/src/client/ide_service/code_actions_services/simplihack/simplihack_interpreter.ml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -293,26 +293,6 @@ end = struct
293293
end
294294

295295
(* Top-level evaluation function for user attributes *)
296-
let eval provider (ua : Tast.user_attribute) method_name =
297-
let open Option.Let_syntax in
296+
let eval provider (e : Tast.expr) =
298297
let ctx = Context.init provider in
299-
(* Extract promptlet and arguments *)
300-
let* (promptlet, args) =
301-
match ua.Aast.ua_params with
302-
| promptlet :: args -> Some (promptlet, args)
303-
| [] -> None
304-
in
305-
(* Evaluate the first argument *)
306-
let promptlet_value = Expr.eval ctx promptlet in
307-
(* If it's a string, return it directly *)
308-
match Value.str promptlet_value with
309-
| Some str -> Some str
310-
| None ->
311-
(* Otherwise, proceed with the original class pointer logic *)
312-
let* promptlet_class = Value.class_ptr promptlet_value in
313-
let* method_ = Context.find_method ~ctx promptlet_class method_name in
314-
(* Evaluate method call *)
315-
let* value =
316-
Call.eval ctx method_ @@ List.map ~f:(fun a -> Aast.Anormal a) args
317-
in
318-
Value.str value
298+
Value.str @@ Expr.eval ctx e

hphp/hack/src/client/ide_service/code_actions_services/simplihack/simplihack_interpreter.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
*
77
*)
88

9-
val eval : Provider_context.t -> Tast.user_attribute -> string -> string option
9+
val eval : Provider_context.t -> Tast.expr -> string option

hphp/hack/src/client/ide_service/code_actions_services/simplihack/simplihack_prompt.ml

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,24 @@
99
open Hh_prelude
1010

1111
type t = {
12-
attribute_pos: Pos.t;
12+
param_pos: Pos.t;
1313
derive_prompt: unit -> string option;
1414
edit_span: Pos.t;
1515
}
1616

17-
let derive_prompt ctx ua method_name () =
18-
Simplihack_interpreter.eval ctx ua method_name
17+
let derive_prompt ctx e () = Simplihack_interpreter.eval ctx e
1918

2019
let find ctx tast =
21-
let user_attribute edit_span method_name acc ua =
20+
let user_attribute edit_span acc ua =
2221
let attribute_name = snd ua.Aast.ua_name in
2322
if
2423
String.equal
2524
attribute_name
2625
Naming_special_names.UserAttributes.uaSimpliHack
2726
then
28-
let attribute_pos = fst ua.Aast.ua_name in
29-
{
30-
attribute_pos;
31-
edit_span;
32-
derive_prompt = derive_prompt ctx ua method_name;
33-
}
34-
:: acc
27+
List.fold ~init:acc ua.Aast.ua_params ~f:(fun acc e ->
28+
let param_pos = snd3 e in
29+
{ param_pos; edit_span; derive_prompt = derive_prompt ctx e } :: acc)
3530
else
3631
acc
3732
in
@@ -47,42 +42,42 @@ let find ctx tast =
4742
let acc = super#on_class_ env cls in
4843
List.fold
4944
~init:acc
50-
~f:(user_attribute cls.Aast.c_span "onClass")
45+
~f:(user_attribute cls.Aast.c_span)
5146
cls.Aast.c_user_attributes
5247

5348
method! on_method_ env meth =
5449
let acc = super#on_method_ env meth in
5550
List.fold
5651
~init:acc
57-
~f:(user_attribute meth.Aast.m_span "onMethod")
52+
~f:(user_attribute meth.Aast.m_span)
5853
meth.Aast.m_user_attributes
5954

6055
method! on_fun_ env func =
6156
let acc = super#on_fun_ env func in
6257
List.fold
6358
~init:acc
64-
~f:(user_attribute func.Aast.f_span "onFunction")
59+
~f:(user_attribute func.Aast.f_span)
6560
func.Aast.f_user_attributes
6661

6762
method! on_class_var env cv =
6863
let acc = super#on_class_var env cv in
6964
List.fold
7065
~init:acc
71-
~f:(user_attribute cv.Aast.cv_span "onField")
66+
~f:(user_attribute cv.Aast.cv_span)
7267
cv.Aast.cv_user_attributes
7368

7469
method! on_typedef env x =
7570
let acc = super#on_typedef env x in
7671
List.fold
7772
~init:acc
78-
~f:(user_attribute x.Aast.t_span "onTypeDef")
73+
~f:(user_attribute x.Aast.t_span)
7974
x.Aast.t_user_attributes
8075

8176
method! on_class_const env x =
8277
let acc = super#on_class_const env x in
8378
List.fold
8479
~init:acc
85-
~f:(user_attribute x.Aast.cc_span "onClassConstant")
80+
~f:(user_attribute x.Aast.cc_span)
8681
x.Aast.cc_user_attributes
8782

8883
method! on_fun_param env x =
@@ -92,35 +87,35 @@ let find ctx tast =
9287
| Some _ ->
9388
List.fold
9489
~init:acc
95-
~f:(user_attribute x.Aast.param_pos "onParameter")
90+
~f:(user_attribute x.Aast.param_pos)
9691
x.Aast.param_user_attributes
9792

9893
method! on_file_attribute env x =
9994
let acc = super#on_file_attribute env x in
10095
List.fold
10196
~init:acc
102-
~f:(user_attribute Pos.none "onFile")
97+
~f:(user_attribute Pos.none)
10398
x.Aast.fa_user_attributes
10499

105100
method! on_tparam env x =
106101
let acc = super#on_tparam env x in
107102
List.fold
108103
~init:acc
109-
~f:(user_attribute (fst x.Aast.tp_name) "onGeneric")
104+
~f:(user_attribute (fst x.Aast.tp_name))
110105
x.Aast.tp_user_attributes
111106

112107
method! on_class_typeconst_def env x =
113108
let acc = super#on_class_typeconst_def env x in
114109
List.fold
115110
~init:acc
116-
~f:(user_attribute x.Aast.c_tconst_span "onTypeConstant")
111+
~f:(user_attribute x.Aast.c_tconst_span)
117112
x.Aast.c_tconst_user_attributes
118113

119114
method! on_module_def env x =
120115
let acc = super#on_module_def env x in
121116
List.fold
122117
~init:acc
123-
~f:(user_attribute x.Aast.md_span "onModule")
118+
~f:(user_attribute x.Aast.md_span)
124119
x.Aast.md_user_attributes
125120
end
126121
in

hphp/hack/src/client/ide_service/code_actions_services/simplihack/simplihack_prompt.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*)
88

99
type t = {
10-
attribute_pos: Pos.t;
10+
param_pos: Pos.t;
1111
derive_prompt: unit -> string option;
1212
edit_span: Pos.t;
1313
}

hphp/hack/test/ide_code_actions/simplihack/simplihack_getter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?hh
2-
<<__SimpliHack(DeriveGetters::class)>>
3-
// ^ at-caret
2+
<<__SimpliHack(DeriveGetters::onClass())>>
3+
// ^ at-caret
44
class SomeClass {
55
public int $one;
66
public string $word;

hphp/hack/test/ide_code_actions/simplihack/simplihack_interp.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?hh
2-
<<__SimpliHack(DeriveGetters::class, "and setters")>>
3-
// ^ at-caret
2+
<<__SimpliHack(DeriveGetters::onClass("and setters"))>>
3+
// ^ at-caret
44
class SomeClass {
55
public int $one;
66
public string $word;

hphp/hack/test/ide_code_actions/simplihack/simplihack_interp_string.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?hh
22
<<__SimpliHack("Direct string value", "additional parameter")>>
3-
// ^ at-caret
3+
// ^ at-caret
44
class SomeClass {
55
public int $one;
66
public string $word;

0 commit comments

Comments
 (0)