branch: externals/phps-mode
commit cef3c1b3f7597cc9fc63baed53cf6798c5527a92
Author: christian <christ...@cvj.se>
Commit: christian <christ...@cvj.se>

    More work on end-of-defun implementation
---
 phps-mode-lex-analyzer.el           | 54 ++++++++++++++++++++++++++++++++-----
 test/phps-mode-test-lex-analyzer.el |  1 +
 2 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/phps-mode-lex-analyzer.el b/phps-mode-lex-analyzer.el
index 660ac3f38f..d594334b0c 100644
--- a/phps-mode-lex-analyzer.el
+++ b/phps-mode-lex-analyzer.el
@@ -1528,17 +1528,59 @@ of performed operations.  Optionally do it 
FORCE-SYNCHRONOUS."
               (setq found-index (point)))
           (setq found-index nil))
         (setq index (1+ index))))
-    (when found-index
-      (goto-char found-index))
     (if found-index
-        t
+        (progn
+          (goto-char found-index)
+          t)
       nil)))
 
 (defun phps-mode-lex-analyzer--end-of-defun (&optional arg interactive)
   "Custom implementation of `end-of-defun'."
-  ;; TODO Implement this
-  ;; TODO should start from beginning-of-defun and scan until balance of 
brackets
-  )
+  (let ((found-index))
+    (save-excursion
+      (when (phps-mode-lex-analyzer--beginning-of-defun)
+        (let ((beginning (point))
+              (bracket-level 0)
+              (found-initial-bracket)
+              (next-bracket (search-forward-regexp "[{}\"']" nil t)))
+          (message "beginning: %S" beginning)
+          (while (and
+                  next-bracket
+                  (or
+                   (not found-initial-bracket)
+                   (not (= bracket-level 0))))
+            (let ((match-string (match-string 0)))
+              (message "match-string: %S" match-string)
+              (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 "\"")
+                ;; TODO Handle double quoted string here
+                )
+               ((string= match-string "'")
+                ;; TODO Handle single-quoted string here
+                )))
+            (unless (and
+                     found-initial-bracket
+                     (= bracket-level 0))
+              (setq
+               next-bracket
+               (search-forward-regexp "[{}\"']" nil t))))
+          (when (and
+                 (= bracket-level 0)
+                 found-initial-bracket)
+            (setq
+             found-index
+             (point))))))
+    (if found-index
+        (progn
+          (goto-char found-index)
+          t)
+      nil)))
 
 (provide 'phps-mode-lex-analyzer)
 
diff --git a/test/phps-mode-test-lex-analyzer.el 
b/test/phps-mode-test-lex-analyzer.el
index 07552ec662..946903be63 100644
--- a/test/phps-mode-test-lex-analyzer.el
+++ b/test/phps-mode-test-lex-analyzer.el
@@ -157,6 +157,7 @@
 
   (phps-mode-test--with-buffer
    "<?php\nfunction test($a) {\n    return $a + 1;\n}\necho 'here';\n"
+   "Test beginning-of-defun and end-of-defun"
    (goto-char 27)
    (should (equal (phps-mode-lex-analyzer--beginning-of-defun) t))
    (should (equal (point) 7))

Reply via email to