Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a 'strict_else' option to control indentation after the 'else' …

…keyword

Implements feature requested in issue #37, as it seems to be of importance to many users.
  • Loading branch information...
commit 037874170ac40720b573f78701df5d2174ad7f71 1 parent 0e3b82c
@AltGr AltGr authored
View
16 .ocp-indent
@@ -71,6 +71,22 @@ max_indent = 4
# ^^| Foo -> bar
strict_with = never
+# Controls indentation after the `else` keyword. `always` indents after the
+# `else` keyword normally, like after `then`.
+# If set to `never', the `else` keyword won't indent when followed by a newline.
+# `auto` indents after `else` unless in a few "unclosable" cases (`let in`,
+# `match`...).
+#
+# For example, with `strict_else=never`:
+# if cond then
+# foo
+# else
+# bar;
+# baz
+# `never` is discouraged if you may encounter code like this example,
+# because it hides the scoping error (`baz` is always executed)
+strict_else = always
+
# Ocp-indent will normally try to preserve your in-comment indentation, as long
# as it respects the left-margin or starts with `(*\n`. Setting this to `true`
# forces alignment within comments.
View
14 src/indentBlock.ml
@@ -868,7 +868,19 @@ let rec update_path config block stream tok =
extend KThen L (unwind ((=) KIf) block.path)
| ELSE ->
- extend KElse L (unwind ((=) KThen) block.path)
+ let pad =
+ match config.i_strict_else with
+ | Always -> config.i_base
+ | Never ->
+ if next_offset tok stream <> None then config.i_base
+ else 0
+ | Auto ->
+ if next_offset tok stream <> None then config.i_base
+ else match next_token stream with
+ | Some (LET|MATCH|TRY|FUN|FUNCTION) -> 0
+ | _ -> config.i_base
+ in
+ extend KElse L ~pad (unwind ((=) KThen) block.path)
| WHILE | FOR ->
append KLoop L (fold_expr block.path)
View
24 src/indentConfig.ml
@@ -25,6 +25,7 @@ type t = {
i_match_clause: int;
i_max_indent: int option;
i_strict_with: threechoices;
+ i_strict_else: threechoices;
i_strict_comments: bool;
i_align_ops: bool;
i_align_params: threechoices;
@@ -38,6 +39,7 @@ let default = {
i_match_clause = 2;
i_max_indent = Some 4;
i_strict_with = Never;
+ i_strict_else = Always;
i_strict_comments = false;
i_align_ops = true;
i_align_params = Auto;
@@ -47,14 +49,14 @@ let presets = [
"apprentice",
{ i_base = 2; i_type = 4; i_in = 2; i_with = 2; i_match_clause = 4;
i_max_indent = None;
- i_strict_with = Never; i_strict_comments = false;
+ i_strict_with = Never; i_strict_else = Always; i_strict_comments = false;
i_align_ops = true; i_align_params = Always };
"normal",
default;
"JaneStreet",
{ i_base = 2; i_type = 2; i_in = 0; i_with = 0; i_match_clause = 2;
i_max_indent = Some 2;
- i_strict_with = Auto; i_strict_comments = true;
+ i_strict_with = Auto; i_strict_else = Always; i_strict_comments = true;
i_align_ops = true; i_align_params = Always };
]
@@ -89,6 +91,7 @@ let to_string ?(sep=",") indent =
match_clause = %d%s\
max_indent = %s%s\
strict_with = %s%s\
+ strict_else = %s%s\
strict_comments = %b%s\
align_ops = %b%s\
align_params = %s"
@@ -99,6 +102,7 @@ let to_string ?(sep=",") indent =
indent.i_match_clause sep
(string_of_intoption indent.i_max_indent) sep
(string_of_threechoices indent.i_strict_with) sep
+ (string_of_threechoices indent.i_strict_else) sep
indent.i_strict_comments sep
indent.i_align_ops sep
(string_of_threechoices indent.i_align_params)
@@ -113,6 +117,7 @@ let set ?(extra=fun _ -> None) t var_name value =
| "match_clause" -> {t with i_match_clause = int_of_string value}
| "max_indent" -> {t with i_max_indent = intoption_of_string value}
| "strict_with" -> {t with i_strict_with = threechoices_of_string value}
+ | "strict_else" -> {t with i_strict_else = threechoices_of_string value}
| "with_never" -> (* backwards compat, don't document *)
{t with i_strict_with = if bool_of_string value then Always else Never}
| "strict_comments" -> {t with i_strict_comments = bool_of_string value}
@@ -230,11 +235,24 @@ let man =
If `auto', there are exceptions for constructs like \
`begin match with'.\n\
If `never', `i_with' is always strictly respected.")
- :: pre " Example, with `strict_with=$(b,never),i_with=0`:\n\
+ :: pre " Example, with `strict_with=$(b,never),i_with=0':\n\
\ begin match foo with\n\
\ $(b,..)| _ -> bar\n\
\ end"
@
+ `I (option_name "strict_else" "<always|never|auto>"
+ (string_of_threechoices default.i_strict_else),
+ "`always' indents after the `else' keyword normally, like after \
+ `then'. If set to `never', the `else' keyword won't indent when \
+ followed by a newline. `auto' indents after `else' unless in a few \
+ \"unclosable\" cases (`let in', `match'...).")
+ :: pre " Example, with `strict_else=$(b,auto)':\n\
+ \ if cond then\n\
+ \ foo\n\
+ \ else\n\
+ \ $(b,let) x = bar in\n\
+ \ baz"
+ @
`I (option_name "strict_comments" "BOOL"
(string_of_bool default.i_strict_comments),
"in-comment indentation is normally preserved, as long as it respects \
View
1  src/indentConfig.mli
@@ -26,6 +26,7 @@ type t = {
(** indentation toggles *)
i_max_indent: int option;
i_strict_with: threechoices;
+ i_strict_else: threechoices;
i_strict_comments: bool;
i_align_ops: bool;
i_align_params: threechoices;
View
4 tests/failing.html
@@ -2,7 +2,7 @@
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
- <title>Failing tests, ocp-indent version 1.2.0+13 (2013-05-22)</title>
+ <title>Failing tests, ocp-indent version 1.2.0+14 (2013-05-27)</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style>
TABLE { border-collapse: collapse; border-spacing: 0px; margin: auto; }
@@ -16,7 +16,7 @@
</style>
</head>
<body>
-<h1>Failing tests, ocp-indent version 1.2.0+13 (2013-05-22)</h1>
+<h1>Failing tests, ocp-indent version 1.2.0+14 (2013-05-27)</h1>
<div>
<h2>Differences in js-args.ml</h2>
<table>
View
60 tests/passing/str_else_always.ml
@@ -0,0 +1,60 @@
+let () =
+ if true then "bla" else
+ if true then "bli" else
+ "blo"
+
+let () =
+ if true then "bla" else
+ if true then "bli" else
+ begin
+ "hop"
+ end
+
+let () =
+ if true then "hop" else
+ if true then "hap" else
+ ((); "bla")
+
+let () =
+ if
+ x
+ then
+ y
+ else k,
+ w;
+ z
+
+let () =
+ if x then a
+ else
+ let y = x / 42 in
+ y
+
+let () =
+ if x then a
+ else if y
+ then b
+ else begin
+ blabla
+ end;
+ x
+
+let () =
+ if x then
+ a
+ else match y with
+ | A -> x
+ | B -> y
+
+let () =
+ if x then
+ a
+ else
+ match y with
+ | A -> x
+ | B -> y
+
+let () =
+ if x then a else
+ fun x ->
+ y
View
1  tests/passing/str_else_always.ml.opts
@@ -0,0 +1 @@
+-c strict_else=always
View
60 tests/passing/str_else_auto.ml
@@ -0,0 +1,60 @@
+let () =
+ if true then "bla" else
+ if true then "bli" else
+ "blo"
+
+let () =
+ if true then "bla" else
+ if true then "bli" else
+ begin
+ "hop"
+ end
+
+let () =
+ if true then "hop" else
+ if true then "hap" else
+ ((); "bla")
+
+let () =
+ if
+ x
+ then
+ y
+ else k,
+ w;
+ z
+
+let () =
+ if x then a
+ else
+ let y = x / 42 in
+ y
+
+let () =
+ if x then a
+ else if y
+ then b
+ else begin
+ blabla
+ end;
+ x
+
+let () =
+ if x then
+ a
+ else match y with
+ | A -> x
+ | B -> y
+
+let () =
+ if x then
+ a
+ else
+ match y with
+ | A -> x
+ | B -> y
+
+let () =
+ if x then a else
+ fun x ->
+ y
View
1  tests/passing/str_else_auto.ml.opts
@@ -0,0 +1 @@
+-c strict_else=auto
View
60 tests/passing/str_else_never.ml
@@ -0,0 +1,60 @@
+let () =
+ if true then "bla" else
+ if true then "bli" else
+ "blo"
+
+let () =
+ if true then "bla" else
+ if true then "bli" else
+ begin
+ "hop"
+ end
+
+let () =
+ if true then "hop" else
+ if true then "hap" else
+ ((); "bla")
+
+let () =
+ if
+ x
+ then
+ y
+ else k,
+ w;
+ z
+
+let () =
+ if x then a
+ else
+ let y = x / 42 in
+ y
+
+let () =
+ if x then a
+ else if y
+ then b
+ else begin
+ blabla
+ end;
+ x
+
+let () =
+ if x then
+ a
+ else match y with
+ | A -> x
+ | B -> y
+
+let () =
+ if x then
+ a
+ else
+ match y with
+ | A -> x
+ | B -> y
+
+let () =
+ if x then a else
+ fun x ->
+ y
View
1  tests/passing/str_else_never.ml.opts
@@ -0,0 +1 @@
+-c strict_else=never
Please sign in to comment.
Something went wrong with that request. Please try again.