[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode 0e3889a122 202/212: Added indentation support
From: |
Christian Johansson |
Subject: |
[elpa] externals/phps-mode 0e3889a122 202/212: Added indentation support for HTML/XML |
Date: |
Wed, 26 Jan 2022 01:51:27 -0500 (EST) |
branch: externals/phps-mode
commit 0e3889a12261f28e3f982afc64b9cdadfe82e78d
Author: Christian Johansson <christian@cvj.se>
Commit: Christian Johansson <christian@cvj.se>
Added indentation support for HTML/XML
---
TODO.md | 10 -----
phps-mode-indent.el | 102 +++++++++++++++++++++++++++++++++++++++---
test/phps-mode-test-indent.el | 45 +++++++++++++++++++
3 files changed, 141 insertions(+), 16 deletions(-)
diff --git a/TODO.md b/TODO.md
index 3832977c81..c234814e8e 100644
--- a/TODO.md
+++ b/TODO.md
@@ -2,22 +2,12 @@
## Indentation
-* Fix HTML/XML indentation support
-* Strange case
-
- $A =
- B::
- C($D, true);
- // Comment
-
-
## Code intelligence
* Bookkeeping of chained object operators like WC()->cart->subtotal
* Bookkeeping of variables inside classes with multiple methods seems to not
work
* Move bookkeeping and imenu generation to main thread to be able to populate
Sematic Subsystem in the future
* Catch signaling from AST-generation, bookkeeping and imenu generation
-* Remove support for incremental parsing if it speeds up
* Lexer/parser fix for multiple cases like
switch($here) {
diff --git a/phps-mode-indent.el b/phps-mode-indent.el
index 78e1e9abd7..deb16ecd08 100644
--- a/phps-mode-indent.el
+++ b/phps-mode-indent.el
@@ -124,17 +124,13 @@
1))
(defun phps-mode-indent--get-string-brackets-count
- (string &optional html-mode)
- "Get bracket count for STRING optionally in HTML-MODE."
+ (string)
+ "Get bracket count for STRING."
(let ((bracket-level 0)
(start 0)
(line-is-empty
(string-match-p "^[ \t\f\r\n]*$" string))
(test-string "\\([\]{}()[]\\|^[\t ]/\\*\\*\\|^[\t\\* ]*\\*/\\)"))
- (when html-mode
- (setq
- test-string
- "\\([\]{}()[]\\|<[a-zA-Z]+\\|</[a-zA-Z]+\\|/>\\|^[\t ]/\\*\\*\\|^[\t\\*
]*\\*/\\)"))
(unless line-is-empty
;; (message "string: %S" string)
(while
@@ -162,6 +158,64 @@
(setq bracket-level (- bracket-level tab-width)))))))
bracket-level))
+(defun phps-mode-indent--get-html-string-bracket-level (string)
+ "Get HTML bracket-level for STRING."
+ (let* ((html-bracket-level 0)
+ (start 0)
+ (next-item
+ (string-match
+ "\\(<[^>]+>\\)"
+ string
+ start)))
+ (while next-item
+ (let ((match (match-string 0 string)))
+ (setq
+ start
+ (match-end 0))
+
+ (cond
+
+ ;; Self-closing tag does not change indentation
+ ((string-match
+ "<\\([a-zA-Z]\\)+[^>]+/>"
+ match))
+
+ ;; Opening tag changes indentation for most tags
+ ((string-match
+ "<\\([a-zA-Z]+\\)"
+ match)
+ (let ((tag (match-string 1 match)))
+ (unless
+ (string-match-p
+ "^\\(html\\|meta\\|br\\|em\\|strong\\|i\\|b\\)$"
+ tag)
+ (setq
+ html-bracket-level
+ (1+ html-bracket-level)))))
+
+ ;; Closing tag changes indentation for most tags
+ ((string-match
+ "</\\([a-zA-Z]+\\)"
+ match)
+ (let ((tag (match-string 1 match)))
+ (unless
+ (string-match-p
+ "^\\(html\\|meta\\|br\\|em\\|strong\\|i\\|b\\)$"
+ tag)
+ (setq
+ html-bracket-level
+ (1- html-bracket-level)))))
+ )
+ (setq
+ next-item
+ (string-match
+ "\\(<[^>]+>\\)"
+ string
+ start))))
+ (if (= html-bracket-level 0)
+ nil
+ html-bracket-level)))
+
(defun phps-mode-indent--get-previous-reference-index-line ()
"Get previous index line as reference, if any exist. A index line is a
previous element line inside current bracket scope."
(let ((reference-line))
@@ -568,6 +622,42 @@
(cond
+
+ ((and
+ (or
+ (string-match-p
+ "^[\t ]*\\(<[^>]+>\\)+[\t ]*$"
+ previous-line-string)
+ (string-match-p
+ "^[\t ]*\\(<[^>]+>\\)+[\t ]*$"
+ current-line-string))
+ (not
+ (string-match-p
+ "<\\?"
+ previous-line-string))
+ (not
+ (string-match-p
+ "<\\?"
+ current-line-string)))
+ (setq
+ match-type
+ 'line-after-html-line)
+ (when-let ((html-bracket-level
+ (phps-mode-indent--get-html-string-bracket-level
+ previous-line-string)))
+ (when (> html-bracket-level 0)
+ (setq
+ new-indentation
+ (+ new-indentation tab-width))))
+
+ (when-let ((html-bracket-level
+ (phps-mode-indent--get-html-string-bracket-level
+ current-line-string)))
+ (when (< html-bracket-level 0)
+ (setq
+ new-indentation
+ (- new-indentation tab-width)))))
+
;; LINE AFTER EXTENDS / IMPLEMENTS
;; class MyClass implements
;; myInterface
diff --git a/test/phps-mode-test-indent.el b/test/phps-mode-test-indent.el
index 1a6b7202f1..8e9e7f124c 100644
--- a/test/phps-mode-test-indent.el
+++ b/test/phps-mode-test-indent.el
@@ -213,6 +213,51 @@
(phps-mode-indent--get-previous-start-of-bracket-line t)
nil)))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "<!DOCTYPE html>")
+ nil))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "<html>")
+ nil))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "</html>")
+ nil))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "<body>")
+ 1))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "</body>")
+ -1))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "<meta>")
+ nil))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "<meta />")
+ nil))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "<button>")
+ 1))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "<button />")
+ nil))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "</button>")
+ -1))
+ (should
+ (equal
+ (phps-mode-indent--get-html-string-bracket-level "</p>")
+ -1))
+
(message "Passed tests for indentation helper functions"))
(defun phps-mode-test-indent--get-lines-indent ()
- [elpa] externals/phps-mode 0ca0e447b8 167/212: Improved indentation on lines starting with a closing bracket, (continued)
- [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, 2022/01/26
- [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 <=
- [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
- [elpa] externals/phps-mode 5273bdf42f 206/212: Started on cache feature, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode d19bd439cf 198/212: Updated TODO items, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 502a42b5c0 205/212: Improved indentation around doc-comments, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 198cfbd360 199/212: Added failing indentation tests, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode af263a5462 200/212: Improved indentation detection of start of alternative control structure, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode 5ae4886f14 212/212: Updated docs and version, Christian Johansson, 2022/01/26
- [elpa] externals/phps-mode fe83e986c7 192/212: Fixed issue with optimized lexer after byte-compilation, Christian Johansson, 2022/01/26