Skip to content

Commit

Permalink
Completed ident retrieving and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiphaine Turpin authored and Tiphaine Turpin committed Jul 11, 2011
1 parent 4e32034 commit bbc3bd0
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 48 deletions.
37 changes: 28 additions & 9 deletions ocamlwizard/README
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ containing <file.ml>, or its parent directories, back to the root.
This file must contain exactly one directory name per line, and This file must contain exactly one directory name per line, and
compiled interface will be searched in this directories. Relative compiled interface will be searched in this directories. Relative
directory names are interpreted with respect to the directory directory names are interpreted with respect to the directory
containing the project file .ocamlwizard. containing the project file .ocamlwizard. See ../.ocamlwizard for an
example.






Expand Down Expand Up @@ -109,20 +110,38 @@ the test cases for a detailed overview of the current behavior.
Refactoring: Refactoring:
............ ............


- Renaming (C-c C-o r): rename a value identifier through a single .ml - Renaming (C-c C-o r): rename an identifier through a single .ml or
file (other sorts of idents are not yet implemented). The cursor .mli file. The cursor must be placed on the *definition* of the
must be placed on the *definition* of the value (typically, a let value (for example, a let binding or a pattern). Renaming takes care
binding or a pattern). Renaming takes care of necessary propagation of necessary propagation (e.g., when distinct values with the same
(e.g., when distinct values with the same name need to be renamed name need to be renamed consistently because this name appears in a
consistently because this name appears in a common interface), and common interface), and capture is detected. Undoing works in emacs.
capture is detected. Backups are made in case anything goes wrong. Backups are made in case anything goes wrong.


Renaming is implemented for: values, types, modules (non-recursive),
module types, fields, constructors, and exceptions. Objects are not
supported. Type variables, argument labels, and polymorphic variants
are not supported either.

The replacement is intended to be complete in the current file, up
to the following known limitations:
- labels, e.g. let x = .. in f ~x
- fields, e.g. let x = .. in {x} or match .. with {x} -> ..
- or-patterns, e.g. match .. with [], x | x, [] -> ..




- Command line interface - Command line interface
~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~


Needs to be redefined. The following conventions are common to all queries:
- stdin is ignored,
- stderr is used for debug purpose and should be discarded by editors.
- possible exit codes are:
- 0: normal completion, with result is written on stdout
- 1: "controlled" failure, with an explanation message on stdout
- 2: unexpected error, with an exception backtrace printed on stdout.

Command line arguments needs to be redesigned.




- Internal working overview - Internal working overview
Expand Down
3 changes: 1 addition & 2 deletions ocamlwizard/TODO
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
- Grep: should be easy to implement using rename - Grep: should be easy to implement using rename
- Rename: return a list of files that emacs should revert - Rename: return a list of files that emacs should revert
- Rename: check that we always lookup in the right environment - Rename: check that we always lookup in the right environment
- Rename: check that all Ident.t s in typedtree are processed
- Rename: rename type variables, argument labels, polymorphic variants, methods - Rename: rename type variables, argument labels, polymorphic variants, methods
- Rename: propagate more constraints (constrs, fields, exceptions) - Rename: test propagation for constrs, fields, exceptions...
- Rename: collect all occurrences in a or-pattern - Rename: collect all occurrences in a or-pattern
- Rename: retrieve a definition from an occurrence - Rename: retrieve a definition from an occurrence
- Rename: warn about potential future captures - Rename: warn about potential future captures
Expand Down
69 changes: 47 additions & 22 deletions ocamlwizard/refactor/findName.ml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -245,34 +245,60 @@ let locate_field loc pid ploc fs tfs =
in in
pid f pid f


(* Should be almost complete for expressions, but this is not a safety let locate_id loc pid ploc decls =
requirement anyway. *) pid
let locate_renamed_id s loc = (List.find
(function d -> contains (ploc d) loc)
decls)

let find_id_def table id =
StringTbl.find table (Ident.name id)

