Permalink
Browse files

Emacs code for renaming (no backup !)

  • Loading branch information...
Tiphaine Turpin Tiphaine Turpin
Tiphaine Turpin authored and Tiphaine Turpin committed Jul 4, 2011
1 parent a4ae8bf commit 6a9e724404fb12c17fdf8a182fcd042f6bffe16c
View
@@ -15,7 +15,9 @@ have to:
- change the install dir with by giving the configure script a
--prefix option
- manually copy the file emacs/ocamlwizard.el into your ~/.emacs.d/
-- append the contents of emacs/52ocamlwizard.el to your ~/.emacs
+- add the following to your ~/.emacs:
+ (add-to-list 'load-path "~/.emacs.d")
+ <contents of emacs/52ocamlwizard.el>
Compilation and installation
View
@@ -1,3 +1,4 @@
+- Rename: return a list of files that emacs should revert
- Rename: replace in multiple files
- Rename: check that we always lookup in the right environment
@@ -150,7 +150,49 @@
(delete-char 1)
)
-
+(defun ocamlwizard-rename (name)
+ "rename a value using Ocamlwizard"
+ (interactive "sRename with: ")
+ (setq pos (point))
+ (save-excursion
+ (search-backward-regexp "[^a-zA-Z._0-9]")
+ (forward-char 1)
+ (setq start (point))
+ (search-forward-regexp "[^a-zA-Z._0-9]")
+ (setq end (- (point) 1))
+ (setq word (buffer-substring start end)))
+; (interactive (concat "sRename " word))
+; (message "New name: %s" name)
+ (setq file (buffer-name))
+ (setq buffer (get-buffer-create "*ocamlwizard*"))
+ (save-excursion
+ (set-buffer buffer)
+ (compilation-minor-mode 1)
+ (erase-buffer)
+ (insert "\n\n"))
+ (do-auto-save)
+ (setq exit-status
+ (call-process
+ "ocamlwizard" nil (list t nil) nil
+ "completion" "refactor" "-rename"
+ (concat (int-to-string (- start 1)) "-" (int-to-string (- end 1)))
+ word
+ name
+ file))
+ (if (not (eq exit-status 10))
+ (message "ocamlwizard: no completion"))
+; Thank you stackoverflow:
+ (save-excursion
+ (clear-visited-file-modtime)
+ (widen)
+ (delete-region (point-min) (point-max))
+ (insert-file-contents (buffer-file-name)))
+; Problem: if we undo and then redo, emacs forgets the goto.
+ (goto-char pos)
+ (set-buffer-modified-p nil)
+ (set-visited-file-modtime)
+ )
+
(defun ocamlwizard ()
(interactive)
(define-key (current-local-map) [f1] 'ocamlwizard-locate)
@@ -29,6 +29,7 @@ type occurrence_kind = [
| `str_open
| `sig_open
| `mty_with
+| `mty_ident
]
(* This should only be complete w.r.t. values and module paths ! But
@@ -66,6 +67,12 @@ module Occurrence =
| _ -> ()
+ let enter_module_type t =
+ match t.mty_desc with
+ | Tmty_ident p -> found t.mty_loc (assert false) `mty_ident
+
+ | _ -> ()
+
let enter_structure_item i =
match i.str_desc with
| Tstr_open _ -> found i.str_loc (assert false) `str_open
@@ -142,29 +149,23 @@ let get_occurrences s =
*)
let ident_of_subtree = function
- | `pattern {pat_desc = Tpat_var id ; pat_loc = loc}
- | `expression {exp_desc = Texp_for (id, _, _, _, _) ; exp_loc = loc}
- | `signature_item {sig_desc = Tsig_value (id, _) ; sig_loc = loc}
- -> value_ops, id, loc
- | `structure_item {str_desc = Tstr_module (id, _) ; str_loc = loc}
- -> module_ops, id, loc
+ | `pattern {pat_desc = Tpat_var id}
+ | `expression {exp_desc = Texp_for (id, _, _, _, _)}
+ | `signature_item {sig_desc = Tsig_value (id, _)}
+ -> value_ops, id
+ | `structure_item {str_desc = Tstr_module (id, _)}
+ -> module_ops, id
+ | `structure_item {str_desc = Tstr_modtype (id, _)}
+ -> modtype_ops, id
| _ -> raise Not_found
(* Should be almost complete for expressions, but this is not a safety
requirement anyway. *)
let locate_renamed_id s loc =
try
- let kind, id, _ = ident_of_subtree (locate_innermost s loc) in kind, id
+ let kind, id = ident_of_subtree (locate_innermost s loc) in kind, id
with Not_found ->
invalid_arg "rename"
-let find_id_def s id =
- find_map_innermost s
- (function t ->
- try
- let _, id', loc = ident_of_subtree t in
- if id' = id then Some loc else None
- with Not_found -> None)
-
let find_id_def table id =
StringTbl.find table (Ident.name id)
@@ -214,8 +214,10 @@ let find_id_defs ids name s =
[fst loc, snd loc, name']
*)
-(* TODO *)
-let valid_ident kind name = true
+let fix_case kind =
+ match kind.sort with
+ | `Module | `Modtype -> String.capitalize
+ | _ -> String.uncapitalize
(* Temporary : we rename only in one file *)
let rename loc name name' file =
@@ -224,6 +226,8 @@ let rename loc name name' file =
(* Get the "initial" id to rename and its sort *)
let renamed_kind, id = locate_renamed_id (`structure s) loc in
+ let name' = fix_case renamed_kind name' in
+
(* Collect constraints requiring simultaneous renaming *)
let incs, includes = collect_signature_inclusions s in
@@ -7,7 +7,7 @@ R_CASES=renameSimple.ml renameSigNewCaptured.ml renameSigOldCaptured.ml \
renameIncludeNewCaptured.ml renameIncludeOldCaptured.ml \
renameOpenNewCaptured.ml renameOpenOldCaptured.ml \
renameMultiple.ml renameProp.ml renamePropFunctor.ml \
- renamePropFunctorNoApp.ml
+ renamePropFunctorNoApp.ml renameModtype.ml
R_TESTS=$(subst .ml,_rres.ml, $(R_CASES))
RES=match_cases_res.ml expansion_res.ml path_res.ml errors_res.ml \
@@ -0,0 +1,6 @@
+(* Does not work because modtypes don't have environments. *)
+module type $M= sig end
+
+module type N = M
+
+module M : M = struct end
@@ -0,0 +1,7 @@
+module M = struct
+ let $x€ = 1
+end
+
+module N = (M : sig val x : int end)
+
+let _ = N.x
@@ -0,0 +1,11 @@
+module M = struct
+ let $x€ = 1
+end
+
+module F(X : sig val x : int end) = struct let z = X.x end
+
+module N = F(struct let x = 2 let tot = () end)
+
+module O = F(M)
+
+let _ = M.x
@@ -0,0 +1,9 @@
+module M = struct
+ let $x€ = 1
+end
+
+module F(X : sig val x : int end) = struct let z = X.x end
+
+module N = F(struct let x = 2 let tot = () end)
+
+let _ = M.x

0 comments on commit 6a9e724

Please sign in to comment.