branch: externals/phps-mode commit 8587c24bbddb547d4178b263d9589615cf62ece3 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Passing tests for beginning-of-defun, end-of-defun and narrow-to-defun --- phps-mode-lex-analyzer.el | 165 +++++++++++++++++++----------------- test/phps-mode-test-lex-analyzer.el | 13 +++ 2 files changed, 98 insertions(+), 80 deletions(-) diff --git a/phps-mode-lex-analyzer.el b/phps-mode-lex-analyzer.el index deeb62ac47..e3b66ba516 100644 --- a/phps-mode-lex-analyzer.el +++ b/phps-mode-lex-analyzer.el @@ -1518,100 +1518,105 @@ of performed operations. Optionally do it FORCE-SYNCHRONOUS." (save-excursion (move-end-of-line nil) (while (and found-index (< index iterations)) - (if + (setq found-index nil) + (setq index (1+ index)) + (when (search-backward-regexp "[\n\t ]+function\\([\t\n ]+\\|(\\)" nil t) - (progn - (search-forward-regexp - "[\n]+") - (setq found-index (point))) - (setq found-index nil)) - (setq index (1+ index)))) + (search-forward-regexp "[\n]*") + (move-beginning-of-line nil) + (setq found-index (point))))) (if found-index (progn (goto-char found-index) t) nil))) -(defun phps-mode-lex-analyzer--end-of-defun (&optional arg interactive) +(defun phps-mode-lex-analyzer--end-of-defun (&optional arg _interactive) "Custom implementation of `end-of-defun'." - (let ((found-index)) + (let ((found-index t) + (index 0) + (iterations (if arg arg 1))) (save-excursion - (when (phps-mode-lex-analyzer--beginning-of-defun) - (let ((beginning (point)) - (bracket-level 0) - (found-initial-bracket) - (failed-to-find-ending-quote)) - (while (and - (not failed-to-find-ending-quote) - (or - (not found-initial-bracket) - (not (= bracket-level 0))) - (search-forward-regexp "[{}\"']" nil t)) - (let ((match-string (match-string 0))) - (cond - ((string= match-string "{") - (unless found-initial-bracket - (setq found-initial-bracket t)) - (setq bracket-level (1+ bracket-level))) - ((string= match-string "}") - (setq bracket-level (1- bracket-level))) - ((string= match-string "\"") - (let ((is-escaped) - (quote-ending-at)) - (save-excursion - (backward-char 2) - (while (looking-at-p "\\\\") - (setq is-escaped (not is-escaped)) - (backward-char))) - (unless is-escaped + (while (and + found-index + (< index iterations)) + (setq found-index nil) + (setq index (1+ index)) + (when (phps-mode-lex-analyzer--beginning-of-defun) + (let ((bracket-level 0) + (found-initial-bracket) + (failed-to-find-ending-quote)) + (while (and + (not failed-to-find-ending-quote) + (or + (not found-initial-bracket) + (not (= bracket-level 0))) + (search-forward-regexp "[{}\"']" nil t)) + (let ((match-string (match-string 0))) + (cond + ((string= match-string "{") + (unless found-initial-bracket + (setq found-initial-bracket t)) + (setq bracket-level (1+ bracket-level))) + ((string= match-string "}") + (setq bracket-level (1- bracket-level))) + ((string= match-string "\"") + (let ((is-escaped) + (quote-ending-at)) (save-excursion - (while (and - (not quote-ending-at) - (search-forward-regexp "\"" nil t)) - (let ((is-escaped-ending)) - (save-excursion - (backward-char 2) - (while (looking-at-p "\\\\") - (setq is-escaped-ending (not is-escaped-ending)) - (backward-char))) - (unless is-escaped-ending - (setq quote-ending-at (point))))))) - (if quote-ending-at - (goto-char quote-ending-at) - (setq failed-to-find-ending-quote t)))) - ((string= match-string "'") - (let ((is-escaped) - (quote-ending-at)) - (save-excursion - (backward-char 2) - (while (looking-at-p "\\\\") - (setq is-escaped (not is-escaped)) - (backward-char))) - (unless is-escaped + (backward-char 2) + (while (looking-at-p "\\\\") + (setq is-escaped (not is-escaped)) + (backward-char))) + (unless is-escaped + (save-excursion + (while (and + (not quote-ending-at) + (search-forward-regexp "\"" nil t)) + (let ((is-escaped-ending)) + (save-excursion + (backward-char 2) + (while (looking-at-p "\\\\") + (setq is-escaped-ending (not is-escaped-ending)) + (backward-char))) + (unless is-escaped-ending + (setq quote-ending-at (point))))))) + (if quote-ending-at + (goto-char quote-ending-at) + (setq failed-to-find-ending-quote t)))) + ((string= match-string "'") + (let ((is-escaped) + (quote-ending-at)) (save-excursion - (while (and - (not quote-ending-at) - (search-forward-regexp "'" nil t)) - (let ((is-escaped-ending)) - (save-excursion - (backward-char 2) - (while (looking-at-p "\\\\") - (setq is-escaped-ending (not is-escaped-ending)) - (backward-char))) - (unless is-escaped-ending - (setq quote-ending-at (point))))))) - (if quote-ending-at - (goto-char quote-ending-at) - (setq failed-to-find-ending-quote t))))))) - (when (and - (= bracket-level 0) - found-initial-bracket) - (setq - found-index - (point)))))) + (backward-char 2) + (while (looking-at-p "\\\\") + (setq is-escaped (not is-escaped)) + (backward-char))) + (unless is-escaped + (save-excursion + (while (and + (not quote-ending-at) + (search-forward-regexp "'" nil t)) + (let ((is-escaped-ending)) + (save-excursion + (backward-char 2) + (while (looking-at-p "\\\\") + (setq is-escaped-ending (not is-escaped-ending)) + (backward-char))) + (unless is-escaped-ending + (setq quote-ending-at (point))))))) + (if quote-ending-at + (goto-char quote-ending-at) + (setq failed-to-find-ending-quote t))))))) + (when (and + (= bracket-level 0) + found-initial-bracket) + (setq + found-index + (point))))))) (if found-index (progn (goto-char found-index) diff --git a/test/phps-mode-test-lex-analyzer.el b/test/phps-mode-test-lex-analyzer.el index 1d0f95a834..f906f9dc54 100644 --- a/test/phps-mode-test-lex-analyzer.el +++ b/test/phps-mode-test-lex-analyzer.el @@ -181,6 +181,19 @@ (should (equal (point-min) 7)) (should (equal (point-max) 108))) + (phps-mode-test--with-buffer + "<?php\necho 'here';\n$var = function() {\n echo 'here';\n};\necho 'there';" + "Test `beginning-of-defun', `end-of-defun' and `narrow-to-defun' for a anonymous function." + (goto-char 53) + (should (equal (phps-mode-lex-analyzer--beginning-of-defun) t)) + (should (equal (point) 20)) + (should (equal (phps-mode-lex-analyzer--end-of-defun) t)) + (should (equal (point) 58)) + (goto-char 47) + (narrow-to-defun) + (should (equal (point-min) 20)) + (should (equal (point-max) 58))) + ) (defun phps-mode-test-lex-analyzer ()