branch: externals/phps-mode commit 1b891ea83c61fbd3bd745b8fd65186d3c3718ea8 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
More work on indentation and related tests --- phps-functions.el | 25 +++++++------ phps-lexer.el | 105 ++++++++++++++++++++++++++++++----------------------- phps-test-lexer.el | 50 +++++++++++++++---------- 3 files changed, 103 insertions(+), 77 deletions(-) diff --git a/phps-functions.el b/phps-functions.el index e6bf6d3..57f59c6 100644 --- a/phps-functions.el +++ b/phps-functions.el @@ -38,17 +38,20 @@ (defun phps-mode/indent-line () "Indent line." - (save-excursion - (beginning-of-line) - (let ((data (phps-mode/lexer-get-point-data))) - ;; Are we in scripting? - (if (nth 0 data) - (let ((indent-level (* (+ (nth 1 data) (nth 2 data)) 4))) - (message "inside scripting %s, indenting to column %s " data indent-level) - (indent-line-to indent-level)) - (progn - (message "Outside scripting %s" data) - (indent-relative)))))) + (let ((data (phps-mode/lexer-get-point-data))) + (save-excursion + (beginning-of-line) + (let ((start (nth 0 data)) + (end (nth 1 data))) + + ;; Are we in scripting? + (if (nth 0 start) + (let ((indent-level (* (+ (nth 1 start) (nth 2 start)) 4))) + (message "inside scripting, start: %s, end: %s, indenting to column %s " start end indent-level) + (indent-line-to indent-level)) + (progn + (message "Outside scripting %s" start) + (indent-relative))))))) (defun phps-mode/indent-region () "Indent region." diff --git a/phps-lexer.el b/phps-lexer.el index 979a383..da663ec 100644 --- a/phps-lexer.el +++ b/phps-lexer.el @@ -368,12 +368,12 @@ "Push TOKEN to list with START and END." (phps-mode/COLOR_SYNTAX token start end) - (when (and - phps-mode/prepend_trailing_brace - (> end (- (point-max) 2))) - ;; (message "Adding trailing brace") - (setq phps-mode/prepend_trailing_brace nil) - (phps-mode/RETURN_TOKEN "}" (- end 1) end)) + ;; (when (and + ;; phps-mode/prepend_trailing_brace + ;; (> end (- (point-max) 2))) + ;; ;; (message "Adding trailing brace") + ;; (setq phps-mode/prepend_trailing_brace nil) + ;; (phps-mode/RETURN_TOKEN "}" (- end 1) end)) ;; (message "Added token %s %s %s" token start end) @@ -834,7 +834,7 @@ (when (string= data ";") (setq phps-mode/prepend_trailing_brace t) ;; (message "Set flag prepend trailing brace") - (setq use-brace t) + ;; (setq use-brace t) ) (setq phps-mode/declaring_namespace nil)) (if use-brace @@ -1260,45 +1260,58 @@ ANY_CHAR' (defun phps-mode/lexer-get-point-data() "Return information about point in tokens." ;; (message "Point: %s in %s" (point) phps-mode/lexer-tokens) - (let ((position (point)) - (line-end (line-end-position)) - (in-scripting nil) - (brace-level 0) - (parenthesis-level 0) - (inline-function-level 0)) - (catch 'stop-iteration - (dolist (item phps-mode/lexer-tokens) - (let ((token (car item)) - (start (car (cdr item))) - (end (cdr (cdr item)))) - ;; (message "Token: %s Start: %s End: %s Item: %s" token start end item) - - (when (> start line-end) - ;; (message "Stopping iteration at: %s %s" start position) - (throw 'stop-iteration nil)) - - ;; When start of token is equal or less to current point - (when (<= start position) - (pcase token - ('T_OPEN_TAG (setq in-scripting t)) - ('T_OPEN_TAG_WITH_ECHO (setq in-scripting t)) - ('T_CLOSE_TAG (setq in-scripting nil)) - ("{" (setq brace-level (+ brace-level 1))) - ("(" (setq parenthesis-level (+ parenthesis-level 1))) - (")" (setq parenthesis-level (- parenthesis-level 1))) - (_))) - - ;; When start of token is equal or less to end of curent line - (when (<= start line-end) - (pcase token - ("}" (setq brace-level (- brace-level 1))) - (_))) - - ))) - (let ((data (list in-scripting brace-level parenthesis-level inline-function-level))) - ;; (message "data: %s" data) - data) - )) + (save-excursion + (beginning-of-line) + (let ((position (point)) + (line-end (line-end-position)) + (start-in-scripting nil) + (start-brace-level 0) + (start-parenthesis-level 0) + (start-inline-function-level 0) + (end-in-scripting nil) + (end-brace-level 0) + (end-parenthesis-level 0) + (end-inline-function-level 0)) + (catch 'stop-iteration + (dolist (item phps-mode/lexer-tokens) + (let ((token (car item)) + (start (car (cdr item))) + (end (cdr (cdr item)))) + ;; (message "Token: %s Start: %s End: %s Item: %s" token start end item) + + (when (> start line-end) + ;; (message "Stopping iteration at: %s %s" start position) + (throw 'stop-iteration nil)) + + ;; When start of token is equal or less to current point + (when (< start position) + (pcase token + ('T_OPEN_TAG (setq start-in-scripting t)) + ('T_OPEN_TAG_WITH_ECHO (setq start-in-scripting t)) + ('T_CLOSE_TAG (setq start-in-scripting nil)) + ("}" (setq start-brace-level (- start-brace-level 1))) + ("{" (setq start-brace-level (+ start-brace-level 1))) + ("(" (setq start-parenthesis-level (+ start-parenthesis-level 1))) + (")" (setq start-parenthesis-level (- start-parenthesis-level 1))) + (_))) + + ;; When start of token is equal or less to end of curent line + (when (< start line-end) + (pcase token + ('T_OPEN_TAG (setq end-in-scripting t)) + ('T_OPEN_TAG_WITH_ECHO (setq end-in-scripting t)) + ('T_CLOSE_TAG (setq end-in-scripting nil)) + ("}" (setq end-brace-level (- end-brace-level 1))) + ("{" (setq end-brace-level (+ end-brace-level 1))) + ("(" (setq end-parenthesis-level (+ end-parenthesis-level 1))) + (")" (setq end-parenthesis-level (- end-parenthesis-level 1))) + (_))) + + ))) + (let ((data (list (list start-in-scripting start-brace-level start-parenthesis-level start-inline-function-level) (list end-in-scripting end-brace-level end-parenthesis-level end-inline-function-level)))) + ;; (message "data: %s" data) + data) + ))) (defun phps-mode/lex--SETUP (start end) "Just prepare other lexers for lexing region START to END." diff --git a/phps-test-lexer.el b/phps-test-lexer.el index c96499c..225a426 100644 --- a/phps-test-lexer.el +++ b/phps-test-lexer.el @@ -231,13 +231,14 @@ (phps-mode/with-test-buffer "<?php\nnamespace MyNameSpace{\n\tclass MyClass {\n\t\tpublic function __construct() {\n\t\t\texit;\n\t\t}\n\t}\n}\n" + (should (equal phps-mode/lexer-tokens '((T_OPEN_TAG 1 . 7) (T_NAMESPACE 7 . 16) (T_STRING 17 . 28) ("{" 28 . 29) (T_CLASS 31 . 36) (T_STRING 37 . 44) ("{" 45 . 46) (T_PUBLIC 49 . 55) (T_FUNCTION 56 . 64) (T_STRING 65 . 76) ("(" 76 . 77) (")" 77 . 78) ("{" 79 . 80) (T_EXIT 84 . 88) (";" 88 . 89) ("}" 92 . 93) ("}" 95 . 96) ("}" 97 . 98))))) (phps-mode/with-test-buffer "<?php\nNAMESPACE MyNameSpace;\nCLASS MyClass {\n\tpublic function __construct() {\n\t\texit;\n\t}\n}\n" (should (equal phps-mode/lexer-tokens - '((T_OPEN_TAG 1 . 7) (T_NAMESPACE 7 . 16) (T_STRING 17 . 28) ("{" 28 . 29) (T_CLASS 30 . 35) (T_STRING 36 . 43) ("{" 44 . 45) (T_PUBLIC 47 . 53) (T_FUNCTION 54 . 62) (T_STRING 63 . 74) ("(" 74 . 75) (")" 75 . 76) ("{" 77 . 78) (T_EXIT 81 . 85) (";" 85 . 86) ("}" 88 . 89) ("}" 90 . 91) ("}" 90 . 91))))) + '((T_OPEN_TAG 1 . 7) (T_NAMESPACE 7 . 16) (T_STRING 17 . 28) (";" 28 . 29) (T_CLASS 30 . 35) (T_STRING 36 . 43) ("{" 44 . 45) (T_PUBLIC 47 . 53) (T_FUNCTION 54 . 62) (T_STRING 63 . 74) ("(" 74 . 75) (")" 75 . 76) ("{" 77 . 78) (T_EXIT 81 . 85) (";" 85 . 86) ("}" 88 . 89) ("}" 90 . 91))))) ) (defun phps-mode/test-lexer--errors () @@ -265,44 +266,45 @@ (phps-mode/with-test-buffer "<?php\nNAMESPACE MyNameSpace;\nCLASS MyClass {\n\tpublic function __construct() {\n\t\texit;\n\t}\n}\n" - (goto-char 30) - (should (equal (list t 1 0 0) (phps-mode/lexer-get-point-data)))) + (goto-char 35) + (should (equal (list (list t 0 0 0) (list t 1 0 0)) (phps-mode/lexer-get-point-data)))) (phps-mode/with-test-buffer "<html><head><title><?php echo $title; ?></title><body>Bla bla</body></html>" (goto-char 15) - (should (equal (list nil 0 0 0) (phps-mode/lexer-get-point-data)))) + (should (equal (list (list nil 0 0 0) (list nil 0 0 0)) (phps-mode/lexer-get-point-data)))) (phps-mode/with-test-buffer "<html><head><title><?php echo $title; ?></title><body>Bla bla</body></html>" (goto-char 30) - (should (equal (list t 0 0 0) (phps-mode/lexer-get-point-data)))) + (should (equal (list (list nil 0 0 0) (list nil 0 0 0)) (phps-mode/lexer-get-point-data)))) (phps-mode/with-test-buffer "<html><head><title><?php echo $title; ?></title><body>Bla bla</body></html>" (goto-char 50) - (should (equal (list nil 0 0 0) (phps-mode/lexer-get-point-data)))) + (should (equal (list (list nil 0 0 0) (list nil 0 0 0)) (phps-mode/lexer-get-point-data)))) (phps-mode/with-test-buffer - "<html><head><title><?php if ($myCondition) { if ($mySeconCondition) { echo $title; } } ?></title><body>Bla bla</body></html>" + "<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 t 1 0 0) (phps-mode/lexer-get-point-data)))) + (should (equal (list (list t 1 0 0) (list nil 0 0 0)) (phps-mode/lexer-get-point-data)))) (phps-mode/with-test-buffer - "<html><head><title><?php if ($myCondition) { if ($mySeconCondition) { echo $title; } } ?></title><body>Bla bla</body></html>" - (goto-char 70) - (should (equal (list t 2 0 0) (phps-mode/lexer-get-point-data)))) + "<html><head><title><?php if ($myCondition) { if ($mySeconCondition) {\n echo $title;\n} } ?></title><body>Bla bla</body></html>" + (goto-char 72) + (should (equal (list (list t 2 0 0) (list t 2 0 0)) (phps-mode/lexer-get-point-data)))) (phps-mode/with-test-buffer - "<html><head><title><?php if ($myCondition) { if ($mySeconCondition) { echo $title; } } ?></title><body>Bla bla</body></html>" - (goto-char 85) - (should (equal (list t 1 0 0) (phps-mode/lexer-get-point-data)))) + "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title;\n}\n}\n ?></title><body>Bla bla</body></html>" + (goto-char 84) + (should (equal (list (list t 2 0 0) (list t 1 0 0)) (phps-mode/lexer-get-point-data)))) (phps-mode/with-test-buffer "<html><head><title><?php if ($myCondition) { if ($mySeconCondition) { echo $title; } } ?></title><body>Bla bla</body></html>" + (goto-char 100) - (should (equal (list nil 0 0 0) (phps-mode/lexer-get-point-data)))) + (should (equal (list (list nil 0 0 0) (list nil 0 0 0)) (phps-mode/lexer-get-point-data)))) ) @@ -313,20 +315,28 @@ (goto-char 69) (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) {\n if ($mySeconCondition) {\necho $title;\n\n} ?></title><body>Bla bla</body></html>"))) - (goto-char 85) + (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\n} ?></title><body>Bla bla</body></html>" + (goto-char 80) (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) {\n if ($mySeconCondition) {\n echo $title;\n\n} ?></title><body>Bla bla</body></html>"))) + (should (equal buffer-contents "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\n echo $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\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) {\n if ($mySeconCondition) {\n echo $title;\n\n } ?></title><body>Bla bla</body></html>"))))) + (should (equal buffer-contents "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title;\n\n} ?></title><body>Bla bla</body></html>")))) + + ) (defun phps-mode/test-lexer () "Run test for lexer." ;; (message "-- Running all tests for lexer... --\n") - ;; (setq debug-on-error t) + (setq debug-on-error t) (phps-mode/test-lexer--script-boundaries) (phps-mode/test-lexer--simple-tokens) (phps-mode/test-lexer--complex-tokens)