(* Missing: Tstr_class_type, class_infos, Tmeth_val, cstr_meths,
Tcf_inher, Tcf_val, Tcf_let *)
let locate_renamed_id table loc =
let open Env in let open Env in
locate_map `innermost locate_map `innermost
(function n -> (function n ->
try Some (match n with try Some (match n with

(* Values *)
| `pattern {pat_desc = Tpat_var id} | `pattern {pat_desc = Tpat_var id}
| `pattern {pat_desc = Tpat_alias (_, TPat_alias id)}
| `expression {exp_desc = Texp_for (id, _, _, _, _)} | `expression {exp_desc = Texp_for (id, _, _, _, _)}
| `signature_item {sig_desc = Tsig_value (id, _)} | `structure_item {str_desc = Tstr_primitive (id, _)}
-> Value, id | `signature_item {sig_desc = Tsig_value (id, _)} -> Value, id

| `class_expr {cl_desc =
Tcl_fun (_, _, bs, _, _) | Tcl_let (_, _, bs, _)} ->
Value, locate_id loc fst (function id, _ -> find_id_def table id) bs

(* Modules *)
| `structure_item {str_desc = Tstr_module (id, _)} | `structure_item {str_desc = Tstr_module (id, _)}
| `signature_item {sig_desc = Tsig_module (id, _)} | `signature_item {sig_desc = Tsig_module (id, _)}
| `module_expr {mod_desc = Tmod_functor (id, _, _)} | `module_expr {mod_desc = Tmod_functor (id, _, _)}
| `module_type {mty_desc = Tmty_functor (id, _, _)} | `module_type {mty_desc = Tmty_functor (id, _, _)}
-> Module, id | `expression {exp_desc = Texp_letmodule (id, _, _)} -> Module, id

| `structure_item {str_desc = Tstr_recmodule mods} ->
Module,
locate_id loc (function id, _, _ -> id)
(function id, _, _ -> find_id_def table id) mods

| `signature_item {sig_desc = Tsig_recmodule mods} ->
Module,
locate_id loc fst (function id, _ -> find_id_def table id) mods

(* Module types *)
| `structure_item {str_desc = Tstr_modtype (id, _)} | `structure_item {str_desc = Tstr_modtype (id, _)}
| `signature_item {sig_desc = Tsig_modtype (id, _)} | `signature_item {sig_desc = Tsig_modtype (id, _)} -> Modtype, id
-> Modtype, id
(* Types *)
| `structure_item {str_desc = Tstr_type types} | `structure_item {str_desc = Tstr_type types}
| `signature_item {sig_desc = Tsig_type types} | `signature_item {sig_desc = Tsig_type types} ->
-> Type, locate_id loc fst (function _, d -> d.typ_loc) types
let id, _ =
List.find (* Constructors, fields, and exceptions *)
(function id, d -> contains d.typ_loc loc)
types
in
Type, id
| `type_declaration d -> | `type_declaration d ->
(match d.typ_type.type_kind, d.typ_kind with (match d.typ_type.type_kind, d.typ_kind with
| Type_variant cs, Ttype_variant tcs -> | Type_variant cs, Ttype_variant tcs ->
Expand All @@ -285,12 +311,11 @@ let locate_renamed_id s loc =
fs tfs fs tfs
| Type_abstract, Ttype_abstract -> raise Not_found | Type_abstract, Ttype_abstract -> raise Not_found
| _ -> assert false) | _ -> assert false)

| `structure_item {str_desc = Tstr_exception (id, _)} | `structure_item {str_desc = Tstr_exception (id, _)}
| `signature_item {sig_desc = Tsig_exception (id, _)} -> | `structure_item {str_desc = Tstr_exn_rebind (id, _)}
Constructor, id | `signature_item {sig_desc = Tsig_exception (id, _)} -> Constructor, id

| _ -> raise Not_found) | _ -> raise Not_found)
with Not_found -> None) with Not_found -> None)
loc s loc

let find_id_def table id =
StringTbl.find table (Ident.name id)
2 changes: 1 addition & 1 deletion ocamlwizard/refactor/findName.mli
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ val get_lids :
(Location.t * Longident.t * (Env.t * Env.path_sort)) list (Location.t * Longident.t * (Env.t * Env.path_sort)) list


val locate_renamed_id : val locate_renamed_id :
Location.string_table -> int * int ->
[ `signature of Typedtree.signature | `structure of Typedtree.structure ] -> [ `signature of Typedtree.signature | `structure of Typedtree.structure ] ->
int * int ->
Env.path_sort * Ident.t Env.path_sort * Ident.t


val find_id_def : val find_id_def :
Expand Down
5 changes: 4 additions & 1 deletion ocamlwizard/refactor/rename.ml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ let rename loc name' file =
let s, idents, lidents, paths = read_typedtree source_kind typedtree_file in let s, idents, lidents, paths = read_typedtree source_kind typedtree_file in


(* Get the "initial" id to rename and its sort *) (* Get the "initial" id to rename and its sort *)
let renamed_kind, id = locate_renamed_id s loc in let renamed_kind, id =
try locate_renamed_id idents loc s
with Not_found -> fail_owz "Cannot rename anything here"
in
let name = Ident.name id in let name = Ident.name id in


let name' = fix_case renamed_kind name' in let name' = fix_case renamed_kind name' in
Expand Down
2 changes: 1 addition & 1 deletion ocamlwizard/test/expected/errors_res.ml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let r = {a = () ; b = ()}
let () = match [] with$ let () = match [] with$
=> =>
let () = match [] withError: File "completion/completion.ml", line 150, characters 19-25: Assertion failed let () = match [] withError: File "completion/completion.ml", line 128, characters 19-25: Assertion failed
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF
Expand Down
4 changes: 2 additions & 2 deletions ocamlwizard/test/expected/expansion_res.ml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ let () = match () with $fo€o -> ()
=> =>
let () = match () with Error: Not_found let () = match () with Error: Not_found
Raised at file "common/typedtreeOps.ml", line 181, characters 10-19 Raised at file "common/typedtreeOps.ml", line 181, characters 10-19
Called from file "completion/completion.ml", line 120, characters 3-85 Called from file "completion/completion.ml", line 98, characters 3-85
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF
Expand All @@ -64,7 +64,7 @@ let () = match None with Some $_€ -> ()
let () = match None with Some Error: var let () = match None with Some Error: var
Raised at file "pervasives.ml", line 22, characters 22-33 Raised at file "pervasives.ml", line 22, characters 22-33
Called from file "completion/extraction/proposal_extraction.ml", line 281, characters 11-25 Called from file "completion/extraction/proposal_extraction.ml", line 281, characters 11-25
Called from file "completion/completion.ml", line 161, characters 14-56 Called from file "completion/completion.ml", line 139, characters 14-56
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF
Expand Down
12 changes: 6 additions & 6 deletions ocamlwizard/test/expected/match_cases_res.ml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ let () = match `a with `b -> ()$
let () = match `a with `b -> ()Error: Error while typing let () = match `a with `b -> ()Error: Error while typing
Raised at file "pervasives.ml", line 22, characters 22-33 Raised at file "pervasives.ml", line 22, characters 22-33
Called from file "completion/completion.ml", line 62, characters 6-35 Called from file "completion/completion.ml", line 62, characters 6-35
Called from file "completion/completion.ml", line 100, characters 26-48 Called from file "completion/completion.ml", line 82, characters 26-48
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF
Expand All @@ -140,14 +140,14 @@ let () = match {a = () ; b = ()} with


let (_ : unit -> 'a) = function $ let (_ : unit -> 'a) = function $
=> =>
let (_ : unit -> 'a) = function Error: File "completion/completion.ml", line 150, characters 19-25: Assertion failed let (_ : unit -> 'a) = function Error: File "completion/completion.ml", line 128, characters 19-25: Assertion failed
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF


let () = match () with | $ let () = match () with | $
=> =>
let () = match () with | Error: File "completion/completion.ml", line 150, characters 19-25: Assertion failed let () = match () with | Error: File "completion/completion.ml", line 128, characters 19-25: Assertion failed
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF
Expand All @@ -156,14 +156,14 @@ let () = match true with true -> $
=> =>
let () = match true with true -> Error: Not_found let () = match true with true -> Error: Not_found
Raised at file "common/typedtreeOps.ml", line 181, characters 10-19 Raised at file "common/typedtreeOps.ml", line 181, characters 10-19
Called from file "completion/completion.ml", line 120, characters 3-85 Called from file "completion/completion.ml", line 98, characters 3-85
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF


let () = match true with true -> () | $ let () = match true with true -> () | $
=> =>
let () = match true with true -> () | Error: File "completion/completion.ml", line 150, characters 19-25: Assertion failed let () = match true with true -> () | Error: File "completion/completion.ml", line 128, characters 19-25: Assertion failed
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF
Expand All @@ -183,7 +183,7 @@ let () = match `a with `b -> ()$ (* because it doesn't type... *)
let () = match `a with `b -> ()Error: Error while typing let () = match `a with `b -> ()Error: Error while typing
Raised at file "pervasives.ml", line 22, characters 22-33 Raised at file "pervasives.ml", line 22, characters 22-33
Called from file "completion/completion.ml", line 62, characters 6-35 Called from file "completion/completion.ml", line 62, characters 6-35
Called from file "completion/completion.ml", line 100, characters 26-48 Called from file "completion/completion.ml", line 82, characters 26-48
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF
Expand Down
8 changes: 4 additions & 4 deletions ocamlwizard/test/expected/path_res.ml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ let () = so $ (* this does not work, purposely *)
let () = so Error: Error while typing let () = so Error: Error while typing
Raised at file "pervasives.ml", line 22, characters 22-33 Raised at file "pervasives.ml", line 22, characters 22-33
Called from file "completion/completion.ml", line 62, characters 6-35 Called from file "completion/completion.ml", line 62, characters 6-35
Called from file "completion/completion.ml", line 100, characters 26-48 Called from file "completion/completion.ml", line 82, characters 26-48
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF
Expand Down Expand Up @@ -143,21 +143,21 @@ EOF


let _ = {a = () $ let _ = {a = () $
=> =>
let _ = {a = () Error: File "completion/completion.ml", line 149, characters 17-23: Assertion failed let _ = {a = () Error: File "completion/completion.ml", line 127, characters 17-23: Assertion failed
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF


let _ = {a = () ; M$ let _ = {a = () ; M$
=> =>
let _ = {a = () ; MError: File "completion/completion.ml", line 150, characters 19-25: Assertion failed let _ = {a = () ; MError: File "completion/completion.ml", line 128, characters 19-25: Assertion failed
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF


let _ = {r $ let _ = {r $
=> =>
let _ = {r Error: File "completion/completion.ml", line 149, characters 17-23: Assertion failed let _ = {r Error: File "completion/completion.ml", line 127, characters 17-23: Assertion failed
Called from file "main/ocamlwizard.ml", line 90, characters 14-32 Called from file "main/ocamlwizard.ml", line 90, characters 14-32
Called from file "main/ocamlwizard.ml", line 61, characters 6-10 Called from file "main/ocamlwizard.ml", line 61, characters 6-10
EOF EOF
Expand Down
5 changes: 5 additions & 0 deletions ocamlwizard/test/expected/renameLetModule_rres.ml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
let _ =
let module Y = struct let x = () end in
Y.x
Renamed 1 definition(s) and 1 reference(s)

5 changes: 5 additions & 0 deletions ocamlwizard/test/renameLetModule_rres.ml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
let _ =
let module Y = struct let x = () end in
Y.x
Renamed 1 definition(s) and 1 reference(s)

14 changes: 14 additions & 0 deletions ocamlwizard/test/renameModuleRec_rres.ml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,14 @@
module rec M : sig val x : unit end = struct let x = () end
and N : sig end = struct let y = M.x end
Error: different environments
Raised at file "pervasives.ml", line 22, characters 22-33
Called from file "refactor/findName.ml", line 196, characters 24-57
Called from file "refactor/findName.ml", line 217, characters 21-33
Called from file "hashtbl.ml", line 157, characters 23-35
Called from file "hashtbl.ml", line 161, characters 12-33
Called from file "refactor/findName.ml", line 238, characters 27-62
Called from file "refactor/rename.ml", line 307, characters 6-79
Called from file "main/ocamlwizard.ml", line 96, characters 31-59
Called from file "main/ocamlwizard.ml", line 61, characters 6-10


0 comments on commit bbc3bd0

Please sign in to comment.