branch: externals/phps-mode commit 86ec35fb8b7b9e301e702d13c792ba6c4bed107d Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Incremental lexer working somewhat --- phps-functions.el | 14 ++++++++++---- phps-lexer.el | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/phps-functions.el b/phps-functions.el index fe7ded9..6c4a67b 100644 --- a/phps-functions.el +++ b/phps-functions.el @@ -40,6 +40,7 @@ "Start of buffer changes, nil if none.") ;; TODO Also format white-space inside the line, i.e. after function declarations? +;; TODO Support inline function indentation (defun phps-mode/indent-line () "Indent line." (let ((data (phps-mode/get-point-data))) @@ -101,7 +102,12 @@ ;; (message "Indenting to %s" indent-sum) ;; (message "inside scripting, start: %s, end: %s, indenting to column %s " start end indent-level) (indent-line-to indent-sum) - (phps-mode/run-incremental-lex)))))))) + (let ((line-start (line-beginning-position))) + (when (or (not phps-mode/buffer-changes--start) + (< line-start phps-mode/buffer-changes--start)) + ;; (message "Setting changes start from %s to %s" phps-mode/buffer-changes--start start) + (setq phps-mode/buffer-changes--start line-start)) + (phps-mode/run-incremental-lex))))))))) ;; TODO Implement this? (defun phps-mode/indent-region () @@ -114,13 +120,13 @@ (when (string= major-mode "phps-mode") (when (and (not phps-mode/buffer-changes--start) (boundp 'phps-mode/idle-interval)) - (run-with-idle-timer phps-mode/idle-interval nil #'phps-mode/lex--RUN) + ;; (run-with-idle-timer phps-mode/idle-interval nil #'phps-mode/lex--RUN) ;; TODO Maybe use incremental lexer once it's working - ;; (run-with-idle-timer phps-mode/idle-interval nil #'phps-mode/run-incremental-lex) + (run-with-idle-timer phps-mode/idle-interval nil #'phps-mode/run-incremental-lex) ) (when (or (not phps-mode/buffer-changes--start) (< start phps-mode/buffer-changes--start)) - ;; (message "Setting %s to %s" phps-mode/buffer-changes--start start) + ;; (message "Setting start of changes from %s to %s" phps-mode/buffer-changes--start start) (setq phps-mode/buffer-changes--start start)) ;; (message "phps-mode/after-change-functions %s %s %s" start stop length) )) diff --git a/phps-lexer.el b/phps-lexer.el index dfca7c2..bd0a586 100644 --- a/phps-lexer.el +++ b/phps-lexer.el @@ -1262,9 +1262,9 @@ ANY_CHAR' ;; TODO Need to store lexer state and stack at each changing point of buffer to be able to rewind lexer (defun phps-mode/lex--SETUP (start end) "Just prepare other lexers for lexing region START to END." + (message "phps-mode/lex--SETUP %s %s" start end) (when (and (eq start 1) end) - ;; (message "SETUP %s %s" start end) (delete-all-overlays) (when (boundp 'phps-mode/buffer-changes--start) (setq phps-mode/buffer-changes--start nil)) @@ -1288,8 +1288,12 @@ ANY_CHAR' (new-states '()) (states (nreverse phps-mode/lexer-states)) (change-start phps-mode/buffer-changes--start) - (previous-token-start nil)) + (previous-token-start nil) + (tokens phps-mode/lexer-tokens)) ;; (message "Looking for state to rewind to for %s in stack %s" change-start states) + + ;; Find state and state stack before point of change + ;; also determine were previous token to change starts (catch 'stop-iteration (dolist (state-object states) (let ((start (nth 0 state-object)) @@ -1301,14 +1305,33 @@ ANY_CHAR' (push state-object new-states)) (when (> start change-start) (throw 'stop-iteration nil))))) + (if (and state state-stack) - (progn - (setq phps-mode/STATE state) - (setq phps-mode/state_stack state-stack) - (setq phps-mode/lexer-states new-states) - ;; (message "Rewinding lex to state: %s and stack: %s and states: %s and start: %s" state state-stack new-states previous-token-start) - (setq phps-mode/lexer-tokens (semantic-lex previous-token-start (point-max)))) + (let ((old-tokens '())) + + ;; Build new list of tokens before point of change + (catch 'stop-iteration + (dolist (token tokens) + (let ((start (car (cdr token)))) + (if (< start previous-token-start) + (push token old-tokens) + (throw 'stop-iteration nil) + )))) + (setq old-tokens (nreverse old-tokens)) + + (let* ((new-tokens (semantic-lex previous-token-start (point-max))) + (appended-tokens (append old-tokens new-tokens))) + ;; (message "old-tokens: %s, new-tokens: %s" old-tokens new-tokens) + (setq phps-mode/lexer-tokens appended-tokens) + (setq phps-mode/STATE state) + (setq phps-mode/state_stack state-stack) + (setq phps-mode/lexer-states new-states) + ;; TODO Should clear overlays after point of change here + ;; (message "Rewinding lex to state: %s and stack: %s and states: %s and start: %s old tokens: %s" state state-stack new-states previous-token-start old-tokens) + + ;; TODO Here clear all tokens after previous-token-start and add new tokens to stack + )) (display-warning "phps-mode" (format "Found no state to rewind to for %s in stack %s" change-start states)) (phps-mode/lex--RUN))) (setq phps-mode/buffer-changes--start nil)))