[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode a2c61b9e7c 174/212: Started refactoring of in
From: |
Christian Johansson |
Subject: |
[elpa] externals/phps-mode a2c61b9e7c 174/212: Started refactoring of indentation functions |
Date: |
Wed, 26 Jan 2022 01:51:22 -0500 (EST) |
branch: externals/phps-mode
commit a2c61b9e7c76edd747905400340e81741f5e9d79
Author: Christian Johansson <christian@cvj.se>
Commit: Christian Johansson <christian@cvj.se>
Started refactoring of indentation functions
---
phps-mode-indent.el | 663 ++++++++++++++++++----------------------------------
1 file changed, 224 insertions(+), 439 deletions(-)
diff --git a/phps-mode-indent.el b/phps-mode-indent.el
index 5210ccb3d7..59013acd38 100644
--- a/phps-mode-indent.el
+++ b/phps-mode-indent.el
@@ -764,115 +764,36 @@
match-type
'line-after-line-ending-with-comma)
- ;; Backtrack first to line were bracket started
- ;; and use indentation from that line from that line
(forward-line (* -1 move-length1))
(end-of-line)
(search-backward-regexp "," nil t) ;; Skip trailing comma
- (let ((not-found-bracket-start t)
- (reference-line)
- (found-colon)
- (reference-line-started-bracket)
- (parenthesis-level 0))
- (while
- (and
- not-found-bracket-start
- (search-backward-regexp
- "\\([][(),]\\|=>\\)"
- nil
- t))
- (let ((match (match-string-no-properties 0)))
- (cond
-
- ((or
- (string= "(" match)
- (string= "[" match))
- (setq
- parenthesis-level
- (1+ parenthesis-level))
- (when (= parenthesis-level 1)
- (unless found-colon
- (setq
- reference-line-started-bracket
- t)
- (setq
- reference-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position))))
- (setq
- not-found-bracket-start
- nil)))
-
- ((or
- (string= ")" match)
- (string= "]" match))
- (setq
- parenthesis-level
- (1- parenthesis-level)))
-
- ;; The second occurence of a colon
- ;; is a significant marker of
- ;; a starting bracket row
- ((string= "," match)
- (when (= parenthesis-level 0)
- (if found-colon
- (setq
- not-found-bracket-start
- nil)
- (setq
- found-colon
- t)
- (setq
- reference-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position))))))
-
- ;; The first occurrence of a =>
- ;; is a significant marker of
- ;; a starting bracket row
- ((string= "=>" match)
- (when (= parenthesis-level 0)
- (setq
- reference-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position)))
- (setq
- not-found-bracket-start
- nil)))
-
- )))
-
- (when reference-line
- ;; (message "reference-line-2: %S" reference-line)
- (setq
- new-indentation
- (phps-mode-indent--string-indentation
- reference-line))
- (when reference-line-started-bracket
- (let ((reference-bracket-level
- (phps-mode-indent--get-string-brackets-count
- reference-line)))
- ;; (message "reference-bracket-level: %S"
reference-bracket-level)
- ;; define('_PRIVATE_ROOT',
- ;; 'here');
- ;; or
- ;; ['abc',
- ;; 'def'];
- (when (> reference-bracket-level 0)
- (setq
- new-indentation
- (+ new-indentation tab-width)))))
- )
- (when current-line-starts-with-closing-bracket
- (setq
- new-indentation
- (- new-indentation tab-width))))
+ (when-let
+ ((reference-line
+ (phps-mode-indent--get-previous-reference-index-line)))
+ ;; (message "reference-line-2: %S" reference-line)
+ (setq
+ new-indentation
+ (phps-mode-indent--string-indentation
+ reference-line))
+ (let ((reference-bracket-level
+ (phps-mode-indent--get-string-brackets-count
+ reference-line)))
+ ;; (message "reference-bracket-level: %S"
reference-bracket-level)
+ ;; define('_PRIVATE_ROOT',
+ ;; 'here');
+ ;; or
+ ;; ['abc',
+ ;; 'def'];
+ (when (> reference-bracket-level 0)
+ (setq
+ new-indentation
+ (+ new-indentation tab-width)))))
- (goto-char point))
+ (when current-line-starts-with-closing-bracket
+ (setq
+ new-indentation
+ (- new-indentation tab-width))))
;; LINE AFTER LINE THATS ENDS WITH SEMICOLON
;; $var .=
@@ -911,69 +832,22 @@
match-type
'line-after-line-that-ends-with-semicolon)
- ;; Back-trace buffer from previous line semi-colon
- ;; find line where command started
- ;; use that lines indentation for this line
(forward-line (* -1 move-length1))
(end-of-line)
(search-backward-regexp ";" nil t) ;; Skip the semi-colon
- (let ((not-found t)
- (reference-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position)))
- (reference-indentation))
- (while
- (and
- not-found
- (search-backward-regexp
- "^[\t ]*[^\t ]+.*$"
- nil
- t))
- (let ((match (match-string-no-properties 0)))
- (cond
-
- ;; Commented out line
- ((string-match-p
- "^[\t ]*//"
- match))
-
- ;; A separate command
- ((or
- (string-match-p
- "{[\t ]*$"
- match)
- (string-match-p
- "\\(;\\|:\\)[\t ]*$"
- match)
- (string-match-p
- "[\t ]*<\\?"
- match))
- (setq
- not-found
- nil))
-
- (t
- (setq
- reference-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position))))
-
- )))
-
- (goto-char point)
+ (when-let
+ ((reference-line
+ (phps-mode-indent--get-previous-reference-command-line)))
- (unless not-found
- ;; (message "reference-line: %S" reference-line)
- (setq
- reference-indentation
- (phps-mode-indent--string-indentation
- reference-line))
- (setq
- new-indentation
- reference-indentation))))
+ ;; (message "reference-line: %S" reference-line)
+ (setq
+ reference-indentation
+ (phps-mode-indent--string-indentation
+ reference-line))
+ (setq
+ new-indentation
+ reference-indentation)))
;; LINE AFTER ALTERNATIVE CASE DEFINITION
;; switch ($array):
@@ -1078,67 +952,17 @@
(setq
match-type
'line-that-starts-with-closing-bracket)
-
- ;; Backtrack to line were bracket started
- ;; and use indentation from that line for this line
- (forward-line (* -1 move-length1))
(end-of-line)
- (let ((not-found t)
- (reference-line)
- (reference-indentation)
- (parenthesis-level -1))
- (while
- (and
- not-found
- (search-backward-regexp
- "[][(){}]"
- nil
- t))
- (let ((match (match-string-no-properties 0)))
- (cond
-
- ((or
- (string= "(" match)
- (string= "[" match)
- (string= "{" match))
- (setq
- parenthesis-level
- (1+ parenthesis-level))
- (when (= parenthesis-level 0)
- (setq
- not-found
- nil)))
-
- ((or
- (string= ")" match)
- (string= "]" match)
- (string= "}" match))
- (setq
- parenthesis-level
- (1- parenthesis-level))
- (when (= parenthesis-level 0)
- (setq
- not-found
- nil)))
-
- )))
- (unless not-found
- (setq
- reference-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position)))
- (setq
- reference-indentation
- (phps-mode-indent--string-indentation
- reference-line)))
-
- (goto-char point)
-
- (when reference-indentation
- (setq
- new-indentation
- reference-indentation))))
+ (when-let
+ ((reference-line
+ (phps-mode-indent--get-previous-reference-index-line)))
+ (setq
+ reference-indentation
+ (phps-mode-indent--string-indentation
+ reference-line))
+ (setq
+ new-indentation
+ reference-indentation)))
;; LINE AFTER LINE INSIDE DOC-COMMENT
;; /**
@@ -1198,109 +1022,60 @@
(setq
match-type
'line-after-line-that-ends-with-closing-bracket)
- ;; Backtrack to line were bracket started
- ;; and use indentation from that line for this line
(forward-line (* -1 move-length1))
(end-of-line)
- (let ((not-found t)
- (reference-line)
- (reference-line2)
- (reference-indentation)
- (parenthesis-level 0))
- (while
- (and
- not-found
- (search-backward-regexp
- "[][(){}]"
- nil
- t))
- (let ((match (match-string-no-properties 0)))
- (cond
-
- ((or
- (string= "(" match)
- (string= "[" match)
- (string= "{" match))
- (setq
- parenthesis-level
- (1+ parenthesis-level))
- (when (= parenthesis-level 0)
- (setq
- not-found
- nil)))
-
- ((or
- (string= ")" match)
- (string= "]" match)
- (string= "}" match))
- (setq
- parenthesis-level
- (1- parenthesis-level))
- (when (= parenthesis-level 0)
- (setq
- not-found
- nil)))
-
- )))
- (unless not-found
- (setq
- reference-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position)))
- (setq
- reference-line2
- (buffer-substring-no-properties
- (point)
- (line-end-position)))
- (setq
- reference-indentation
- (phps-mode-indent--string-indentation
- reference-line))
- (setq
- new-indentation
- reference-indentation)
- (let ((reference-bracket-level
- (phps-mode-indent--get-string-brackets-count
- reference-line))
- (reference-bracket-level2
- (phps-mode-indent--get-string-brackets-count
- reference-line2))
- (reference-contains-assignment
- (string-match-p
- "^[\t ]*$[a-zA-Z0-9_]+[\t ]*[^=!]*=\\($\\|[\t
]+.*[^,;]$\\)"
- reference-line)))
- ;; if (
- ;; (is_array($data)
- ;; && !empty($data['index'])
- ;; && (is_a($data['index'], 'Index')
- ;; || is_a($data['Index'], 'Index2')))
- ;; || is_a($data, 'WC_Index')
- ;; or
- ;; $copies = method_exists($object, 'get_copies')
- ;; ? true
- ;; (message "reference-bracket-level: %S"
reference-bracket-level)
- ;; (message "reference-bracket-level2: %S"
reference-bracket-level2)
-
- (when (or
- reference-contains-assignment
- (and
- (> reference-bracket-level 0)
- (> reference-bracket-level
reference-bracket-level2)))
- (setq
- new-indentation
- (+ new-indentation tab-width))))
-
- (when current-line-starts-with-closing-bracket
+ (when-let
+ ((reference-line
+ (phps-mode-indent--get-previous-reference-index-line)))
+ (setq
+ reference-line2
+ (buffer-substring-no-properties
+ (point)
+ (line-end-position)))
+ (setq
+ reference-indentation
+ (phps-mode-indent--string-indentation
+ reference-line))
+ (setq
+ new-indentation
+ reference-indentation)
+ (let ((reference-bracket-level
+ (phps-mode-indent--get-string-brackets-count
+ reference-line))
+ (reference-bracket-level2
+ (phps-mode-indent--get-string-brackets-count
+ reference-line2))
+ (reference-contains-assignment
+ (string-match-p
+ "^[\t ]*$[a-zA-Z0-9_]+[\t ]*[^=!]*=\\($\\|[\t
]+.*[^,;]$\\)"
+ reference-line)))
+ ;; if (
+ ;; (is_array($data)
+ ;; && !empty($data['index'])
+ ;; && (is_a($data['index'], 'Index')
+ ;; || is_a($data['Index'], 'Index2')))
+ ;; || is_a($data, 'WC_Index')
+ ;; or
+ ;; $copies = method_exists($object, 'get_copies')
+ ;; ? true
+ ;; (message "reference-bracket-level: %S"
reference-bracket-level)
+ ;; (message "reference-bracket-level2: %S"
reference-bracket-level2)
+
+ (when (or
+ reference-contains-assignment
+ (and
+ (> reference-bracket-level 0)
+ (> reference-bracket-level
reference-bracket-level2)))
(setq
new-indentation
- (- new-indentation tab-width)))
-
- )
+ (+ new-indentation tab-width))))
- (goto-char point))
+ (when current-line-starts-with-closing-bracket
+ (setq
+ new-indentation
+ (- new-indentation tab-width)))
- )
+ ))
;; LINE AFTER OPENING MULTI-LINE ASSIGNMENT
;; $var = 'A line' .
@@ -1511,126 +1286,6 @@
new-indentation
(+ new-indentation tab-width)))
- ;; LINE AFTER ENDING OF BRACKET AND COMMAND
- ;; if (true) {
- ;; $cacheKey = sprintf(
- ;; 'key_%s',
- ;; md5(json_encode($key))
- ;; );
- ;; $cache =
- ;; or
- ;; if (true) {
- ;; $cache =
- ;; Cache::getInstance();
- ;; echo 'here';
- ((string-match-p
- "[])][\t ]*;[\t ]*\\(\\?>[\t\n ]*\\)?$"
- previous-line-string)
- (setq
- match-type
- 'line-after-ending-of-bracket-and-command)
-
- ;; Backtrack first to line were bracket started
- ;; and then backwards until the line were statement /
expression
- ;; started and use indentation from that line from that line
- (forward-line (* -1 move-length1))
- (end-of-line)
- (search-backward-regexp ";" nil t) ;; Skip trailing comma
- (let ((not-found-bracket-start t)
- (reference-line)
- (parenthesis-level 0))
- (while
- (and
- not-found-bracket-start
- (search-backward-regexp
- "[][()]"
- nil
- t))
- (let ((match (match-string-no-properties 0)))
- (cond
-
- ((or
- (string= "(" match)
- (string= "[" match))
- (setq
- parenthesis-level
- (1+ parenthesis-level))
- (when (= parenthesis-level 0)
- (setq
- not-found-bracket-start
- nil)))
-
- ((or
- (string= ")" match)
- (string= "]" match))
- (setq
- parenthesis-level
- (1- parenthesis-level))
- (when (= parenthesis-level 0)
- (setq
- not-found-bracket-start
- nil)))
-
- )))
-
- ;; Found line were bracket started?
- (unless not-found-bracket-start
- (setq
- reference-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position)))
- ;; (message "reference-line-1: %S" reference-line)
-
- ;; Search for first line of statement / expression here
- (let ((not-found-command-start t))
- (while
- (and
- not-found-command-start
- (search-backward-regexp
- "\\(;\\|}\\|{\\|^[\t ]*[^\t\n ]+.*$\\)"
- nil
- t))
- (let ((match (match-string-no-properties 1)))
- ;; (message "match: %S" match)
- (cond
-
- ;; End of expression / statement
- ((or
- (string= ";" match)
- (string= "}" match)
- (string= "{" match))
- (setq
- not-found-command-start
- nil))
-
- ;; Non-empty line
- (t
- (setq
- reference-line
- (buffer-substring-no-properties
- (line-beginning-position)
- (line-end-position))))
-
- )))))
-
- (when reference-line
- ;; (message "reference-line-2: %S" reference-line)
- (setq
- new-indentation
- (phps-mode-indent--string-indentation
- reference-line)))
-
- (when
- (and
- current-line-starts-with-closing-bracket
- (string= current-line-starts-with-closing-bracket "}"))
- (setq
- new-indentation
- (- new-indentation tab-width))))
-
- (goto-char point))
-
)
(phps-mode-debug-message
@@ -1689,6 +1344,136 @@
(setq bracket-level (- bracket-level tab-width)))))))
bracket-level))
+(defun phps-mode-indent--get-previous-reference-index-line ()
+ "Get previous index line as reference, if any exist."
+ (let ((reference-line)
+ (old-point (point)))
+ (let ((not-found-bracket-start t)
+ (found-colon)
+ (reference-line-started-bracket)
+ (parenthesis-level 0))
+ (while
+ (and
+ not-found-bracket-start
+ (search-backward-regexp
+ "\\([][(),]\\|=>\\)"
+ nil
+ t))
+ (let ((match (match-string-no-properties 0)))
+ (cond
+
+ ((or
+ (string= "(" match)
+ (string= "[" match))
+ (setq
+ parenthesis-level
+ (1+ parenthesis-level))
+ (when (= parenthesis-level 1)
+ (unless found-colon
+ (setq
+ reference-line-started-bracket
+ t)
+ (setq
+ reference-line
+ (buffer-substring-no-properties
+ (line-beginning-position)
+ (line-end-position))))
+ (setq
+ not-found-bracket-start
+ nil)))
+
+ ((or
+ (string= ")" match)
+ (string= "]" match))
+ (setq
+ parenthesis-level
+ (1- parenthesis-level)))
+
+ ;; The second occurrence of a colon
+ ;; is a significant marker of
+ ;; a starting bracket row
+ ((string= "," match)
+ (when (= parenthesis-level 0)
+ (if found-colon
+ (setq
+ not-found-bracket-start
+ nil)
+ (setq
+ found-colon
+ t)
+ (setq
+ reference-line
+ (buffer-substring-no-properties
+ (line-beginning-position)
+ (line-end-position))))))
+
+ ;; The first occurrence of a =>
+ ;; is a significant marker of
+ ;; a starting bracket row
+ ((string= "=>" match)
+ (when (= parenthesis-level 0)
+ (setq
+ reference-line
+ (buffer-substring-no-properties
+ (line-beginning-position)
+ (line-end-position)))
+ (setq
+ not-found-bracket-start
+ nil)))
+
+ )))
+
+ (goto-char old-point)
+ reference-line)))
+
+(defun phps-mode-indent--get-previous-reference-command-line ()
+ "Get previous line that is a command (if any)."
+ (let ((not-found t)
+ (old-point (point))
+ (reference-line))
+
+ (while
+ (and
+ not-found
+ (search-backward-regexp
+ "^[\t ]*[^\t ]+.*$"
+ nil
+ t))
+ (let ((match (match-string-no-properties 0)))
+ (cond
+
+ ;; Commented out line
+ ((string-match-p
+ "^[\t ]*//"
+ match))
+
+ ;; A separate command
+ ((or
+ (string-match-p
+ "{[\t ]*$"
+ match)
+ (string-match-p
+ "\\(;\\|:\\)[\t ]*$"
+ match)
+ (string-match-p
+ "[\t ]*<\\?"
+ match))
+ (setq
+ not-found
+ nil))
+
+ (t
+ (setq
+ reference-line
+ (buffer-substring-no-properties
+ (line-beginning-position)
+ (line-end-position))))
+
+ )))
+
+ (goto-char old-point)
+ reference-line))
+
(provide 'phps-mode-indent)
- [elpa] externals/phps-mode 2ef1168cb1 155/212: Added new failing indentation tests, (continued)
- [elpa] externals/phps-mode 2ef1168cb1 155/212: Added new failing indentation tests, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 519888fe79 150/212: Added another failing indent test, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 7ec071188a 137/212: Added more indent unit tests for nested function calls, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 837412c0e5 140/212: Improved indentation on lines starting with a closing bracket, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode cdbcd5ebe6 154/212: Added a new failing indent test and made it pass, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode c2a7af2a98 142/212: Updated copyright years, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 91fb5402cd 158/212: Added another failing indentation and made it pass, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 66d4352052 169/212: Added more failing indent tests, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 0ca0e447b8 167/212: Improved indentation on lines starting with a closing bracket, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 18221a85f0 177/212: Added failing parse test for multiple swith case conditions, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode a2c61b9e7c 174/212: Started refactoring of indentation functions,
Christian Johansson <=
- [elpa] externals/phps-mode e15d602e46 179/212: More TODO items, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 22c4f31716 178/212: Added TODO items, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode c05d97fc31 182/212: Disabled debug-mode in this branch, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode d095e757e6 186/212: Added failing imenu test, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 2dc9280bcd 185/212: More work, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 87974abfcc 190/212: Improved indentation after lines containing => symbol, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode e049853a6f 193/212: Remove use of lex-analyzer from semantic and instead use custom, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 0e3889a122 202/212: Added indentation support for HTML/XML, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode d85974c2aa 196/212: Improved indentation inside multi-dimensional arrays, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 057aadd2f6 197/212: Improved indentation around multi-line logical expressions, Christian Johansson, 2022/01/26