[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phps-mode e8658c8 192/405: New indentation algorithm no
From: |
Stefan Monnier |
Subject: |
[elpa] externals/phps-mode e8658c8 192/405: New indentation algorithm now passes class unit tests, started on indentation document |
Date: |
Sat, 13 Jul 2019 10:00:11 -0400 (EDT) |
branch: externals/phps-mode
commit e8658c89d9a3d9cc57e2d14448a4516ffdef9fe6
Author: Christian Johansson <address@hidden>
Commit: Christian Johansson <address@hidden>
New indentation algorithm now passes class unit tests, started on
indentation document
---
docs/indentation-algorithm.md | 57 +++++++++++++++++++++++++++++++++++++++++++
phps-mode-functions.el | 45 +++++++++++++++++++---------------
2 files changed, 83 insertions(+), 19 deletions(-)
diff --git a/docs/indentation-algorithm.md b/docs/indentation-algorithm.md
new file mode 100644
index 0000000..1c59e8e
--- /dev/null
+++ b/docs/indentation-algorithm.md
@@ -0,0 +1,57 @@
+# Indentation algorithm for PHP
+
+Document describing indentation algorithm.
+
+## Terminology
+
+* `nesting`: sum of all kinds of open brackets
+* `nesting-stack`: contains nesting-levels before indentation was increased
+* `nesting-start`: nesting-level at the beginning of the last line
+* `nesting-end`: nesting-level at the end of the last line
+* `nesting-stack-start`: nesting-start at top of stack
+* `nesting-stack-end`: nesting-end at top of stack
+
+## Algorithm
+
+Here follows pseudo-code for a algorithm that calculates indentation for each
line in buffer.
+
+```php
+foreach token in buffer:
+
+ calculate nesting-end;
+
+ if nesting-stack AND nesting-end <= nesting-stack-end:
+ pop stack;
+ indentation--;
+ endif;
+
+ if we reached end of a line:
+ if nesting-stack AND nesting-end <= nesting-stack-start:
+ pop stack;
+ indent--;
+ endif;
+
+ save line indent;
+
+ if nesting-end > 0 AND (!nesting-stack OR nesting-end >
nesting-stack-end):
+ if !nesting-stack:
+ nesting-stack-end = 0;
+ endif;
+
+ push (nesting-stack-end nesting-end) to stack;
+ indent++;
+ endif;
+ endif;
+
+endforeach;
+```
+
+## Examples
+
+```php
+If (function( <- (1)
+ false)
+) { <- (3, 1)
+ echo true;
+} <- (3)
+```
diff --git a/phps-mode-functions.el b/phps-mode-functions.el
index ce834d0..77cd287 100644
--- a/phps-mode-functions.el
+++ b/phps-mode-functions.el
@@ -136,6 +136,31 @@
(when first-token-on-line
(setq first-token-is-nesting-decrease t)))
+ ;; Keep track of when we are inside a class definition
+ (if in-class-declaration
+ (if (string= token "{")
+ (progn
+ (setq in-class-declaration nil)
+ (setq in-class-declaration-level 0)
+
+ (setq column-level (1- column-level))
+ (setq nesting-start (1- nesting-start))
+ (pop nesting-stack)
+
+ (when first-token-on-line
+ (setq after-class-declaration t)
+ (setq first-token-is-nesting-increase nil)
+ (setq first-token-is-nesting-decrease t))
+
+ (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))
+
+ )
+ (when first-token-on-line
+ (setq in-class-declaration-level 1)))
+ (when (equal token 'T_CLASS)
+ (setq in-class-declaration t)
+ (setq in-class-declaration-level 1)))
+
;; Keep track of curly bracket level
(when (or (equal token 'T_CURLY_OPEN)
(equal token 'T_DOLLAR_OPEN_CURLY_BRACES)
@@ -293,25 +318,7 @@
(when (equal token 'T_END_HEREDOC)
(setq in-heredoc nil))
- ;; Keep track of when we are inside a class definition
- (if in-class-declaration
- (if (string= token "{")
- (progn
- (setq in-class-declaration nil)
- (setq in-class-declaration-level 0)
- (when first-token-on-line
- (setq after-class-declaration t)
- (setq first-token-is-nesting-increase nil)
- (setq first-token-is-nesting-decrease t))
-
- (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))
-
- )
- (when first-token-on-line
- (setq in-class-declaration-level 1)))
- (when (equal token 'T_CLASS)
- (setq in-class-declaration t)
- (setq in-class-declaration-level 1))))
+ )
(when token
- [elpa] externals/phps-mode 253839f 170/405: Added indentation unit tests for concatenated strings outside assignments, (continued)
- [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, 2019/07/13
- [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 <=
- [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
- [elpa] externals/phps-mode 91715f6 217/405: Added another test for multi-line function arguments, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 75c4af4 214/405: Fixed indentation for lines wrapped in scripting open/close, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode d754163 221/405: More work on multi-line token-less lines, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 8947e6e 202/405: Conceptual work on indentation-calculation for multi-line assignment, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode fc2e8b7 212/405: Added logic to indent token-less lines as well, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 8707777 237/405: Updated README, Stefan Monnier, 2019/07/13
- [elpa] externals/phps-mode 2d4646f 203/405: Work on inline control structure indentation, Stefan Monnier, 2019/07/13