Commits

camlspotter  committed 2b713d1

cleanup and fix for semi in match case

  • Participants
  • Parent commits d6bf8c4
  • Branches dev

Comments (0)

Files changed (2)

 
 module State = struct
   type t = {
-    modified : bool; (* Has the source code ever modified ? *)
+    modified : bool;   (* Has the source code ever modified ? *)
     orig_indent : int; (* The original indent of the current line *)
     last_indent : int; (* At line head, first pass: the fixed indent of the last line
                           At line head, snd pass: the fixed indent of the current line
       | GREATERRBRACKET|GREATERRBRACE|FUNCTOR|FUNCTION|FUN|FOR|FALSE
       | EXTERNAL|EXCEPTION|EOF|END|DOTDOT|DOT|DONE|CONSTRAINT|COLONGREATER
       | COLON|CLASS|BARRBRACKET|BAR|BANG|BACKQUOTE|ASSERT|AS|AND
-       -> true
+        -> true
 
       | _ -> false
 
         let rec f bases = match bases with
           | { k = (KParen cols | KBegin cols | KBracket cols | KBrace cols | KLet (_, cols)) | KTry cols | KMatch cols | KFun cols | KFunction cols } :: _ ->
               fix cols, bases
+          | { k = KCaseArrow } :: _ -> bases, bases
           | { k = (KThen cols | KElse cols ) } :: bs ->
               fix cols, bs
-          | { k = KExpr cols } :: bs ->
-              fix cols, bs
           | [] -> bases0, bases0 (* if overrun, keep the original *)
           | _ :: bs -> f bs
         in
   { state with bases = pre_bases },
   { state with bases = post_bases }
 
-let update_state state new_line fix_indent str orig_region t =
+let update_state state ~at_new_line ~fix_indent str orig_region t =
 
   (* If it is a new line + we need to fix the indent of the line, compute the line's indentation.
      Otherwise, we use the original state.
   *)
   let state' =
-    if fix_indent && new_line then
-      let pre, _ = token state str new_line orig_region t in
+    if fix_indent && at_new_line then
+      let pre, _ = token state str at_new_line orig_region t in
       { pre with last_indent = State.indent pre }
     else
       state
   let state = { state
                 with
                 last_indent = state'.last_indent
-              ; modified = state'.modified || orig_region <> fixed_region
+                ; modified = state'.modified || orig_region <> fixed_region
               }
   in
 
   (* Rerun [token] *)
-  token state str new_line fixed_region t
+  token state str at_new_line fixed_region t
 open Pos
-open Machine
 open Args
+module State = Machine.State
+module Stack = Machine.Stack
 
 module Sexp = Sexplib.Sexp (* No open Sexplib, since Parser corrides with Sexplib.Parser *)
 
 
 module Printer : sig
   type t = (int * int) option
-  val add_debug  : t -> int-> string -> unit
-  val add_string : t -> int -> string -> unit
+  val print_debug  : t -> int-> string -> unit
+  val print_string : t -> int -> string -> unit
 end = struct
 
   type t = (int * int) option
 
-  let add_debug t lnum s = match check_line lnum t with
+  let print_debug t lnum s = match check_line lnum t with
     | `Before | `Over -> ()
     | `Inside -> print_string s
 
       If the printing text exceeds the interested region,
       It stops printing.
 
-      Currently it cannot print a string correctly if it has 
-      different number of lines from the original. 
+      Currently it cannot print a string correctly if it has
+      different number of lines from the original.
   *)
-  let add_string t lnum s =
+  let print_string t lnum s =
     let add_line t lnum s = match check_line lnum t with
       | `Before -> ()
       | `Over -> raise Exit
 open Tokenstr
 
 let indent_file path =
+  let print_string = Printer.print_string lines in
+  let print_debug = Printer.print_debug lines in
+
   let str = if path = "" then Tokenstr.of_channel stdin else Tokenstr.of_path path in
 
   let flush_remaining_space info =
-    ignore (Printer.add_string lines (Region.lnum (fst info.space)) (snd info.space))
+    print_string (Region.lnum (fst info.space)) (snd info.space)
   in
 
   let rec loop last_orig_region state str = match Tokenstr.destr str with
     | None -> state
 
-    | Some (({ token = Parser.EOF } as info), _) -> 
+    | Some (({ token = Parser.EOF } as info), _) ->
         flush_remaining_space info;
         state
 
-    | Some (({ token = t; 
-               region = orig_region; 
-               space = (_space_between_region, space_between); 
+    | Some (({ token = t;
+               region = orig_region;
+               space = (_space_between_region, space_between);
                substr } as info), str) ->
 
         match check_line (Region.lnum orig_region) lines with
-        | `Over -> 
-            (* The token is outside of our interest. 
+        | `Over ->
+            (* The token is outside of our interest.
                Print the remaining things and go out *)
             flush_remaining_space info;
             state
-        | (`Before | `Inside as line_status) -> 
+        | (`Before | `Inside as line_status) ->
             (*
               Format.eprintf "<%s %d>@."
             *)
 
             let last_line = Region.lnum last_orig_region in
             let current_line = Region.lnum orig_region in
-            let new_line = last_line <>  current_line in (* Is this token at a new line? *)
-    
+            let at_new_line = last_line <>  current_line in (* Is this token at a new line? *)
+
+            (* update the original indent if [at_new_line] *)
+            let state =
+              if at_new_line then { state with State.orig_indent = Region.columns orig_region }
+              else state
+            in
+
             (* Where the cursor move *)
             let _cursor_info = match cursor with
               | None -> None
               | Some lines_cols ->
                   match
-                    Pos.Region.contain_lines_cols last_orig_region lines_cols,
-                    Pos.Region.contain_lines_cols orig_region lines_cols
+                    Region.contain_lines_cols last_orig_region lines_cols,
+                    Region.contain_lines_cols orig_region lines_cols
                   with
-                  | (`In | `Right), _ -> None (* far past *)
-                  | `Left, `Right -> None     (* far future *)
-                  | `Left, `Left ->           (* cursor in the space_between *)
-                      Some `In_space_between 
-                  | `Left, `In ->             (* cursor on the token *)
-                      Some `In_the_token
-                  | _ -> assert false         (* It must be more tolerant, but for now... *)
-            in
-
-            let state =
-              if new_line then { state with State.orig_indent = Region.columns orig_region } else state
+                  | (`In | `Right), _ -> None                   (* far past *)
+                  | `Left, `Right     -> None                   (* far future *)
+                  | `Left, `Left      -> Some `In_space_between (* cursor in the space_between *)
+                  | `Left, `In        -> Some `In_the_token     (* cursor on the token *)
+                  | _                 -> assert false           (* It must be more tolerant, but for now... *)
             in
 
             let fix_indent = match line_status with
               | `Over -> assert false
             in
 
-            let pre, post = update_state state new_line fix_indent str orig_region t in
-            
+            let pre, post = Machine.update_state state ~at_new_line ~fix_indent str orig_region t in
+
             (* printing *)
 
-            if new_line then begin
+            if not at_new_line then print_string current_line space_between
+            else begin
               (* the line 1 has no previous new line char *)
               let spaces = try String.sub space_between 0 (String.rindex space_between '\n' + 1) with _ -> "" in
               let indent_string = String.make (State.indent pre) ' ' in
 
               (* CR jfuruse: can be a bug. something from space_between_region *)
-              Printer.add_string lines last_line spaces;
+              print_string last_line spaces;
 
               if debug then begin
-                Printer.add_string lines current_line indent_string;
+                print_string current_line indent_string;
                 if pre == post then
-                  Printer.add_debug lines current_line
+                  print_debug current_line
                     (Printf.sprintf "-- %s\n" (Sexp.to_string_mach (Stack.sexp_of_t pre.State.bases)))
                 else
-                  Printer.add_debug lines current_line
+                  print_debug current_line
                     (Printf.sprintf "-- %s // %s\n"
                       (Sexp.to_string_mach (Stack.sexp_of_t pre.State.bases))
                       (Sexp.to_string_mach (Stack.sexp_of_t post.State.bases)))
               end;
 
-              Printer.add_string lines current_line indent_string;
-              Printer.add_string lines current_line substr
-            end else begin
-              Printer.add_string lines current_line space_between;
-              Printer.add_string lines current_line substr;
+              print_string current_line indent_string;
             end;
 
+            print_string current_line substr;
+
             (* Now move to the next token *)
-
-            let post =
-              if new_line then
-                { post with
-                  State.last_token = (if t <> Parser.COMMENT then Some t else state.State.last_token)
-                  ; last_indent = State.indent pre
-                }
-              else
-                { post with
-                  State.last_token = (if t <> Parser.COMMENT then Some t else state.State.last_token)
-                }
+            (* CR jfuruse: last_token thing seems strange. The state machine should be able to
+               access previous tokens freely. (But with risk of memory leak) *)
+            let last_token = if t <> Parser.COMMENT then Some t else state.State.last_token in
+            let post = 
+              { post with 
+                State.last_token = last_token; 
+                last_indent = if at_new_line then State.indent pre else post.State.last_indent }
             in
 
             loop orig_region post str
   if showstate then State.print final_state;
   Tokenstr.close str
 
-let indent_file path = try indent_file path with Exit -> ()
-
 let _ = List.iter indent_file paths