[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode f0c18c8: Improved token-blind indentation
From: |
Christian Johansson |
Subject: |
[elpa] externals/phps-mode f0c18c8: Improved token-blind indentation |
Date: |
Thu, 21 Nov 2019 01:16:34 -0500 (EST) |
branch: externals/phps-mode
commit f0c18c8e2afac126013ec7e896644dcd31800b96
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
Improved token-blind indentation
---
phps-mode-analyzer.el | 167 ++++++++++++++++++++++---------------
phps-mode.el | 17 +---
test/phps-mode-test-functions.el | 71 +++++++++++++++-
test/phps-mode-test-integration.el | 1 -
4 files changed, 172 insertions(+), 84 deletions(-)
diff --git a/phps-mode-analyzer.el b/phps-mode-analyzer.el
index 71f785e..e901edb 100644
--- a/phps-mode-analyzer.el
+++ b/phps-mode-analyzer.el
@@ -1828,7 +1828,8 @@
(with-current-buffer buffer
(let ((run-full-lexer nil)
(old-tokens phps-mode-lexer-tokens)
- (old-states phps-mode-lexer-states))
+ (old-states phps-mode-lexer-states)
+ (log '()))
(if phps-mode-analyzer-change-min
(progn
@@ -1924,25 +1925,29 @@
(setq-local phps-mode-lexer-tokens (append
head-tokens incremental-tokens))
- (phps-mode-debug-message
- (message "Incremental tokens: %s"
incremental-tokens))
+ (push (list 'INCREMENTAL-LEX
incremental-start-new-buffer) log)
- )
+ (phps-mode-debug-message
+ (message "Incremental tokens: %s"
incremental-tokens)))
+ (push (list 'FOUND-NO-HEAD-STATES
incremental-start-new-buffer) log)
(phps-mode-debug-message
(message "Found no head states"))
(setq run-full-lexer t)))
+ (push (list 'FOUND-NO-HEAD-TOKENS
incremental-start-new-buffer) log)
(phps-mode-debug-message
(message "Found no head tokens"))
(setq run-full-lexer t))))
+ (push (list 'FOUND-NO-CHANGE-POINT-MINIMUM) log)
(phps-mode-debug-message
(message "Found no change point minimum"))
(setq run-full-lexer t))
- (if run-full-lexer
- (progn
- (phps-mode-debug-message
- (message "Running full lexer"))
- (phps-mode-lexer-run))))))
+ (when run-full-lexer
+ (push (list 'RUN-FULL-LEXER) log)
+ (phps-mode-debug-message
+ (message "Running full lexer"))
+ (phps-mode-lexer-run))
+ log)))
(defun phps-mode-functions-get-processed-buffer ()
"Get flag for whether buffer is processed or not."
@@ -3259,66 +3264,94 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL."
(phps-mode-debug-message
(message "Using alternative indentation since buffer is not processed
yet"))))
-(defun phps-mode-analyzer--alternative-indentation (point)
+(defun phps-mode-analyzer--alternative-indentation (&optional point)
"Apply alternative indentation at POINT here."
- (save-excursion
- (let ((line-number (line-number-at-pos point))
- (move-length 1)
- (line-is-empty t)
- line-beginning-position
- line-end-position
- line-string
- old-line-number)
- (goto-char point)
- (when (> line-number 1)
- (while (and
- (> (- line-number move-length) 0)
- line-is-empty)
- (forward-line (* move-length -1))
- (beginning-of-line)
- (setq line-beginning-position (line-beginning-position))
- (setq line-end-position (line-end-position))
- (setq
- line-string
- (buffer-substring-no-properties line-beginning-position
line-end-position)
- )
- (setq line-is-empty (string= line-string ""))
+ (unless point
+ (setq point (point)))
+ (let ((new-indentation))
+ (save-excursion
+ (let ((line-number (line-number-at-pos point))
+ (move-length 1)
+ (line-is-empty t)
+ line-beginning-position
+ line-end-position
+ line-string
+ current-line-string)
+ (goto-char point)
+ (setq
+ current-line-string
+ (buffer-substring-no-properties
+ (line-beginning-position)
+ (line-end-position)
)
-
- (unless line-is-empty
- (let* ((old-indentation (current-indentation))
- (new-indentation old-indentation)
- (bracket-level 0)
- (start 0)
- (end (- line-end-position line-beginning-position)))
- (while (and (< start end)
- (string-match
"\\([\]{}()[]\\|<[a-zA-Z]+\\|</[a-zA-Z]+\\|/>\\)" line-string start))
- (setq start (match-end 0))
- (let ((bracket (substring line-string (match-beginning 0)
(match-end 0))))
- (cond
- ((or
- (string= bracket "{")
- (string= bracket "[")
- (string= bracket "(")
- (string= bracket "<")
- (string-match "<[a-zA-Z]+" bracket))
- (setq bracket-level (1+ bracket-level)))
- (t
- (setq bracket-level (1- bracket-level))))))
-
- (forward-line move-length)
-
- (when (> bracket-level 0)
- (setq new-indentation (+ new-indentation tab-width)))
-
- (when (< bracket-level 0)
- (setq new-indentation (- new-indentation tab-width)))
-
- (when (< new-indentation 0)
- (setq new-indentation 0))
-
- (indent-line-to new-indentation))))))
- (end-of-line))
+ )
+ (when (> line-number 1)
+ (while (and
+ (> (- line-number move-length) 0)
+ line-is-empty)
+ (forward-line (* move-length -1))
+ (setq line-number (1- line-number))
+ (beginning-of-line)
+ (setq line-beginning-position (line-beginning-position))
+ (setq line-end-position (line-end-position))
+ (setq
+ line-string
+ (buffer-substring-no-properties line-beginning-position
line-end-position)
+ )
+ (setq line-is-empty (string-match-p "^[ \t\f\r\n]*$" line-string))
+ )
+
+ (unless line-is-empty
+ (let* ((old-indentation (current-indentation))
+ (new-bracket-level
(phps-mode-analyzer--get-string-brackets-count current-line-string))
+ (bracket-level
(phps-mode-analyzer--get-string-brackets-count line-string)))
+ (setq new-indentation old-indentation)
+
+ (forward-line move-length)
+
+ (when (> bracket-level 0)
+ (setq new-indentation (+ new-indentation tab-width)))
+
+ (when (< bracket-level 0)
+ (setq new-indentation (- new-indentation tab-width)))
+
+ (when (< new-bracket-level 0)
+ (setq new-indentation (- new-indentation tab-width)))
+
+ ;; Decrease indentation if current line decreases in bracket
level
+ (when (< new-indentation 0)
+ (setq new-indentation 0))
+
+ (indent-line-to new-indentation))))))
+ ;; Only move to end of line if point is the current point
+ (when (equal point (point))
+ (end-of-line))
+ new-indentation))
+
+(defun phps-mode-analyzer--get-string-brackets-count (string)
+ "Get bracket count for STRING."
+ (let ((bracket-level 0)
+ (start 0)
+ (line-is-empty
+ (string-match-p "^[ \t\f\r\n]*$" string)))
+ (unless line-is-empty
+ (while (string-match
+ "\\([\]{}()[]\\|<[a-zA-Z]+\\|</[a-zA-Z]+\\|/>\\)"
+ string
+ start)
+ (setq start (match-end 0))
+ (let ((bracket (substring string (match-beginning 0) (match-end 0))))
+ (cond
+ ((or
+ (string= bracket "{")
+ (string= bracket "[")
+ (string= bracket "(")
+ (string= bracket "<")
+ (string-match "<[a-zA-Z]+" bracket))
+ (setq bracket-level (1+ bracket-level)))
+ (t
+ (setq bracket-level (1- bracket-level)))))))
+ (* bracket-level tab-width)))
(defun phps-mode-functions--cancel-idle-timer ()
"Cancel idle timer."
diff --git a/phps-mode.el b/phps-mode.el
index a4221b8..1f6728c 100644
--- a/phps-mode.el
+++ b/phps-mode.el
@@ -5,8 +5,8 @@
;; Author: Christian Johansson <address@hidden>
;; Maintainer: Christian Johansson <address@hidden>
;; Created: 3 Mar 2018
-;; Modified: 18 Nov 2019
-;; Version: 0.3.15
+;; Modified: 21 Nov 2019
+;; Version: 0.3.16
;; Keywords: tools, convenience
;; URL: https://github.com/cjohansson/emacs-phps-mode
@@ -56,19 +56,6 @@
(defvar phps-mode-use-psr-2 t
"Whether to use PSR-2 guidelines for white-space or not.")
-(defvar phps-mode-runtime-debug nil
- "Whether or not to use runtime debugging.")
-
-(defun phps-mode-runtime-debug-message (message)
- "Output MESSAGE if flag is on."
- (when phps-mode-runtime-debug
- (let ((buffer (get-buffer-create "*PHPs Debug Messages*")))
- (with-current-buffer buffer
- (save-excursion
- (goto-char (point-max))
- (insert message)
- (insert "\n"))))))
-
(defvar phps-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-r") #'phps-mode-lexer-run)
diff --git a/test/phps-mode-test-functions.el b/test/phps-mode-test-functions.el
index 31ae263..c63a228 100644
--- a/test/phps-mode-test-functions.el
+++ b/test/phps-mode-test-functions.el
@@ -29,6 +29,73 @@
(require 'phps-mode)
(require 'phps-mode-test)
+(defun phps-mode-test-functions-process-changes ()
+ "Test `phps-mode-analyzer-process-changes'."
+
+ (phps-mode-test-with-buffer
+ "\n<html>\n<?php\n/**\n * Bla\n */"
+ "Process changes before current tokens"
+ (goto-char (point-min))
+ (insert "<?php echo 'test';\n?>")
+ (should (equal
+ (phps-mode-analyzer-process-changes)
+ '((RUN-FULL-LEXER) (FOUND-NO-HEAD-TOKENS 1)))))
+
+ (phps-mode-test-with-buffer
+ "\n<html>\n<?php\n/**\n * Bla\n */"
+ "Process changes without changes"
+ (should (equal
+ (phps-mode-analyzer-process-changes)
+ '((RUN-FULL-LEXER) (FOUND-NO-CHANGE-POINT-MINIMUM)))))
+
+ (phps-mode-test-with-buffer
+ "\n<html>\n<?php\n/**\n * Bla\n */"
+ "Process changes after existing tokens"
+ (goto-char (point-max))
+ (insert "\necho 'I was here';\n")
+ (should (equal
+ (phps-mode-analyzer-process-changes)
+ '((INCREMENTAL-LEX 15)))))
+
+ )
+
+(defun phps-mode-test-functions-alternative-indentation ()
+ "Test `phps-mode-analyzer--alternative-indentation'."
+
+ (phps-mode-test-with-buffer
+ "<?php\nif ($myCondition) {\necho 'I was here';\n}"
+ "Alternative indentation inside if block"
+ (goto-char 32)
+ (should (equal
+ (phps-mode-analyzer--alternative-indentation)
+ 4))
+ (goto-char 15)
+ (should (equal
+ (phps-mode-analyzer--alternative-indentation)
+ 0))
+ (goto-char (point-max))
+ (should (equal
+ (phps-mode-analyzer--alternative-indentation)
+ 0)))
+
+ (phps-mode-test-with-buffer
+ "<?php\nif ($myCondition) {\necho 'I was here';\necho 'I was here
again';\n}"
+ "Alternative indentation on closing if block"
+ (goto-char 30)
+ (should (equal
+ (phps-mode-analyzer--alternative-indentation)
+ 4))
+ (goto-char 57)
+ (should (equal
+ (phps-mode-analyzer--alternative-indentation)
+ 4))
+ (goto-char (point-max))
+ (should (equal
+ (phps-mode-analyzer--alternative-indentation)
+ 0)))
+
+ )
+
(defun phps-mode-test-functions-move-lines-indent ()
"Test `phps-mode-functions-move-lines-indent'."
@@ -1097,7 +1164,9 @@
(defun phps-mode-test-functions ()
"Run test for functions."
;; (setq debug-on-error t)
- (setq phps-mode-runtime-debug t)
+ (phps-mode-test-functions-process-changes)
+ (phps-mode-test-functions-alternative-indentation)
+ (phps-mode-test-functions-move-lines-indent)
(phps-mode-test-functions-get-inline-html-indentation)
(phps-mode-test-functions-get-lines-indent-if)
(phps-mode-test-functions-get-lines-indent-classes)
diff --git a/test/phps-mode-test-integration.el
b/test/phps-mode-test-integration.el
index fa2a1b0..62b7820 100644
--- a/test/phps-mode-test-integration.el
+++ b/test/phps-mode-test-integration.el
@@ -297,7 +297,6 @@
(defun phps-mode-test-integration ()
"Run test for integration."
(setq debug-on-error t)
- ;; (setq phps-mode-runtime-debug t)
;; (setq phps-mode-analyzer-process-on-indent-and-imenu t)
(phps-mode-test-integration-incremental-vs-initial-buffers)
;; (phps-mode-test-integration-whitespace-modifications)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [elpa] externals/phps-mode f0c18c8: Improved token-blind indentation,
Christian Johansson <=