Permalink
Browse files

Hack `--ide-refactor` support for class constants

Summary:
Added rename refactor support for class constants in Hack.

Quick Explanation for the change in `findRefsService.ml`:

Since identifying constants across the workspace is already supported in Find-References, adding rename support for class constants was initially a matter of just supplying the right types. Furthermore, although Find-References tends to ignore the definition of constants, methods, classes, etc.. we're usually able to include the definitions anyways when calling its services from the refactor command call by simply passing in the appropriate boolean value.

However, in this case, finding and including the definition of constants hadn't yet been implemented in `findRefsService.ml`. My edits implement this so that when the user renames a constant, the constant is renamed at its definition as well.

NOTE: Currently, renaming constants doesn't work for cases where it is being referenced with `self` or `static` (i.e. `static::CONSTANT`). I've put a task out for this and will try to revisit this issue later.

Reviewed By: arxanas

Differential Revision: D8758310

fbshipit-source-id: 3dd8feeda4d366bf3b350918ba4097e6e4974548
  • Loading branch information...
ConderS authored and hhvm-bot committed Jul 10, 2018
1 parent 564e3ff commit 7ff4f95e62f336850d426f540bdf808c087a976c
@@ -214,6 +214,20 @@ let get_definitions tcopt = function
acc
| None -> acc
end
| IMember (Class_set classes, Class_const class_const_name) ->
SSet.fold classes ~init:[] ~f:begin fun class_name acc ->
match Typing_lazy_heap.get_class tcopt class_name with
| Some class_ ->
let add_class_const class_consts acc = match SMap.get class_consts class_const_name with
| Some class_const when class_const.cc_origin = class_.tc_name ->
let pos = class_const.cc_pos in
(class_const_name, pos) :: acc
| _ -> acc
in
let acc = add_class_const class_.tc_consts acc in
acc
| None -> acc
end
| IClass class_name ->
Option.value ~default:[] begin Naming_heap.TypeIdHeap.get class_name >>=
function (_, `Class) -> Typing_lazy_heap.get_class tcopt class_name >>=
@@ -228,7 +242,7 @@ let get_definitions tcopt = function
end
| IGConst _
| IMember (Subclasses_of _, _)
| IMember (_, (Property _ | Class_const _ | Typeconst _)) ->
| IMember (_, (Property _ | Typeconst _)) ->
(* this code path is used only in ServerRefactor, we can update it at some
later time *)
[]
@@ -25,6 +25,9 @@ let go action genv env =
let find_refs_action, new_name = match action with
| ClassRename (old_name, new_name) ->
Types.Class old_name, new_name
| ClassConstRename (class_name, old_name, new_name) ->
Types.Member (class_name, Types.Class_const old_name),
new_name
| MethodRename (class_name, old_name, new_name) ->
Types.Member (class_name, Types.Method old_name),
new_name
@@ -61,6 +64,10 @@ let go_ide (filename, line, char) new_name genv env =
let command =
ServerRefactorTypes.ClassRename (class_name, new_name) in
go command genv env
| Const, [class_name; const_name] ->
let command =
ServerRefactorTypes.ClassConstRename (class_name, const_name, new_name) in
go command genv env
| Method, [class_name; method_name] ->
let command =
ServerRefactorTypes.MethodRename (class_name, method_name, new_name) in
@@ -19,6 +19,8 @@ and insert_patch = {
type action =
| ClassRename of string * string (* old_name * new_name *)
| ClassConstRename of string * string * string
(* class_name * old_name * new_name *)
| MethodRename of string * string * string
(* class_name * old_name * new_name*)
| FunctionRename of string * string (* old_name * new_name *)
Oops, something went wrong.

0 comments on commit 7ff4f95

Please sign in to comment.