branch: externals/phps-mode commit e8658c89d9a3d9cc57e2d14448a4516ffdef9fe6 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
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