branch: externals/phps-mode commit a259292c393926de51a9faa8f44290034bdea5c4 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Changed indentation function to work solely on tokens --- phps-functions.el | 48 +++++++++++++++++++++++++++++++++++++++--------- phps-test-functions.el | 28 +++++++++++++++------------- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/phps-functions.el b/phps-functions.el index 7bdfe26..54e021c 100644 --- a/phps-functions.el +++ b/phps-functions.el @@ -49,15 +49,45 @@ ;; Are we in scripting? (when in-scripting - (let ((indent-start (* (+ (nth 1 start) (nth 2 start)) tab-width)) - (indent-end (* (+ (nth 1 end) (nth 2 end)) tab-width)) - (indent-diff 0)) - (when (and (> indent-start indent-end) - (looking-at-p "^[][ \t)(}{};]+\\($\\|?>\\)")) - (setq indent-diff (- indent-start indent-end))) - (let ((indent-level (- indent-start indent-diff))) - (message "inside scripting, start: %s, end: %s, indenting to column %s " start end indent-level) - (indent-line-to indent-level)))))))) + (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))) + (let* ((indent-start (+ start-bracket-level start-parenthesis-level)) + (indent-end (+ end-bracket-level end-parenthesis-level)) + (indent-level indent-start)) + ;; (message "indent-start %s, indent-end %s" indent-start indent-end) + (when (and + (boundp 'phps-mode/lexer-tokens) + (> indent-start indent-end)) + (let ((token-number start-token-number) + (valid-tokens t)) + ;; (message "token start %s, token end %s" start-token-number end-token-number) + (while (and valid-tokens + (<= token-number end-token-number)) + (let ((token (car (nth token-number phps-mode/lexer-tokens))) + (token-start (car (cdr (nth token-number phps-mode/lexer-tokens))))) + (when (and valid-tokens + (>= token-start (point)) + (not (or + (string= token "{") + (string= token "}") + (string= token "(") + (string= token ")") + (string= token "[") + (string= token "]") + (string= token ";") + (eq token 'T_CLOSE_TAG)))) + ;; (message "Token %s - %s in %s was invalid" token token-number phps-mode/lexer-tokens) + (setq valid-tokens nil))) + (setq token-number (+ token-number 1))) + (when valid-tokens + ;; (message "Tokens was valid, decreasing indent %s - %s" (line-beginning-position) (line-end-position)) + (setq indent-level (- indent-level (- indent-start indent-end)))))) + ;; (message "inside scripting, start: %s, end: %s, indenting to column %s " start end indent-level) + (indent-line-to (* indent-level tab-width))))))))) (defun phps-mode/indent-region () "Indent region." diff --git a/phps-test-functions.el b/phps-test-functions.el index d207c62..644f916 100644 --- a/phps-test-functions.el +++ b/phps-test-functions.el @@ -51,49 +51,51 @@ (should (equal buffer-contents "<html><head><title><?php if ($myCondition) {\n if ($mySeconCondition) {\necho $title;\n\n} ?></title><body>Bla bla</body></html>")))) (phps-mode/with-test-buffer - "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title;\n} ?></title><body>Bla bla</body></html>" + "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title1;\n} ?></title><body>Bla bla</body></html>" (goto-char 75) (phps-mode/indent-line) (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal buffer-contents "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\n echo $title;\n} ?></title><body>Bla bla</body></html>")))) + (should (equal buffer-contents "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\n echo $title1;\n} ?></title><body>Bla bla</body></html>")))) (phps-mode/with-test-buffer - "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title;\n\n} ?></title><body>Bla bla</body></html>" + "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title2;\n\n} ?></title><body>Bla bla</body></html>" (goto-char 98) (phps-mode/indent-line) (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal buffer-contents "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title;\n\n } ?></title><body>Bla bla</body></html>")))) + (should (equal buffer-contents "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title2;\n\n } ?></title><body>Bla bla</body></html>")))) (phps-mode/with-test-buffer - "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title;\n\n}\n?>\n</title><body>Bla bla</body></html>" + "<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)))) - (should (equal buffer-contents "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title;\n\n}\n?>\n</title><body>Bla bla</body></html>")))) + (should (equal buffer-contents "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title3;\n\n}\n?>\n</title><body>Bla bla</body></html>")))) (phps-mode/with-test-buffer - "<?php\n$variable = array(\n'random'\n);\n$variable = true;\n" + "<?php\n$variable = array(\n'random3'\n);\n$variable = true;\n" (goto-char 28) (phps-mode/indent-line) (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal buffer-contents "<?php\n$variable = array(\n 'random'\n);\n$variable = true;\n")))) + (should (equal buffer-contents "<?php\n$variable = array(\n 'random3'\n);\n$variable = true;\n")))) (phps-mode/with-test-buffer - "<?php\n$variable = array(\n 'random'\n );\n$variable = true;\n" - (goto-char 39) + "<?php\n$variable = array(\n 'random2'\n );\n$variable = true;\n" + (goto-char 43) + ;; (message "Tokens %s point %s" phps-mode/lexer-tokens (point)) (phps-mode/indent-line) (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal buffer-contents "<?php\n$variable = array(\n 'random'\n);\n$variable = true;\n")))) + (should (equal buffer-contents "<?php\n$variable = array(\n 'random2'\n);\n$variable = true;\n")))) (phps-mode/with-test-buffer - "<?php\n$variable = array(\n'random');\n$variable = true;\n" + "<?php\n$variable = array(\n'random4');\n$variable = true;\n" (goto-char 29) (phps-mode/indent-line) + ;; (message "Tokens %s point %s" phps-mode/lexer-tokens (point)) (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal buffer-contents "<?php\n$variable = array(\n 'random');\n$variable = true;\n")))) + (should (equal buffer-contents "<?php\n$variable = array(\n 'random4');\n$variable = true;\n")))) )