Skip to content

Commit 6db85bd

Browse files
author
Eric James Michael Ritz
committed
Stop marching indentation for try-catch blocks
This patch not only fixes the issue but also includes a test case which has a lot of commentary explaining how CC Mode tries to indent try-case blocks and the potential problems therein. Our specific fix in this patch is to add the keyword `catch' to the constant list `php-block-stmt-2-kwds'. The result is that it adds the keyword to the CC Mode list `c-block-stmt-2-key' and that automatically takes care of indenting try-catch blocks in situations where we put the keywords and braces on the same lines together. GitHub-Issue: 68
1 parent babd790 commit 6db85bd

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

php-mode.el

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
(defconst php-mode-version-number "1.11"
1212
"PHP Mode version number.")
1313

14-
(defconst php-mode-modified "2013-08-22"
14+
(defconst php-mode-modified "2013-08-26"
1515
"PHP Mode build date.")
1616

1717
;;; License
@@ -532,7 +532,7 @@ example `html-mode'. Known such libraries are:\n\t"
532532

533533
(defconst php-block-stmt-1-kwds '("do" "else" "finally" "try"))
534534
(defconst php-block-stmt-2-kwds
535-
'("for" "if" "while" "switch" "foreach" "elseif" "catch all"))
535+
'("for" "if" "while" "switch" "foreach" "elseif" "catch" "catch all"))
536536

537537
(defconst php-block-stmt-1-key
538538
(regexp-opt php-block-stmt-1-kwds))

tests/issue-68.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* GitHub Issue: https://github.com/ejmr/php-mode/issues/68
5+
*
6+
* The 'catch' keywords should align with 'try' and not march
7+
* off to the right side of the screen like they do in the bug
8+
* report at the URL above.
9+
*
10+
*/
11+
12+
/* CC Mode uses the indentation for 'block-close' for these 'catch'
13+
* statements. However, that is assuming that the closing braces for
14+
* 'try' and each 'catch' align with the keywords themselves. If they
15+
* do not then CC Mode gets confused and starts trying to indent
16+
* 'catch' blocks as if they were function definitions and other
17+
* strange things.
18+
*
19+
* The easiest way to deal with this is to make sure that the
20+
* `c-block-stmt-2-key' list contains the string "catch", which we
21+
* take care of in the definition of `php-block-stmt-2-key'.
22+
*/
23+
24+
try {
25+
$mongo = new \Mongo($dsn, array("replicaSet" => 'mat_replica'));
26+
} catch (\MongoConnectionException $e) {
27+
throw $e;
28+
} catch (\Exception $e) {
29+
throw $e;
30+
}
31+
32+
/* CC Mode uses the indentation syntax for 'topmost-intro' for the
33+
* 'catch' keywords in the remaining tests.
34+
*/
35+
36+
try {
37+
$mongo = new \Mongo($dsn, array("replicaSet" => 'mat_replica'));
38+
}
39+
catch (\MongoConnectionException $e) {
40+
throw $e;
41+
}
42+
catch (\Exception $e) {
43+
throw $e;
44+
}
45+
46+
try
47+
{
48+
$mongo = new \Mongo($dsn, array("replicaSet" => 'mat_replica'));
49+
}
50+
catch (\MongoConnectionException $e)
51+
{
52+
throw $e;
53+
}
54+
catch (\Exception $e)
55+
{
56+
throw $e;
57+
}

0 commit comments

Comments
 (0)