camlspotter avatar camlspotter committed bd376ab

small rewrites

Comments (0)

Files changed (7)

+2.0.2
+--------------
+
+- ocamlspot-samewindow: default is now nil. (The author prefers nil!)
+- Cmt.recover_env used too much memory sometimes.
+
+2.0.1
+--------------
+
+- Several elisp bug fixes
+- ocamlspot-samewindow custom elisp var not to split windows at spotting
+
 2.0.0
 --------------
 
 val recover_env : Env.t -> Env.t
 (** Type environments in cmt are simplified and just have env summaries.
     If we want the real environment, we need to recover it from the summary. *)
-val reset_env_cache : unit -> unit
-(** Reset the environment restoration cache *)
   "*Turn on ocamlspot debug output."
   :type 'boolean :group 'ocamlspot)
 
-(defcustom ocamlspot-samewindow t
+(defcustom ocamlspot-samewindow nil
   "Use current window to show the spot."
   :type 'boolean :group 'ocamlspot)
 
     res
 
   let aliases_of_include mexp ids =
-    let sg = try match Mtype.scrape (Cmt.recover_env mexp.mod_env) mexp.mod_type with Mty_signature sg -> sg | _ -> assert false with _ -> assert false in
+    let env' = try Cmt.recover_env mexp.mod_env with e -> 
+      Format.eprintf "recover_env: %s@." (Printexc.to_string e);
+      assert false 
+    in 
+    let sg = try match Mtype.scrape env' mexp.mod_type with 
+      | Mty_signature sg -> sg 
+      | _ -> prerr_endline "strange!";assert false 
+      with _ -> assert false 
+    in
     aliases_of_include' true sg ids
 
   let rec module_expr mexp =
     (* CR jfuruse: aaa.mll creates cmt with aaa.ml as source, but
        aaa.ml is often removed by the build system.
     *)
-    let stat_cmt = Unix.stat cmt in
+    let stat_cmt = try Unix.stat cmt with _ -> assert false in
     try
       let stat_source = Unix.stat source in
         (* Needs = : for packed modules, .cmt and the source .cmo are written 
         true
 
   let find_alternative_source ~cmt source =
-      (* if [source] is not found, we try finding files with the same basename
-         in
+      (* if [source] is not found, we try finding files with the same basename in
          - the directory of [cmt]
          - the directory of [cmt] points to (if [cmt] is symlink)
        *)
     let source_dirs =
         Filename.dirname cmt ::
         begin 
-          let stat_cmt = Unix.lstat cmt in
+          let stat_cmt = try Unix.lstat cmt with _ -> assert false in
           if stat_cmt.Unix.st_kind = Unix.S_LNK then
-            [ Filename.dirname (Unix.readlink cmt) ]
+            [ Filename.dirname (try Unix.readlink cmt with _ -> assert false) ]
           else []
         end
-      in
-      List.find Sys.file_exists 
-        (List.map (fun d -> d ^/ source_base) source_dirs)
+    in
+    List.find Sys.file_exists 
+      (List.map (fun d -> d ^/ source_base) source_dirs)
 
   let load_cmt_file file = snd (Cmt_format.read file)
 
   let load_directly path : Unit.t =
     Debug.format "cmt loading from %s@." path;
     match load_cmt_file path with
-    | Some cmt -> 
-        Spot.Unit.of_file (Spot.File.of_cmt path cmt)
+    | Some cmt -> Spot.Unit.of_file (Spot.File.of_cmt path cmt)
     | None -> failwithf "load_directly failed: %s" path
 
   exception Old_cmt of string (* cmt *) * string (* source *)
   (* CR jfuruse: exception *)
   (* CRv2 jfuruse: add and check cache time stamp *)
   let load_directly_with_cache : string -> Unit.t = 
-    let cache = Hashtbl.create 17 in
-    fun path ->
-      try 
-        Hashtbl.find cache path
+    Hashtbl.memoize (Hashtbl.create 17 ) (fun path ->
+      try
+        let file = load_directly path in
+        if not (check_time_stamp ~cmt:path file.Unit.path) then 
+          if Spotconfig.strict_time_stamp then 
+            raise (Old_cmt (path, file.Unit.path))
+          else
+            eprintf "Warning: source %s is newer than the cmt@." file.Unit.path;
+        file
       with
       | Not_found ->
-          try
-            let file = load_directly path in
-            if not (check_time_stamp ~cmt:path file.Unit.path) then 
-              if Spotconfig.strict_time_stamp then 
-                raise (Old_cmt (path, file.Unit.path))
-              else
-                eprintf "Warning: source %s is newer than the cmt@." file.Unit.path;
-            Hashtbl.replace cache path file;
-            file
-          with
-          | Not_found ->
-              failwithf "failed to find cmt file %s" path
+          failwithf "failed to find cmt file %s" path)
 
   let find_in_path load_paths body ext =
     let body_ext = body ^ ext in
     let find_in_path load_paths name = 
-      Debug.format "@[<2>searching %s in@ pwd=%s@ paths=[@[%a@]]@]@." 
+      Debug.format "@[<2>find_in_path: searching %s in@ pwd=%s@ paths=[@[%a@]]@]@." 
         name
         (Sys.getcwd ())
         (Format.list "; " (fun ppf x -> fprintf ppf "%S" x)) 
 
 module Hashtbl = struct
   include Hashtbl
+
   let of_list size kvs =
     let tbl = Hashtbl.create size in
     List.iter (fun (k,v) ->
       Hashtbl.replace tbl k v) kvs;
     tbl
+
+  let memoize tbl f k =
+    try 
+      Hashtbl.find tbl k 
+    with
+    | Not_found ->
+        let v = f k in
+        Hashtbl.replace tbl k v;
+        v
 end
 
 module Hashset = struct
 module Hashtbl : sig
   include module type of Hashtbl with type ('a,'b) t = ('a, 'b) Hashtbl.t
   val of_list : int -> ('a * 'b) list -> ('a, 'b) Hashtbl.t
+  val memoize : ('a, 'b) Hashtbl.t -> ('a -> 'b) -> 'a -> 'b
 end
 
 module Hashset : sig
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.