branch: externals/phps-mode commit 761b56e418e994a55a3da354b1f95f719e92cbfb Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Added algorithm for token-blind indentation, implementing PHP CG function --- admin/phps-mode-automation.el | 3 +- phps-mode-analyzer.el | 93 ++++++++++++++++++++++++++++++++++++------- phps-mode-wy-macros.el | 11 +++-- phps-mode-wy-wy.el | 2 +- phps-mode.el | 6 ++- 5 files changed, 93 insertions(+), 22 deletions(-) diff --git a/admin/phps-mode-automation.el b/admin/phps-mode-automation.el index 4820ea9..c84b658 100644 --- a/admin/phps-mode-automation.el +++ b/admin/phps-mode-automation.el @@ -41,7 +41,8 @@ ;; Generate grammar (message "Generating Wisent grammar..") (if (fboundp 'emacs-wisent-grammar-converter-generate-grammar-from-filename) - (emacs-wisent-grammar-converter-generate-grammar-from-filename php-yacc-file wisent-destination header) + (emacs-wisent-grammar-converter-generate-grammar-from-filename + php-yacc-file wisent-destination header) (display-warning 'warning "Missing emacs-wisent-grammar-converter!")) (message "Automation completed")) diff --git a/phps-mode-analyzer.el b/phps-mode-analyzer.el index f9d7d36..71f785e 100644 --- a/phps-mode-analyzer.el +++ b/phps-mode-analyzer.el @@ -34,6 +34,7 @@ (require 'phps-mode-macros) +(require 'phps-mode-wy-macros) (require 'semantic) (require 'semantic/lex) @@ -88,11 +89,9 @@ (defvar phps-mode-lexer-long-limit 2147483648 "Limit for 32-bit integer.") -(defvar phps-mode-lexer-PARSER_MODE t - "Flag whether we is using parser-mode or not.") -(defvar phps-mode-lexer-SHORT_TAGS t - "Flag whether we support short-tags or not.") +(phps-mode-wy-macros-CG 'PARSER_MODE t) +(phps-mode-wy-macros-CG 'SHORT_TAGS t) ;; FLAGS/SIGNALS @@ -380,7 +379,7 @@ (defun phps-mode-lexer-RETURN_OR_SKIP_TOKEN (token start end) "Return TOKEN with START and END but only in parse-mode." - (when phps-mode-lexer-PARSER_MODE + (when (phps-mode-wy-macros-CG 'PARSER_MODE) (phps-mode-analyzer-emit-token token start end))) (defun phps-mode-lexer-RETURN_TOKEN (token start end) @@ -712,7 +711,7 @@ (let* ((start (match-beginning 0)) (end (match-end 0)) (data (buffer-substring-no-properties start end))) - (if phps-mode-lexer-PARSER_MODE + (if (phps-mode-wy-macros-CG 'PARSER_MODE) (phps-mode-lexer-MOVE_FORWARD end) (phps-mode-lexer-RETURN_TOKEN data start end))))) @@ -807,7 +806,7 @@ phps-mode-lexer-TABS_AND_SPACES ")"))) (lambda() - (when phps-mode-lexer-PARSER_MODE + (when (phps-mode-wy-macros-CG 'PARSER_MODE) (display-warning 'phps-mode "PHPs Lexer Error - The (real) cast is deprecated, use (float) instead")) (phps-mode-lexer-RETURN_TOKEN 'T_DOUBLE_CAST (match-beginning 0) (match-end 0)))) @@ -1301,7 +1300,7 @@ (let ((start (match-beginning 0)) (end (match-end 0))) (phps-mode-lexer-BEGIN 'ST_IN_SCRIPTING) - (when phps-mode-lexer-PARSER_MODE + (when (phps-mode-wy-macros-CG 'PARSER_MODE) (phps-mode-lexer-RETURN_TOKEN 'T_ECHO start end)) (phps-mode-lexer-RETURN_TOKEN 'T_OPEN_TAG_WITH_ECHO start end)))) @@ -1332,7 +1331,7 @@ start end)) - (phps-mode-lexer-SHORT_TAGS + ((phps-mode-wy-macros-CG 'SHORT_TAGS) (phps-mode-lexer-yyless 3) (setq end (- end 3)) (phps-mode-lexer-BEGIN 'ST_IN_SCRIPTING) @@ -1347,7 +1346,7 @@ (phps-mode-lexer-re2c-rule (and ST_INITIAL (looking-at "<\\?")) (lambda() - (when phps-mode-lexer-SHORT_TAGS + (when (phps-mode-wy-macros-CG 'SHORT_TAGS) (let ((start (match-beginning 0)) (end (match-end 0))) (phps-mode-lexer-BEGIN 'ST_IN_SCRIPTING) @@ -1470,7 +1469,7 @@ (when (= (- end start) 3) (setq end (1- end))) (phps-mode-lexer-BEGIN 'ST_INITIAL) - (when phps-mode-lexer-PARSER_MODE + (when (phps-mode-wy-macros-CG 'PARSER_MODE) (phps-mode-lexer-RETURN_TOKEN ";" start end)) (phps-mode-lexer-RETURN_TOKEN 'T_CLOSE_TAG start end)))) @@ -3249,11 +3248,77 @@ SQUARE-BRACKET-LEVEL and ROUND-BRACKET-LEVEL." (setq-local phps-mode-lexer-buffer-length (1- (point-max))) (setq-local phps-mode-lexer-buffer-contents - (buffer-substring-no-properties (point-min) (point-max))))))))))) + (buffer-substring-no-properties (point-min) (point-max)))))))) + (phps-mode-analyzer--alternative-indentation (point)) + (phps-mode-debug-message + (message "Did not find indent for line, using alternative indentation.."))))) + (phps-mode-analyzer--alternative-indentation (point)) (phps-mode-debug-message - (message "Did not find lines indent index, skipping indenting.."))) + (message "Did not find lines indent index, using alternative indentation.."))) + (phps-mode-analyzer--alternative-indentation (point)) (phps-mode-debug-message - (message "Skipping indentation since buffer is not processed yet")))) + (message "Using alternative indentation since buffer is not processed yet")))) + +(defun phps-mode-analyzer--alternative-indentation (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 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)) (defun phps-mode-functions--cancel-idle-timer () "Cancel idle timer." diff --git a/phps-mode-wy-macros.el b/phps-mode-wy-macros.el index c583404..980145d 100644 --- a/phps-mode-wy-macros.el +++ b/phps-mode-wy-macros.el @@ -26,12 +26,15 @@ ;;; Code: -(require 'semantic/wisent/grammar) +(defvar phps-mode-wy-macros--CG + (make-hash-table :test 'equal) + "A hash-table with all settings.") -(defun CG (_subject &optional _value) +(defun phps-mode-wy-macros-CG (subject &optional value) "Return and optionally set VALUE of SUBJECT." - ;; TODO Implement this - ) + (if value + (puthash subject value phps-mode-wy-macros--CG) + (gethash subject phps-mode-wy-macros--CG))) (defun zend_add_class_modifier (_class _modifier) "Add CLASS MODIFIER." diff --git a/phps-mode-wy-wy.el b/phps-mode-wy-wy.el index 3ad834f..a00ec72 100644 --- a/phps-mode-wy-wy.el +++ b/phps-mode-wy-wy.el @@ -3,7 +3,7 @@ ;; Copyright (C) 2018-2019 Free Software Foundation, Inc. ;; Author: Christian Johansson <christianjohansson@Christians-MacBook-Air.local> -;; Created: 2019-10-04 07:20:36+0200 +;; Created: 2019-11-13 07:07:16+0100 ;; Keywords: syntax ;; X-RCS: $Id$ diff --git a/phps-mode.el b/phps-mode.el index 65b5fec..a4221b8 100644 --- a/phps-mode.el +++ b/phps-mode.el @@ -5,8 +5,8 @@ ;; Author: Christian Johansson <christ...@cvj.se> ;; Maintainer: Christian Johansson <christ...@cvj.se> ;; Created: 3 Mar 2018 -;; Modified: 12 Nov 2019 -;; Version: 0.3.13 +;; Modified: 18 Nov 2019 +;; Version: 0.3.15 ;; Keywords: tools, convenience ;; URL: https://github.com/cjohansson/emacs-phps-mode @@ -94,6 +94,8 @@ (progn (when phps-mode-use-psr-2 (untabify (point-min) (point-max))) + (phps-mode-analyzer-process-changes) + (phps-mode-functions-process-current-buffer) (indent-region (point-min) (point-max))) (let ((old-buffer-contents (buffer-substring-no-properties (point-min) (point-max)))