[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode f0523b7 137/405: More work on new function to
From: |
Stefan Monnier |
Subject: |
[elpa] externals/phps-mode f0523b7 137/405: More work on new function to calculate indentation |
Date: |
Sat, 13 Jul 2019 10:00:00 -0400 (EDT) |
branch: externals/phps-mode
commit f0523b7a573af339a31cbb39f7c99efee847e7b5
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
More work on new function to calculate indentation
---
phps-mode-functions.el | 54 ++++++++++++++++++++++++++++++++++++---------
phps-mode-test-functions.el | 21 ++++++++++++++++++
2 files changed, 65 insertions(+), 10 deletions(-)
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index 583585e..080c351 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -46,12 +46,15 @@
(let ((line-beginning (line-beginning-position))
(line-end (line-end-position))
(in-scripting nil)
- (in-nowdoc nil)
(in-heredoc nil)
- (in-doc-block nil)
+ (in-doc-comment nil)
+ (in-inline-control-structure nil)
+ (after-special-control-structure nil)
(curly-bracket-level 0)
(round-bracket-level 0)
(square-bracket-level 0)
+ (alternative-control-structure-level 0)
+ (inline-control-structure-level 0)
(indent-level 0)
(indent-start 0)
(indent-end 0)
@@ -73,7 +76,11 @@
(when (> token-line-number last-line-number)
;; Calculate indentation leven at end of line
- (setq indent-end (+ round-bracket-level square-bracket-level
curly-bracket-level))
+ (setq indent-end (+ round-bracket-level square-bracket-level
curly-bracket-level alternative-control-structure-level
inline-control-structure-level))
+
+ ;; TODO Increase indent with 1 inside doc-comment, heredoc
or nowdoc
+ (when (or in-doc-comment in-heredoc)
+ (setq indent-end (1+ indent-end)))
;; Is line ending indentation higher than line beginning
indentation?
(when (> indent-end indent-start)
@@ -85,10 +92,10 @@
(when (< indent-end indent-start)
;; Decrease indentation by one
- (setq indent-level (1- indent-level))
+ (setq indent-level (1- indent-level)))
;; Calculate indentation level at start of line
- (setq indent-start (+ round-bracket-level
square-bracket-level curly-bracket-level)))
+ (setq indent-start (+ round-bracket-level
square-bracket-level curly-bracket-level alternative-control-structure-level
inline-control-structure-level)))
;; Keep track of round bracket level
(when (string= token "(")
@@ -99,7 +106,7 @@
;; Keep track of square bracket level
(when (string= token "[")
(setq square-bracket-level (1+ square-bracket-level)))
- (when (string= token ")")
+ (when (string= token "]")
(setq square-bracket-level (1- square-bracket-level)))
;; Keep track of curly bracket level
@@ -110,8 +117,26 @@
(when (string= token "}")
(setq curly-bracket-level (1- curly-bracket-level)))
- ;; TODO Keep track of alternative control structures
;; TODO Keep track of inline control structures
+ ;; Keep track of alternative control structure level
+ (when (or (equal token 'T_ENDIF)
+ (equal token 'T_ENDWHILE)
+ (equal token 'T_ENDFOR)
+ (equal token 'T_ENDFOREACH)
+ (equal token 'T_ENDSWITCH))
+ (setq alternative-control-structure-level (1-
alternative-control-structure-level)))
+
+ ;; Did we encounter a token that supports alternative and
inline control structures?
+ (when (or (equal token 'T_IF)
+ (equal token 'T_WHILE)
+ (equal token 'T_CASE)
+ (equal token 'T_DEFAULT)
+ (equal token 'T_FOR)
+ (equal token 'T_FOREACH)
+ (equal token 'T_SWITCH)
+ (equal token 'T_ELSE)
+ (equal token 'T_ELSEIF))
+ (setq after-special-control-structure round-bracket-level))
;; Keep track of in scripting
(when (or (equal token 'T_OPEN_TAG)
@@ -120,9 +145,18 @@
(when (equal token 'T_CLOSE_TAG)
(setq in-scripting nil))
- ;; TODO Keep track of inside doc-block
- ;; TODO Keep track of inside HEREDOC
- ;; TODO Keep track of inside NOWDOC
+ ;; Keep track of whether we are inside a doc-comment
+ (when (equal token 'T_DOC_COMMENT)
+ (setq in-doc-comment token-end))
+ (when (and in-doc-comment
+ (> token-start in-doc-comment))
+ (setq in-doc-comment nil))
+
+ ;; 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))
;; Are we on a new line?
(when (> token-line-number last-line-number)
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index c158bba..ffef9fb 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -37,6 +37,27 @@
(defun phps-mode-test-functions-get-current-line-indent ()
"Test `phps-mode-functions-get-current-line-indent' function."
+ (phps-mode-test-with-buffer
+ "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition)
{\necho $title;\n\n} ?></title><body>Bla bla</body></html>"
+ (goto-char 69)
+ (should (equal 1 (phps-mode-functions-get-current-line-indent))))
+
+ (phps-mode-test-with-buffer
+ "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition)
{\necho $title;\n\n} ?></title><body>Bla bla</body></html>"
+ (goto-char 40)
+ (should (equal 0 (phps-mode-functions-get-current-line-indent))))
+
+ (phps-mode-test-with-buffer
+ "<html><head><title><?php if ($myCondition) {\nif ($mySeconCondition)
{\necho $title;\n\n} ?></title><body>Bla bla</body></html>"
+ (goto-char 75)
+ (should (equal 2 (phps-mode-functions-get-current-line-indent))))
+
+ ;; (phps-mode-test-with-buffer
+ ;; "<?php\n/**\n* Bla\n*/"
+ ;; (goto-char 13)
+ ;; (should (equal 2 (phps-mode-functions-get-current-line-indent))))
+
+
)
;; TODO Add unit tests for HEREDOC and NOWDOC regions as well
- [elpa] externals/phps-mode c6aa633 118/405: More work on keeping track of alternative control structures, (continued)
- [elpa] externals/phps-mode c6aa633 118/405: More work on keeping track of alternative control structures, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 60c7431 122/405: Unit tests for gather data about inline and alternative control structure syntax passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 897f249 124/405: Work on alternative and inline indentation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode be35ed6 127/405: Added sample files for inline and alternative control structures, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 7279913 113/405: Improved TODOs, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode a8eadfa 130/405: Improved sample files inline and alternative control structures, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 9996ae0 146/405: More work on indentation calculation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 3776841 138/405: Added new support for tracking doc-comment, HEREDOC and NOWDOC indentation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 780d065 125/405: Code styling updates, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 314dba1 135/405: Improved articulation of what information get-current-line-data should return, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f0523b7 137/405: More work on new function to calculate indentation,
Stefan Monnier <=
- [elpa] externals/phps-mode 16d8056 144/405: Foundation for new indentation tests completed, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode d380cd4 129/405: Updated readme, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 3f08b86 128/405: Updated tracking of alternative control structures, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 52c502c 139/405: Improved logic for doc-comment tracking, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 1472273 136/405: Begun working on new approach to indentation calculation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 9792f70 133/405: Cleaning up code gather point meta information, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode cc947a9 140/405: Added more tests for new indentation function, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode d3e00f2 141/405: Starting on new function for indentation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode c68c7a0 131/405: Fixed issue were first-token-on-line was nil, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode c4ee0c3 132/405: Added more failing tests, Stefan Monnier, 2019/07/13