[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode 1472273 136/405: Begun working on new approac
From: |
Stefan Monnier |
Subject: |
[elpa] externals/phps-mode 1472273 136/405: Begun working on new approach to indentation calculation |
Date: |
Sat, 13 Jul 2019 10:00:00 -0400 (EDT) |
branch: externals/phps-mode
commit 147227369049450769197f938c9682d5102c2019
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
Begun working on new approach to indentation calculation
---
phps-mode-functions.el | 99 +++++++++++++++++++++++++++++++++++++++++++++
phps-mode-test-functions.el | 8 +++-
2 files changed, 106 insertions(+), 1 deletion(-)
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index 12de21a..583585e 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -38,6 +38,105 @@
;; TODO Support inline function indentations
;; TODO Support indentation for multi-line scalar assignments
+(defun phps-mode-functions-get-current-line-indent ()
+ "Get the column number for current line."
+ (if (boundp 'phps-mode-lexer-tokens)
+ (save-excursion
+ (beginning-of-line)
+ (let ((line-beginning (line-beginning-position))
+ (line-end (line-end-position))
+ (in-scripting nil)
+ (in-nowdoc nil)
+ (in-heredoc nil)
+ (in-doc-block nil)
+ (curly-bracket-level 0)
+ (round-bracket-level 0)
+ (square-bracket-level 0)
+ (indent-level 0)
+ (indent-start 0)
+ (indent-end 0)
+ (last-line-number 0))
+
+ ;; Iterate through all buffer tokens from beginning to end
+ (catch 'stop-iteration
+ (dolist (item phps-mode-lexer-tokens)
+ (let* ((token (car item))
+ (token-start (car (cdr item)))
+ (token-end (cdr (cdr item)))
+ (token-line-number (line-number-at-pos token-start t)))
+
+ ;; Break when token starts after current line end
+ (when (> token-start line-end)
+ (throw 'stop-iteration nil))
+
+ ;; Are we on a new line?
+ (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))
+
+ ;; Is line ending indentation higher than line beginning
indentation?
+ (when (> indent-end indent-start)
+
+ ;; Increase indentation by one
+ (setq indent-level (1+ indent-level)))
+
+ ;; Is line ending indentation lesser than line beginning
indentation?
+ (when (< indent-end indent-start)
+
+ ;; Decrease indentation by one
+ (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)))
+
+ ;; Keep track of round bracket level
+ (when (string= token "(")
+ (setq round-bracket-level (1+ round-bracket-level)))
+ (when (string= token ")")
+ (setq round-bracket-level (1- round-bracket-level)))
+
+ ;; Keep track of square bracket level
+ (when (string= token "[")
+ (setq square-bracket-level (1+ square-bracket-level)))
+ (when (string= token ")")
+ (setq square-bracket-level (1- square-bracket-level)))
+
+ ;; Keep track of curly bracket level
+ (when (or (equal token 'T_CURLY_OPEN)
+ (equal token 'T_DOLLAR_OPEN_CURLY_BRACES)
+ (string= token "{"))
+ (setq curly-bracket-level (1+ curly-bracket-level)))
+ (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 in scripting
+ (when (or (equal token 'T_OPEN_TAG)
+ (equal token 'T_OPEN_TAG_WITH_ECHO))
+ (setq in-scripting t))
+ (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
+
+ ;; Are we on a new line?
+ (when (> token-line-number last-line-number)
+
+ ;; Update last line number
+ (setq last-line-number token-line-number))
+
+ )))
+
+ (if in-scripting
+ indent-level
+ nil)))
+ nil))
+
(defun phps-mode-functions-indent-line ()
"Indent line."
(let ((data (phps-mode-functions-get-current-line-data))
diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el
index 430ec92..c158bba 100644
--- a/phps-mode-test-functions.el
+++ b/phps-mode-test-functions.el
@@ -34,6 +34,11 @@
(autoload 'phps-mode-functions-get-current-line-data "phps-mode-functions")
(autoload 'should "ert")
+(defun phps-mode-test-functions-get-current-line-indent ()
+ "Test `phps-mode-functions-get-current-line-indent' function."
+
+ )
+
;; TODO Add unit tests for HEREDOC and NOWDOC regions as well
(defun phps-mode-test-functions-indent-line ()
@@ -245,6 +250,7 @@
)
+;; TODO Remove this functions
(defun phps-mode-test-functions-get-current-line-data ()
"Return information about point in tokens."
@@ -410,7 +416,7 @@
(defun phps-mode-test-functions ()
"Run test for functions."
- ;; (toggle-debug-on-error t)
+ (phps-mode-test-functions-get-current-line-indent)
(phps-mode-test-functions-get-current-line-data)
(phps-mode-test-functions-indent-line))
- [elpa] externals/phps-mode a8eadfa 130/405: Improved sample files inline and alternative control structures, (continued)
- [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, 2019/07/13
- [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 <=
- [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
- [elpa] externals/phps-mode 0b3ced4 145/405: First test for new indentation function passes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 96588b5 142/405: More work on new indentation calculation functions, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 9d6af11 157/405: More work on indentations, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e249124 152/405: Prevent negative indent, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 4a27c44 147/405: Added support for naming unit tests to better find failures, Stefan Monnier, 2019/07/13