[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode c678fc1 209/405: New algorithm handles HEREDO
From: |
Stefan Monnier |
Subject: |
[elpa] externals/phps-mode c678fc1 209/405: New algorithm handles HEREDOC indentation |
Date: |
Sat, 13 Jul 2019 10:00:14 -0400 (EDT) |
branch: externals/phps-mode
commit c678fc1bd47bff781ccf266af8dc2cd4ab8da04d
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
New algorithm handles HEREDOC indentation
---
docs/indentation-algorithm.md | 15 +++++++++++++++
phps-mode-functions.el | 29 +++++++++++++++++++----------
phps-mode-test-functions.el | 7 +++++++
3 files changed, 41 insertions(+), 10 deletions(-)
diff --git a/docs/indentation-algorithm.md b/docs/indentation-algorithm.md
index 42667c7..9d41b0c 100644
--- a/docs/indentation-algorithm.md
+++ b/docs/indentation-algorithm.md
@@ -18,6 +18,14 @@ Here follows pseudo-code for a algorithm that calculates
indentation for each li
```php
foreach token in buffer:
+ if token = T_START_HEREDOC:
+ in-heredoc = true;
+ in-heredoc-started-this-line = true;
+ elseif token == T_END_HEREDOC:
+ in-heredoc = false;
+ in-heredoc-ended-this-line = true;
+ endif;
+
calculate nesting-end;
if nesting-stack AND nesting-end <= nesting-stack-start: // #decrease
@@ -43,6 +51,10 @@ foreach token in buffer:
indent-start = temp-pre-indent;
endif;
+ if (in-heredoc AND !in-heredoc-started-this-line) OR
in-heredoc-ended-this-line:
+ indent-start = 0;
+ endif;
+
save line indent-start; // #save
if temp-post-indent: #temp-post-indent
@@ -57,6 +69,9 @@ foreach token in buffer:
push (nesting-stack-end nesting-end) to stack;
indent++;
endif;
+
+ in-heredoc-started-this-line = false;
+ in-heredoc-ended-this-line = false;
endif;
endforeach;
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index 2f9974c..64545f0 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -52,6 +52,8 @@
(message "\nCalculation indentation for all lines in buffer:\n\n%s"
(buffer-substring-no-properties (point-min) (point-max))))
(let ((in-scripting nil)
(in-heredoc nil)
+ (in-heredoc-started-this-line nil)
+ (in-heredoc-ended-this-line nil)
(in-inline-control-structure nil)
(after-special-control-structure nil)
(after-special-control-structure-token nil)
@@ -312,8 +314,7 @@
(progn
(setq in-assignment nil)
(setq in-assignment-level 0))
- (when (and first-token-on-line
- (not in-heredoc))
+ (when first-token-on-line
(setq in-assignment-level 1)
;; (message "In assignment on new-line at %s" token)
))
@@ -329,12 +330,6 @@
(setq after-extra-special-control-structure t)
(setq after-extra-special-control-structure-first-on-line
first-token-on-line))
- ;; Keep track of whether we are inside a HEREDOC or NOWDOC
- (when (equal token 'T_START_HEREDOC)
- (setq in-heredoc t))
- (when (equal token 'T_END_HEREDOC)
- (setq in-heredoc nil))
-
)
(when token
@@ -342,6 +337,14 @@
;; Calculate nesting
(setq nesting-end (+ round-bracket-level square-bracket-level
curly-bracket-level alternative-control-structure-level in-assignment-level
in-class-declaration-level))
+ ;; Keep track of whether we are inside a HEREDOC or NOWDOC
+ (when (equal token 'T_START_HEREDOC)
+ (setq in-heredoc t)
+ (setq in-heredoc-started-this-line t))
+ (when (equal token 'T_END_HEREDOC)
+ (setq in-heredoc nil)
+ (setq in-heredoc-ended-this-line t))
+
;; Has nesting increased?
(when (and nesting-stack
(<= nesting-end (car (car nesting-stack))))
@@ -397,6 +400,11 @@
(setq temp-pre-indent nil))
+ ;; HEREDOC lines should have zero indent
+ (when (or (and in-heredoc
+ (not in-heredoc-started-this-line))
+ in-heredoc-ended-this-line)
+ (setq column-level-start 0))
;; Save line indent
@@ -422,7 +430,7 @@
(let ((token-line-number-diff (1- (-
token-end-line-number token-start-line-number))))
(while (>= token-line-number-diff 0)
- (puthash (- token-end-line-number
token-line-number-diff) `(,column-level ,tuning-level) line-indents)
+ (puthash (- token-end-line-number
token-line-number-diff) `(,column-level-start ,tuning-level) line-indents)
;; (message "Saved line %s indent %s %s" (-
token-end-line-number token-line-number-diff) column-level tuning-level)
(setq token-line-number-diff (1-
token-line-number-diff))))
@@ -434,7 +442,7 @@
(setq column-level temp-post-indent)
(setq temp-post-indent nil))
- ;; Decrease indentation
+ ;; Increase indentation
(when (and (> nesting-end 0)
(or (not nesting-stack)
(> nesting-end (car (cdr (car
nesting-stack))))))
@@ -482,6 +490,7 @@
(setq in-assignment-started-this-line nil)
(setq changed-nesting-stack-in-line nil)
(setq class-declaration-started-this-line nil)
+ (setq in-heredoc-started-this-line nil)
(setq special-control-structure-started-this-line
nil)))
;; Current token is not first
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index c5c3363..c94606b 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -57,6 +57,13 @@
"Round and square bracket expressions"
(should (equal '((1 (0 0)) (2 (0 0)) (3 (1 0)) (4 (1 0)) (5 (2 0)) (6 (1
0)) (7 (1 0)) (8 (0 0))) (phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
+ (phps-mode-test-with-buffer
+ "<?php\nvar_dump(array(<<<EOD\nfoobar!\nEOD\n));\n?>"
+ "HEREDOC in arguments example"
+ ;; (message "Tokens: %s" phps-mode-lexer-tokens)
+ (should (equal '((1 (0 0)) (2 (0 0)) (3 (0 0)) (4 (0 0)) (5 (0 0)) (6 (0
0))) (phps-mode-test-functions--hash-to-list
(phps-mode-functions-get-lines-indent)))))
+
+
(phps-mode-test-with-buffer
"<?php\n$str = <<<'EOD'\nExample of string\nspanning multiple lines\nusing
nowdoc syntax.\nEOD;\n"
"Multi-line NOWDOC string"
- [elpa] externals/phps-mode c5d8ec6 168/405: Indentation calculation for class implement working, (continued)
- [elpa] externals/phps-mode c5d8ec6 168/405: Indentation calculation for class implement working, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f93765d 184/405: Improved code structure of indentation were nesting start and end equals, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 2af8abd 206/405: Made TODO notes in algorithm, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 7a538ac 200/405: Tests for alternative control structures passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 79e61f4 171/405: Fixed indentation with assignment lines on only one line, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode d18fddc 175/405: Fixed bug with indentation for indented doc-comments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 00c0ab9 194/405: Nesting-stack algorithm handles class declaration indents, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 8a76039 189/405: Started implementing nesting-stack, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode cf7dc07 210/405: Preparations for multi-line quoted indentation tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 208a788 208/405: Re-organized indent tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode c678fc1 209/405: New algorithm handles HEREDOC indentation,
Stefan Monnier <=
- [elpa] externals/phps-mode b6cf3a4 191/405: Started on new algorithm for indentation calculation based on stack, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b2338cd 223/405: All tests for indentation passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 7a4b98e 215/405: More work on algorithm description, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 9031c3a 211/405: Merge branch 'master' of https://github.com/cjohansson/emacs-phps-mode, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 6105e5d 226/405: Added a new failing test for indetation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 5f445cd 229/405: Fixed indentation for while expressions, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode eb0ee5e 236/405: Updated README with imenu-support, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 811c499 220/405: New algorithm now works for switch, case syntax, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode d2e5a03 228/405: Updated indentation algorithm document and README, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 1aa0f90 238/405: Typo in README, Stefan Monnier, 2019/07/13