branch: externals/phps-mode
commit a5beef1ce6f4faf4136efc16e980894521f1c5a9
Author: Christian Johansson <[email protected]>
Commit: Christian Johansson <[email protected]>
Added support for square bracket indentation
---
phps-functions.el | 35 ++++++++++++++++++++---------------
phps-test-functions.el | 13 ++++++++++---
2 files changed, 30 insertions(+), 18 deletions(-)
diff --git a/phps-functions.el b/phps-functions.el
index 2c50305..11c7a46 100644
--- a/phps-functions.el
+++ b/phps-functions.el
@@ -58,15 +58,19 @@
;; Are we in scripting?
(when in-scripting
- (let ((start-bracket-level (nth 1 start))
- (start-parenthesis-level (nth 2 start))
- (start-token-number (nth 4 start))
- (end-bracket-level (nth 1 end))
- (end-parenthesis-level (nth 2 end))
- (end-token-number (nth 4 end))
- (in-doc-comment (nth 5 start)))
- (let* ((indent-start (+ start-bracket-level start-parenthesis-level))
- (indent-end (+ end-bracket-level end-parenthesis-level))
+ (let ((start-curly-bracket-level (nth 1 start))
+ (start-round-bracket-level (nth 2 start))
+ (start-square-bracket-level (nth 3 start))
+ (start-inline-function-level (nth 4 start))
+ (start-token-number (nth 5 start))
+ (end-curly-bracket-level (nth 1 end))
+ (end-round-bracket-level (nth 2 end))
+ (end-square-bracket-level (nth 3 end))
+ (end-inline-function-level (nth 4 end))
+ (end-token-number (nth 5 end))
+ (in-doc-comment (nth 6 start)))
+ (let* ((indent-start (+ start-curly-bracket-level
start-round-bracket-level start-square-bracket-level))
+ (indent-end (+ end-curly-bracket-level
end-round-bracket-level end-square-bracket-level))
(indent-level indent-start)
(indent-adjust 0))
;; (message "indent-start %s, indent-end %s" indent-start
indent-end)
@@ -78,8 +82,8 @@
end-token-number)
(let ((token-number start-token-number)
(valid-tokens t)
- (last-token-is-opening-brace nil)
- (first-token-is-closing-brace nil)
+ (last-token-is-opening-curly-bracket nil)
+ (first-token-is-closing-curly-bracket nil)
(tokens phps-mode/lexer-tokens)
(is-first-line-token t))
;; (message "token start %s, token end %s" start-token-number
end-token-number)
@@ -99,7 +103,7 @@
;; Is it the last token and is it a opening brace?
(when (and (= token-number end-token-number)
(string= token "{"))
- (setq last-token-is-opening-brace t))
+ (setq last-token-is-opening-curly-bracket t))
;; Is it the first line token?
(when is-first-line-token
@@ -107,7 +111,7 @@
;; Is it a closing brace?
(when (string= token "}")
- (setq first-token-is-closing-brace t)))
+ (setq first-token-is-closing-curly-bracket t)))
(when (and valid-tokens
(not (or
@@ -118,6 +122,7 @@
(string= token "[")
(string= token "]")
(string= token ";")
+ (string= token ",")
(eq token 'T_CLOSE_TAG))))
;; (message "Token %s - %s in %s was invalid, line
start %s" token token-number tokens line-start)
(setq valid-tokens nil))
@@ -130,7 +135,7 @@
(progn
;; If last token is a opening brace indent line one
lesser column
- (when last-token-is-opening-brace
+ (when last-token-is-opening-curly-bracket
;; (message "Last token was opening brace")
(setq indent-level (- indent-level 1)))
@@ -139,7 +144,7 @@
;; If first token is a closing brace indent line one lesser
column
- (when first-token-is-closing-brace
+ (when first-token-is-closing-curly-bracket
;; (message "First token was closing brace")
(setq indent-level (- indent-level 1))))
diff --git a/phps-test-functions.el b/phps-test-functions.el
index 137db58..429503c 100644
--- a/phps-test-functions.el
+++ b/phps-test-functions.el
@@ -47,6 +47,7 @@
(defun phps-mode/test-indent-line ()
"Test for indentation."
+ ;; Curly bracket tests
(phps-mode/with-test-buffer
"<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition)
{\necho $title;\n\n} ?></title><body>Bla bla</body></html>"
(goto-char 69)
@@ -70,7 +71,6 @@
(phps-mode/with-test-buffer
"<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition)
{\necho $title3;\n\n}\n?>\n</title><body>Bla bla</body></html>"
-
(goto-char 110)
(phps-mode/indent-line)
(let ((buffer-contents (buffer-substring-no-properties (point-min)
(point-max))))
@@ -163,11 +163,19 @@
(let ((buffer-contents (buffer-substring-no-properties (point-min)
(point-max))))
(should (equal buffer-contents "<?php\nif (myFirstCondition()) {\n
$this->var = 'abc123';\n} else if (mySeconCondition()) {\n $this->var =
'def456';\n}\n"))))
+ ;; Square bracket
+ (phps-mode/with-test-buffer
+ "<?php\n$var = [\n 'random' => [\n 'hello',\n],\n];\n"
+ (goto-char 51)
+ (phps-mode/indent-line)
+ (let ((buffer-contents (buffer-substring-no-properties (point-min)
(point-max))))
+ (should (equal buffer-contents "<?php\n$var = [\n 'random' => [\n
'hello',\n ],\n];\n"))))
+
(phps-mode/with-test-buffer
"<?php\nif (myRandomCondition()):\necho 'Something here';\n else:\n
echo 'Something else here';\nendif;\n"
(goto-char 60)
(phps-mode/indent-line)
- (message "Tokens %s" phps-mode/lexer-tokens)
+ ;; (message "Tokens %s" phps-mode/lexer-tokens)
(let ((buffer-contents (buffer-substring-no-properties (point-min)
(point-max))))
(should (equal buffer-contents "<?php\nif (myRandomCondition()):\necho
'Something here';\nelse:\n echo 'Something else here';\nendif;\n"))))
@@ -270,7 +278,6 @@
(phps-mode/with-test-buffer
"<html><head><title><?php if ($myCondition) { \n if ($mySeconCondition) {
echo $title; } } ?></title><body>Bla bla</body></html>"
- ;; (message "Tokens: %s" phps-mode/lexer-tokens)
(goto-char 48)
(should (equal (list (list t 1 0 0 0 5 nil) (list nil 0 0 0 0 17 nil))
(phps-mode/get-point-data))))