[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode f2f41a3 088/405: Handles indentation case wit
From: |
Stefan Monnier |
Subject: |
[elpa] externals/phps-mode f2f41a3 088/405: Handles indentation case with } else if (...) { |
Date: |
Sat, 13 Jul 2019 09:59:49 -0400 (EDT) |
branch: externals/phps-mode
commit f2f41a3ee195fe831eaaacc10683fd74f91e6662
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
Handles indentation case with } else if (...) {
---
phps-functions.el | 128 ++++++++++++++++++++++++++++---------------------
phps-test-functions.el | 8 ++++
2 files changed, 81 insertions(+), 55 deletions(-)
diff --git a/phps-functions.el b/phps-functions.el
index e915557..d4e1cec 100644
--- a/phps-functions.el
+++ b/phps-functions.el
@@ -42,8 +42,9 @@
(defvar phps-mode/buffer-changes--start nil
"Start of buffer changes, nil if none.")
-;; TODO Also format white-space inside the line, i.e. after function
declarations?
+;; NOTE Also format white-space inside the line, i.e. after function
declarations?
;; TODO Support inline function indentations
+;; TODO Support indentation for multi-line scalar assignments
(defun phps-mode/indent-line ()
"Indent line."
(let ((data (phps-mode/get-point-data))
@@ -70,15 +71,16 @@
;; When bracket count at start is larger than bracket count at end
(when (and
(boundp 'phps-mode/lexer-tokens)
- (or (not (equal start-bracket-level end-bracket-level))
- (not (equal start-parenthesis-level
end-parenthesis-level)))
start-token-number
end-token-number)
(let ((token-number start-token-number)
(valid-tokens t)
- (last-token-is-open-brace nil)
- (tokens phps-mode/lexer-tokens))
+ (last-token-is-opening-brace nil)
+ (first-token-is-closing-brace nil)
+ (tokens phps-mode/lexer-tokens)
+ (is-first-line-token t))
;; (message "token start %s, token end %s" start-token-number
end-token-number)
+ ;; (message "First token %s, last token %s" (car (nth
start-token-number tokens)) (car (nth end-token-number tokens)))
;; Interate tokens in line and check if all are valid
(while (and valid-tokens
@@ -87,37 +89,58 @@
(token-start (car (cdr (nth token-number tokens))))
(token-end (cdr (cdr (nth token-number tokens)))))
- ;; Check if last token is open brace
- (when (and (= token-number end-token-number)
- (string= token "{"))
- (setq last-token-is-open-brace t))
-
;; Check if current token is not one of the valid tokens
- (when (and valid-tokens
- (or (>= token-start line-start)
- (>= token-end line-start))
- (not (or
- (string= token "{")
- (string= token "}")
- (string= token "(")
- (string= token ")")
- (string= token "[")
- (string= token "]")
- (string= token ";")
- (eq token 'T_CLOSE_TAG))))
- ;; (message "Token %s - %s in %s was invalid, line start
%s" token token-number tokens line-start)
- (setq valid-tokens nil)))
-
- (setq token-number (+ token-number 1)))
-
- (when valid-tokens
- ;; (message "Tokens was valid, decreasing indent %s - %s"
(line-beginning-position) (line-end-position))
-
- ;; If last token is a opening brace indent line one lesser
column
- (when last-token-is-open-brace
- (setq indent-end (- indent-end 1)))
-
- (setq indent-level (- indent-level (- indent-start
indent-end))))))
+ (when (or (>= token-start line-start)
+ (>= token-end line-start))
+
+ ;; Is it the last token and is it a opening brace?
+ (when (and (= token-number end-token-number)
+ (string= token "{"))
+ (setq last-token-is-opening-brace t))
+
+ ;; Is it the first line token?
+ (when is-first-line-token
+ (setq is-first-line-token nil)
+
+ ;; Is it a closing brace?
+ (when (string= token "}")
+ (setq first-token-is-closing-brace t)))
+
+ (when (and valid-tokens
+ (not (or
+ (string= token "{")
+ (string= token "}")
+ (string= token "(")
+ (string= token ")")
+ (string= token "[")
+ (string= token "]")
+ (string= token ";")
+ (eq token 'T_CLOSE_TAG))))
+ ;; (message "Token %s - %s in %s was invalid, line
start %s" token token-number tokens line-start)
+ (setq valid-tokens nil))
+
+ )
+
+ (setq token-number (+ token-number 1))))
+
+ (if valid-tokens
+ (progn
+
+ ;; If last token is a opening brace indent line one
lesser column
+ (when last-token-is-opening-brace
+ ;; (message "Last token was opening brace")
+ (setq indent-level (- indent-level 1)))
+
+ ;; (message "Tokens was valid, decreasing indent %s -
%s" (line-beginning-position) (line-end-position))
+ (setq indent-level (- indent-level (- indent-start
indent-end))))
+
+
+ ;; If first token is a closing brace indent line one lesser
column
+ (when first-token-is-closing-brace
+ ;; (message "First token was closing brace")
+ (setq indent-level (- indent-level 1))))
+
+ ))
;; If this line is part of a doc-comment increase indent with one
unit
(when in-doc-comment
@@ -137,25 +160,20 @@
(indent-line-to indent-sum)
- ;; When indent is changed the trailing tokens and states
just need to adjust their positions, this will improve speed of indent-region a
lot
- (phps-mode/move-lexer-tokens line-start indent-diff)
- (phps-mode/move-lexer-states line-start indent-diff)
- ;; (message "Moving tokens and states %s, %s to %s"
indent-diff current-indentation indent-sum)
-
- ;; ;; Set point of change if it's not set or if it's
larger than current point
- ;; (when (or (not phps-mode/buffer-changes--start)
- ;; (< line-start
phps-mode/buffer-changes--start))
- ;; ;; (message "Setting changes start from %s to %s"
phps-mode/buffer-changes--start start)
- ;; (setq phps-mode/buffer-changes--start line-start))
-
- ;; (phps-mode/run-incremental-lex)
-
- )))))))))
-
-;; TODO Implement this?
-(defun phps-mode/indent-region ()
- "Indent region."
- )
+ ;; When indent is changed the trailing tokens and states
just need to adjust their positions, this will improve speed of indent-region a
lot
+ (phps-mode/move-lexer-tokens line-start indent-diff)
+ (phps-mode/move-lexer-states line-start indent-diff)
+ ;; (message "Moving tokens and states %s, %s to %s"
indent-diff current-indentation indent-sum)
+
+ ;; ;; Set point of change if it's not set or if it's larger
than current point
+ ;; (when (or (not phps-mode/buffer-changes--start)
+ ;; (< line-start phps-mode/buffer-changes--start))
+ ;; ;; (message "Setting changes start from %s to %s"
phps-mode/buffer-changes--start start)
+ ;; (setq phps-mode/buffer-changes--start line-start))
+
+ ;; (phps-mode/run-incremental-lex)
+
+ )))))))))
;; TODO Fix flycheck error here
(defun phps-mode/after-change-functions (start stop length)
@@ -279,7 +297,7 @@
(add-hook 'after-change-functions #'phps-mode/after-change-functions)
- ;; (set (make-local-variable 'indent-line-function)
#'phps-mode/indent-region)
+ ;; NOTE Implement indent-region?
)
diff --git a/phps-test-functions.el b/phps-test-functions.el
index cd7711a..8eecbb8 100644
--- a/phps-test-functions.el
+++ b/phps-test-functions.el
@@ -153,6 +153,14 @@
(let ((buffer-contents (buffer-substring-no-properties (point-min)
(point-max))))
(should (equal buffer-contents "<?php\nif (myFirstCondition()) {\n
$this->var = 'abc123';\n} else {\n $this->var = 'def456';\n}\n"))))
+ (phps-mode/with-test-buffer
+ "<?php\nif (myFirstCondition()) {\n $this->var = 'abc123';\n } else
if (mySeconCondition()) {\n $this->var = 'def456';\n}\n"
+ (goto-char 68)
+ (phps-mode/indent-line)
+ ;; (message "Tokens %s point %s" phps-mode/lexer-tokens (point))
+ (let ((buffer-contents (buffer-substring-no-properties (point-min)
(point-max))))
+ (should (equal buffer-contents "<?php\nif (myFirstCondition()) {\n
$this->var = 'abc123';\n} else if (mySeconCondition()) {\n $this->var =
'def456';\n}\n"))))
+
)
- [elpa] externals/phps-mode 287d934 077/405: Updated comments are made un-tested function for moving token positions, (continued)
- [elpa] externals/phps-mode 287d934 077/405: Updated comments are made un-tested function for moving token positions, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode b3821a1 080/405: Updated TODOs, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f4cd90e 072/405: Updated readme and makefile for running all tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f79cf4f 081/405: Started on function for moving lexer states, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e340279 082/405: Added unit tests for moving lexer-state positions, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 89b12ab 084/405: Added autoloads and fixed bug with assigning variable, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode a10cc79 085/405: Debugging a indentation case, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e30ac0c 083/405: Started implementation of moving lexer and state position when, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 8c919ac 087/405: Added failing indentation test, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 69ced0d 090/405: Added another unit test for failed indentation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f2f41a3 088/405: Handles indentation case with } else if (...) {,
Stefan Monnier <=
- [elpa] externals/phps-mode 59ba835 086/405: Fixed issue with wrong indentation at trailing opening brace, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 434c3dc 089/405: Added more failing tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 61abf7e 091/405: Added todo item, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode f7f931a 092/405: Added another failing indentation test, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode a8561ab 096/405: Updated unit test for get-point-data with support for square brackets, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode dbc02bb 099/405: Improved syntax table, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 989cc7e 098/405: Fixed flycheck warning about semantic, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode c5beb50 093/405: Added another unit test for indentation with special syntax, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 3740660 095/405: Get point-data now collects information about square brackets, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 6c8eea0 100/405: Added another failing indentation case for switch case, Stefan Monnier, 2019/07/13