Skip to content

Commit

Permalink
Stop marching indentation for try-catch blocks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Eric James Michael Ritz committed Aug 26, 2013
1 parent babd790 commit 6db85bd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions php-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(defconst php-mode-version-number "1.11"
"PHP Mode version number.")

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

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

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

(defconst php-block-stmt-1-key
(regexp-opt php-block-stmt-1-kwds))
Expand Down
57 changes: 57 additions & 0 deletions tests/issue-68.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* GitHub Issue: https://github.com/ejmr/php-mode/issues/68
*
* The 'catch' keywords should align with 'try' and not march
* off to the right side of the screen like they do in the bug
* report at the URL above.
*
*/

/* CC Mode uses the indentation for 'block-close' for these 'catch'
* statements. However, that is assuming that the closing braces for
* 'try' and each 'catch' align with the keywords themselves. If they
* do not then CC Mode gets confused and starts trying to indent
* 'catch' blocks as if they were function definitions and other
* strange things.
*
* The easiest way to deal with this is to make sure that the
* `c-block-stmt-2-key' list contains the string "catch", which we
* take care of in the definition of `php-block-stmt-2-key'.
*/

try {
$mongo = new \Mongo($dsn, array("replicaSet" => 'mat_replica'));
} catch (\MongoConnectionException $e) {
throw $e;
} catch (\Exception $e) {
throw $e;
}

/* CC Mode uses the indentation syntax for 'topmost-intro' for the
* 'catch' keywords in the remaining tests.
*/

try {
$mongo = new \Mongo($dsn, array("replicaSet" => 'mat_replica'));
}
catch (\MongoConnectionException $e) {
throw $e;
}
catch (\Exception $e) {
throw $e;
}

try
{
$mongo = new \Mongo($dsn, array("replicaSet" => 'mat_replica'));
}
catch (\MongoConnectionException $e)
{
throw $e;
}
catch (\Exception $e)
{
throw $e;
}

0 comments on commit 6db85bd

Please sign in to comment.