branch: externals/phps-mode commit dc361309e023da4077d163c02deaacc8847e22b5 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Passing all indentation tests --- phps-mode-indent.el | 31 +++- test/phps-mode-test-indent.el | 356 +----------------------------------------- 2 files changed, 27 insertions(+), 360 deletions(-) diff --git a/phps-mode-indent.el b/phps-mode-indent.el index a56a14534f..acb1b01fe4 100644 --- a/phps-mode-indent.el +++ b/phps-mode-indent.el @@ -138,6 +138,7 @@ (move-length1 0) (move-length2 0) (current-line-string "") + (current-line-is-empty-p) (previous-line-string "") (previous-line-is-empty-p) (previous2-line-string "")) @@ -189,18 +190,27 @@ (setq searching-previous-lines (1- searching-previous-lines)) - (when (= searching-previous-lines 1) + (when (= searching-previous-lines 2) (setq previous-line-is-empty-p line-is-empty-p))) (setq move-length (1+ move-length))))) - ;; (message "previous-line-string: %S" previous-line-string) - ;; (message "previous2-line-string: %S" previous2-line-string) + (goto-char point) + + (when + (string-match-p + "^[ \t\f\r\n]*$" + current-line-string) + (setq + current-line-is-empty-p + t)) (if previous-line-is-empty-p - (indent-line-to 0) + (indent-line-to + (phps-mode-indent--string-indentation + previous-line-string)) (let* ((previous-indentation (phps-mode-indent--string-indentation previous-line-string)) @@ -243,7 +253,6 @@ (setq new-indentation previous-indentation) - (goto-char point) ;; (message "\ncurrent-line-string: %S" current-line-string) ;; (message "previous-line-string: %S" previous-line-string) @@ -884,7 +893,7 @@ (and not-found (search-backward-regexp - "\\(;\\|{\\|(\\|)\\|=\\|echo[\t ]+\\|print[\t ]+\\|\n\\|<<<'?\"?[a-zA-Z0-9]+'?\"?\\)" + "\\(;\\|{\\|(\\|)\\|=$\\|=[^>]\\|echo[\t ]+\\|print[\t ]+\\|\n\\|<<<'?\"?[a-zA-Z0-9]+'?\"?\\)" nil t)) (let ((match (match-string-no-properties 0))) @@ -909,7 +918,7 @@ parenthesis-level (1- parenthesis-level))) ((= parenthesis-level 0) - (setq is-assignment (string= match "=")) + (setq is-assignment (string-match-p "=" match)) (setq is-bracket-less-command (string-match-p "\\(echo[\t ]+\\|print[\t ]+\\)" @@ -937,10 +946,16 @@ (buffer-substring-no-properties (line-beginning-position) (line-end-position))))) + ;; (message "start-bracket-count: %S from %S" start-bracket-count (buffer-substring-no-properties + ;; (line-beginning-position) + ;; (line-end-position))) (setq bracket-opened-on-first-line (> start-bracket-count 0)))) + ;; (message "is-assignment: %S" is-assignment) + ;; (message "bracket-opened-on-first-line: %S" bracket-opened-on-first-line) + ;; echo 'there' . ;; 'here'; ;; echo 'here'; @@ -1186,6 +1201,8 @@ ) + ;; (message "new-indentation: %S" new-indentation) + (indent-line-to new-indentation))))) ;; Only move to end of line if point is the current point and is at end of line (when (equal point (point)) diff --git a/test/phps-mode-test-indent.el b/test/phps-mode-test-indent.el index 3ba781f7b2..3f0ace71da 100644 --- a/test/phps-mode-test-indent.el +++ b/test/phps-mode-test-indent.el @@ -210,11 +210,11 @@ "Indent multi-dimensional arrays without trailing commas") (phps-mode-test-indent--should-equal - "<html>\n <head>\n <?php echo $title; ?>\n </head>\n <body>\n <?php\n\n if ($myTest) {\n doSomething();\n }\n\n ?>\n </body>\n</html>" + "<html>\n<head>\n<?php echo $title; ?>\n</head>\n<body>\n<?php\n\nif ($myTest) {\n doSomething();\n}\n\n?>\n</body>\n</html>" "A mixed HTML and PHP file.") (phps-mode-test-indent--should-equal - "<?php\n\n if ($fullInfo) $fullInfo = unserialize ($fullInfo);\n else array();\n\n" + "<?php\n\nif ($fullInfo) $fullInfo = unserialize ($fullInfo);\nelse array();\n\n" "Indentation for single-line inline control structures.") (phps-mode-test-indent--should-equal @@ -530,355 +530,6 @@ ) -(defun phps-mode-test-indent--indent-line () - "Test for indentation." - - ;; Curly bracket tests - (phps-mode-test-indent--should-equal - "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\n echo $title;\n\n} ?></title><body>Bla bla</body></html>" - "Curly bracket test") - - (phps-mode-test-indent--should-equal - "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title2;\n\n} ?></title><body>Bla bla</body></html>" - "Curly bracket test 3") - - (phps-mode-test-indent--should-equal - "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition) {\necho $title3;\n\n}\n?>\n</title><body>Bla bla</body></html>" - "Curly bracket test 4") - - (phps-mode-test-indent--should-equal - "<?php\n$variable = array(\n'random3'\n);\n$variable = true;\n" - "Assignment test 1") - - (phps-mode-test-indent--should-equal - "<?php\n$variable = array(\n 'random2'\n );\n$variable = true;\n" - "Assignment test 2") - - (phps-mode-test-indent--should-equal - "<?php\n/**\n* My first line\n* My second line\n**/\n" - "Doc-comment test 1") - - (phps-mode-test-indent--should-equal - "<?php\n/**\n* My first line\n* My second line\n**/\n" - "Doc-comment test 2") - - (phps-mode-test-indent--should-equal - "<?php\n/**\n* My first line\n* My second line\n**/\n" - "Doc-comment test 3") - - (phps-mode-test-indent--should-equal - "<?php\n$variable = array(\n 'random4');\n$variable = true;\n" - "Round bracket test 1") - - (phps-mode-test-indent--should-equal - "<?php\nadd_filter(\n\"views_{$screen->id}\",'__return_empty_array'\n);" - "Round bracket test 2") - - (phps-mode-test-indent--should-equal - "<?php\nif (random_expression(\ntrue\n)) {\nsome_logic_here();\n}" - "Round bracket test 3") - - (phps-mode-test-indent--should-equal - "<?php\nif (empty(\n$this->var\n) && !empty($this->var)\n) {\n$this->var = 'abc123';\n}\n" - "Nested if-expression") - - (phps-mode-test-indent--should-equal - "<?php\nif (myFirstCondition()) {\n $this->var = 'abc123';\n } else {\n $this->var = 'def456';\n}\n" - "Regular else expression") - - (phps-mode-test-indent--should-equal - "<?php\nif (myFirstCondition()) {\n $this->var = 'abc123';\n } else if (mySeconCondition()) {\n $this->var = 'def456';\n}\n" - "Regular else if test") - - ;; Square bracket - (phps-mode-test-indent--should-equal - "<?php\n$var = [\n 'random' => [\n 'hello',\n],\n];\n" - "Square bracket test 1") - - (phps-mode-test-indent--should-equal - "<?php\nif (myRandomCondition()):\necho 'Something here';\n else:\n echo 'Something else here 8';\nendif;\n" - "Alternative else test") - - (phps-mode-test-indent--should-equal - "<?php\nswitch (myRandomCondition()) {\ncase 'Something here':\necho 'Something else here';\n}\n" - "Switch case indentation test") - - (phps-mode-test-indent--should-equal - "<?php\nswitch (myRandomCondition()): \ncase 'Something here':\necho 'Something else here';\nendswitch;\n" - "Alternative switch case indentation test 2") - - (phps-mode-test-indent--should-equal - "<?php\nif (myRandomCondition())\necho 'Something here';\necho 'Something else here';\n" - "Inline control structure indentation") - - (phps-mode-test-indent--should-equal - "<?php\nif (myRandomCondition())\n echo 'Something here';\n echo 'Something else here';\n" - "Inline control structure indentation 2") - - (phps-mode-test-indent--should-equal - "<?php\nif (myRandomCondition()):\necho 'Something here';\n echo 'Something else here';\nendif;\n" - "Alternative control structure indentation 1") - - (phps-mode-test-indent--should-equal - "<?php\nmyFunction(\n array(\n 'random' => 'abc',\n ),\n $var5\n);\n" - "Function arguments with associate array indentation") - - (phps-mode-test-indent--should-equal - "<?php\n$var = $var2->getHead()\n->getTail();\n" - "Multi-line assignment indentation test 1") - - (phps-mode-test-indent--should-equal - "<?php\n$var =\n'random string';\n" - "Single-line assignment indentation test") - - (phps-mode-test-indent--should-equal - "<?php\nif (empty($this->var)):\n$this->var = 'abc123';\n endif;" - "Alternative control structure if expression") - - (phps-mode-test-indent--should-equal - "<?php\nif (empty($this->var)):\n$this->var = 'abc123';\nendif;" - "Alternative control structure test") - - (phps-mode-test-indent--should-equal - "<html>\n<head>\n<title><?php echo $title; ?></title>\n</head>\n<body>\n<div class=\"contents\"><?php echo $body; ?></div>\n</body>\n</html>" - "A mixed HTML and PHP file, each PHP command is inside HTML markup") - - (phps-mode-test-indent--should-equal - "<html>\n<head>\n<title><?php echo $title; ?></title>\n</head>\n<body class=\"<?php echo $class; ?>\">\n<div class=\"contents\"><?php echo $body; ?></div>\n</body>\n</html>" - "A mixed HTML and PHP file, each PHP command is inside HTML markup, one PHP inside markup tag") - - (phps-mode-test-indent--should-equal - "<html>\n <head>\n <title><?php $myTitle; ?></title>\n </head>\n <body>\n <?php echo 'test'; ?>\n <h1>My title</h1>\n <?php if ($myTest): ?>\n <div>\n A lot of other stuff.\n </div>\n <?php endif; ?>\n </body>\n</html>" - "Indent mixed HTML and one-line PHP lines.") - - (phps-mode-test-indent--should-equal - "<?php\nif ($here) {\n $var = \"abc $b[abc] def\";\n// Was here\n}\n\n" - "Indentation after line with square brackets inside double quoted string") - - (phps-mode-test-indent--should-equal - "<?php\n\n// Adjust days to delivery accorind to document\nswitch ($dayOfWeek)\n{\n case 1: // Monday\n case 2: // Tuesday\n case 3: // Wednesday\n case 7: // Sunday\n $daysToDelivery = 3;\n break;\n case 4: // Thursday\n case 5: // Friday\n $daysToDelivery = 5;\n break;\n case 6: // Saturday\n $daysToDelivery = 4;\n break;\n default:\n throw new \Exception(sprintf(\n 'day of week above interval (1-7): [...] - "Switch case with default case and trailing comments") - - (phps-mode-test--with-buffer - "<?php\nif ($myCondition) {\necho 'I was here';\n}" - "Alternative indentation inside if block" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif ($myCondition) {\n echo 'I was here';\n}")))) - - (phps-mode-test--with-buffer - "<?php\nif ($myCondition) {\necho 'I was here';\necho 'I was here again';\n}" - "Alternative indentation on closing if block" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif ($myCondition) {\n echo 'I was here';\n echo 'I was here again';\n}")))) - - (phps-mode-test--with-buffer - "<?php\nif ($test) {\nif ($test2) {\n\n}\n}" - "Alternative indentation on nested if block with empty contents" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif ($test) {\n if ($test2) {\n \n }\n}")))) - - (phps-mode-test--with-buffer - "<?php\nif ($test) {\nif ($test2) {\n\n}\n\n}" - "Alternative indentation on multiple closing brackets" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif ($test) {\n if ($test2) {\n \n }\n \n}")))) - - (phps-mode-test--with-buffer - "<?php\nif ($test) {\n\n} else if ($test) {\n\n}\n" - "Alternative indentation on elseif block" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif ($test) {\n \n} else if ($test) {\n \n}\n")))) - - (phps-mode-test--with-buffer - "if ($true) {\nif ($true) {\n}\n}" - "Alternative indentation on closing bracket inside parent bracket" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "if ($true) {\n if ($true) {\n }\n}")))) - - (phps-mode-test--with-buffer - "/**\n*\n*/" - "Alternative indentation on last line of doc comment block" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "/**\n *\n */")))) - - (phps-mode-test--with-buffer - " $var = 'abc';\n // Comment" - "Alternative indentation on single-line assignment" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "$var = 'abc';\n// Comment")))) - - (phps-mode-test--with-buffer - "$var =\n'abc';\n$var =\n'abc'\n. 'def';\n// Comment\n" - "Alternative indentation on multi-line assignment" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "$var =\n 'abc';\n$var =\n 'abc'\n . 'def';\n// Comment\n")))) - - (phps-mode-test--with-buffer - "<?php\nif ($here) {\nif ($wasHere)\n{\n\n}\n}\n\n" - "Alternative indentation on line after condition" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif ($here) {\n if ($wasHere)\n {\n \n }\n}\n\n")))) - - (phps-mode-test--with-buffer - "<?php\nif ($myCondition)\n{\n$var = array(\n'was here'\n);\n// Was here\n}\n" - "Alternative indentation on line after array declaration" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif ($myCondition)\n{\n $var = array(\n 'was here'\n );\n // Was here\n}\n" - )))) - - (phps-mode-test--with-buffer - "<?php\nif ($myCondition == 2) {\necho 'store_vars: <pre>' . print_r($store_vars, true) . '</pre>';\necho 'search_ids: <pre>' . print_r($search_ids, true) . '</pre>';\n}" - "Alternative indentation on line echo" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif ($myCondition == 2) {\n echo 'store_vars: <pre>' . print_r($store_vars, true) . '</pre>';\n echo 'search_ids: <pre>' . print_r($search_ids, true) . '</pre>';\n}" - )))) - - (phps-mode-test--with-buffer - "<?php\nif (is_array(\n$array\n)) {\necho 'was here';\n}" - "Alternative indentation after trailing opening bracket while closing two earlier on line" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif (is_array(\n $array\n)) {\n echo 'was here';\n}" - )))) - - (phps-mode-test--with-buffer - "<?php\n\n$var = array(\n'123' =>\n'def',\n);" - "Alternative indentation on lines after lines ending with T_DOUBLE_ARROW" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\n\n$var = array(\n '123' =>\n 'def',\n);" - )))) - - (phps-mode-test--with-buffer - "<?php\n$var = array(\n'123' => true,\n\n);" - "Alternative indentation after comma ended double arrow assignment" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\n$var = array(\n '123' => true,\n \n);" - )))) - - (phps-mode-test--with-buffer - "<?php\nfunction myFunction(\n$arg = true,\n$arg2 = false\n) {\n\n}" - "Line after function argument with default value" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nfunction myFunction(\n $arg = true,\n $arg2 = false\n) {\n \n}" - )))) - - (phps-mode-test--with-buffer - "$random = get_post_meta(\n $postId,\n '_random', // TODO Here\n true // TODO Here\n );" - "Line in multi-line function call" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "$random = get_post_meta(\n $postId,\n '_random', // TODO Here\n true // TODO Here\n);" - )))) - - (phps-mode-test--with-buffer - "$cartPrice = round(\n $cartPrice,\n2 // TODO Here\n);" - "Assignment with multi-line function call" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "$cartPrice = round(\n $cartPrice,\n 2 // TODO Here\n);" - )))) - - (phps-mode-test--with-buffer - "$applications =\n $transaction->getResponseBodyDecoded();\n // TODO Here\n" - "Line after multi-line assignment with object-operator" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "$applications =\n $transaction->getResponseBodyDecoded();\n// TODO Here\n" - )))) - - (phps-mode-test--with-buffer - "<?php\necho '<dl><dt>' . __('Data', 'something')\n . ':</dt><dd><pre>' . print_r($decodedData, true) . '</pre></dd></dl>';\necho '<div class=\"meta actions\">';\n" - "Two echo statements, one spans two lines" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\necho '<dl><dt>' . __('Data', 'something')\n . ':</dt><dd><pre>' . print_r($decodedData, true) . '</pre></dd></dl>';\necho '<div class=\"meta actions\">';\n " - )))) - - (phps-mode-test--with-buffer - "<?php\nif ($shippingMethod->id ===\n \\MyClass::METHOD_ID\n ) {\n" - "Multi-line if statement testing equality in two lines" - (phps-mode-test-indent--indent-whole-buffer) - (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) - (should (equal - buffer-contents - "<?php\nif ($shippingMethod->id ===\n \\MyClass::METHOD_ID\n) {\n " - )))) - - (phps-mode-test--with-buffer - "" - "Multi-line if block after opening parenthesis" - (execute-kbd-macro "<?php") - (execute-kbd-macro (kbd "<return>")) - (execute-kbd-macro "if (true) {") - (execute-kbd-macro (kbd "<return>")) - (execute-kbd-macro "if (") - (execute-kbd-macro (kbd "<return>")) - (let ((buffer-contents - (buffer-substring-no-properties - (point-min) - (point-max)))) - (should (equal - buffer-contents - "<?php\nif (true) {\n if (\n \n )\n}" - )))) - - ) - (defun phps-mode-test-indent () "Run test for functions." ;; (setq debug-on-error t) @@ -890,8 +541,7 @@ (phps-mode-test-indent--get-lines-indent-multi-line-assignments) (phps-mode-test-indent--get-lines-indent-switch-case) (phps-mode-test-indent--get-lines-indent-psr-2) - (phps-mode-test-indent--get-lines-indent) - (phps-mode-test-indent--indent-line)) + (phps-mode-test-indent--get-lines-indent)) (phps-mode-test-indent)