[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode 2a1ed52 196/405: Optimized algorithm for inde
From: |
Stefan Monnier |
Subject: |
[elpa] externals/phps-mode 2a1ed52 196/405: Optimized algorithm for indentation |
Date: |
Sat, 13 Jul 2019 10:00:12 -0400 (EDT) |
branch: externals/phps-mode
commit 2a1ed52f4ce86ca3d1059c559fa50632a49ca375
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
Optimized algorithm for indentation
---
docs/indentation-algorithm.md | 44 +++++++++++++++++++++---------
phps-mode-functions.el | 63 +++++++++++++++++--------------------------
2 files changed, 56 insertions(+), 51 deletions(-)
diff --git a/docs/indentation-algorithm.md b/docs/indentation-algorithm.md
index 1c59e8e..89147ea 100644
--- a/docs/indentation-algorithm.md
+++ b/docs/indentation-algorithm.md
@@ -20,20 +20,16 @@ foreach token in buffer:
calculate nesting-end;
- if nesting-stack AND nesting-end <= nesting-stack-end:
+ if nesting-stack AND nesting-end <= nesting-stack-start: // #decrease
pop stack;
- indentation--;
+ indent--;
endif;
if we reached end of a line:
- if nesting-stack AND nesting-end <= nesting-stack-start:
- pop stack;
- indent--;
- endif;
- save line indent;
+ save line indent; // #save
- if nesting-end > 0 AND (!nesting-stack OR nesting-end >
nesting-stack-end):
+ if nesting-end > 0 AND (!nesting-stack OR nesting-end >
nesting-stack-end): // #increase
if !nesting-stack:
nesting-stack-end = 0;
endif;
@@ -49,9 +45,33 @@ endforeach;
## Examples
```php
-If (function( <- (1)
- false)
-) { <- (3, 1)
+if (function( // #save indent: 0, #increase push (0 2) indent: 1
+ false) // #save indent: 1
+) { // #decrease pop (0 2) indent: 0, #save indent:
0, #increase push (0 1) indent: 1
+ echo true; // #save indent: 1
+} // #decrease pop (0 1) indent: 0, #save
indent: 0
+```
+
+## Inline control structure for if-else
+
+```php
+if (true)
+ echo true;
+else
+ echo false;
+```
+
+## Alternative control structure for if-else 2
+
+```php
+if (true &&
+ true
+):
echo true;
-} <- (3)
+elseif (true
+ || false):
+ echo 'another';
+else:
+ echo false;
+endif;
```
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index ddf0270..746608f 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -280,7 +280,6 @@
(when (and in-inline-control-structure
(string= token ";")
(not special-control-structure-started-this-line))
- (setq column-level (1- column-level))
(setq line-contained-nesting-decrease t)
(setq inline-control-structure-level (1-
inline-control-structure-level))
(setq in-inline-control-structure nil))
@@ -345,6 +344,29 @@
)
(when token
+
+ ;; Calculate nesting
+ (setq nesting-end (+ round-bracket-level square-bracket-level
curly-bracket-level alternative-control-structure-level
inline-control-structure-level in-assignment-level in-class-declaration-level))
+
+ ;; Has nesting increased?
+ (when (and nesting-stack
+ (<= nesting-end (car (car nesting-stack))))
+
+ (when phps-mode-functions-verbose
+ ;; (message "\nPopping %s from nesting-stack since %s is
lesser or equal to %s, next value is: %s\n" (car nesting-stack) nesting-end
(car (car nesting-stack)) (nth 1 nesting-stack))
+ )
+ (pop nesting-stack)
+
+ ;; Decrement column
+ (if allow-custom-column-decrement
+ (progn
+ (setq column-level (- column-level (- nesting-start
nesting-end)))
+ (setq allow-custom-column-increment nil))
+ (setq column-level (1- column-level)))
+
+ ;; Prevent negative column-values
+ (when (< column-level 0)
+ (setq column-level 0)))
;; Are we on a new line or is it the last token of the buffer?
(if (> next-token-start-line-number token-start-line-number)
@@ -352,30 +374,6 @@
;; Line logic
(progn
- ;; Calculate nesting
- (setq nesting-end (+ round-bracket-level
square-bracket-level curly-bracket-level alternative-control-structure-level
inline-control-structure-level in-assignment-level in-class-declaration-level))
-
- ;; Has nesting increased?
- (when (and nesting-stack
- (<= nesting-end (car (car nesting-stack))))
-
- (when phps-mode-functions-verbose
- ;; (message "\nPopping %s from nesting-stack since
%s is lesser or equal to %s, next value is: %s\n" (car nesting-stack)
nesting-end (car (car nesting-stack)) (nth 1 nesting-stack))
- )
- (pop nesting-stack)
-
- ;; Decrement column
- (if allow-custom-column-decrement
- (progn
- (setq column-level (- column-level (-
nesting-start nesting-end)))
- (setq allow-custom-column-increment nil))
- (setq column-level (1- column-level)))
-
- ;; Prevent negative column-values
- (when (< column-level 0)
- (setq column-level 0)))
-
-
;; ;; Start indentation might differ from ending
indentation in cases like } else {
(setq column-level-start column-level)
@@ -446,7 +444,7 @@
(setq column-level (1+ column-level)))
(when phps-mode-functions-verbose
- ;; (message "\nPushing (%s %s) to nesting-stack
since %s is greater than %s or stack is empty" nesting-start nesting-end
nesting-end (car (cdr (car nesting-stack))))
+ (message "\nPushing (%s %s %s) to nesting-stack
since %s is greater than %s or stack is empty" nesting-start nesting-end token
nesting-end (car (cdr (car nesting-stack))))
)
(push `(,nesting-stack-end ,nesting-end ,token)
nesting-stack)
(when phps-mode-functions-verbose
@@ -483,19 +481,6 @@
;; Current token is not first
(setq first-token-on-line nil)
- ;; Calculate nesting
- (setq nesting-end (+ round-bracket-level
square-bracket-level curly-bracket-level alternative-control-structure-level
inline-control-structure-level in-assignment-level in-class-declaration-level))
-
- ;; Is current nesting-level equal or below stack-value? (#0)
- (when (and nesting-stack
- (<= nesting-end (car (car nesting-stack))))
- (setq column-level (1- column-level))
- (when phps-mode-functions-verbose
- ;; (message "\nPopping %s from nesting-stack since %s is
lesser somewhere on line, next is: %s, new column is: %s, new stack-value is:
%s\n" (car nesting-stack) nesting-end (nth 1 nesting-stack) column-level (nth 1
nesting-stack))
- )
- (pop nesting-stack)
- (setq changed-nesting-stack-in-line t))
-
(when (> token-end-line-number token-start-line-number)
;; (message "Token not first on line %s starts at %s and
ends at %s" token token-start-line-number token-end-line-number)
(when (equal token 'T_DOC_COMMENT)
- [elpa] externals/phps-mode c71a97d 178/405: Started on refactored indentation calculation, (continued)
- [elpa] externals/phps-mode c71a97d 178/405: Started on refactored indentation calculation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 8bb3918 249/405: Clean-up of unit test logging, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 9201d1c 246/405: Imenu index now handles functions with optional arguments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 2c3239a 187/405: Adding new failing tests for indentation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode d9a0119 183/405: Optimized indentation code by removing unused variables, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode a0cbcd1 186/405: More work on indentation of multi-line IF-expressions, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode d7c5f67 190/405: More work on nesting-stack, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 253839f 170/405: Added indentation unit tests for concatenated strings outside assignments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e38794b0 197/405: More conceptual work on algorithm for inline control structures, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 9db28d9 185/405: Added verbose flag for unit tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 2a1ed52 196/405: Optimized algorithm for indentation,
Stefan Monnier <=
- [elpa] externals/phps-mode a30c3f6 177/405: More work on indentation tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e8a8d41 204/405: Merged remote changes, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode faa4e00 201/405: Started on conceptually describing algorithm for multi-line assignment indentation, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 874701d 195/405: Work on alternative and inline control structure syntax with new algorithm, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode ec69d07 193/405: Added a failing class declaration indentation case, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 164b77e 205/405: Unit test for alternative control syntax passes with new algorithm, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode e8658c8 192/405: New indentation algorithm now passes class unit tests, started on indentation document, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode ba9d8a0 199/405: More organization of tests, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 86404c8 207/405: New algorithm now passes tests for alternative control structure, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 8b4c188 188/405: Now able to detect indent change of scope with same nesting level, Stefan Monnier, 2019/07/13