branch: externals/phps-mode commit 9e621570a8cd0f70f6465a9cda3c4ab9974264b8 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
(comment-region) and (uncomment-region) now handles part of lines as well as full lines --- phps-mode-functions.el | 101 +++++++++++++++++++++++++++++--------------- phps-mode-test-functions.el | 14 ++++++ 2 files changed, 82 insertions(+), 33 deletions(-) diff --git a/phps-mode-functions.el b/phps-mode-functions.el index 0de3711..cbc48a0 100644 --- a/phps-mode-functions.el +++ b/phps-mode-functions.el @@ -794,20 +794,36 @@ (goto-char beg) (let ((end-line-number (line-number-at-pos end t)) - (current-line-number (line-number-at-pos))) + (current-line-number (line-number-at-pos)) + (first-line t)) - ;; Do this for every line in region - (while (< current-line-number end-line-number) - (move-beginning-of-line nil) + ;; Does region start at beginning of line? + (if (not (= beg (line-beginning-position))) - ;; Does this line contain something other than white-space? - (unless (eq (point) (line-end-position)) - (insert comment-start) - (move-end-of-line nil) - (insert comment-end)) + ;; Use doc comment + (progn + (goto-char end) + (insert " */") + (goto-char beg) + (insert "/* ")) - (line-move 1) - (setq current-line-number (line-number-at-pos)))))) + ;; Do this for every line in region + (while (or first-line + (< current-line-number end-line-number)) + (move-beginning-of-line nil) + + (when first-line + (setq first-line nil)) + + ;; Does this line contain something other than white-space? + (unless (eq (point) (line-end-position)) + (insert "// ") + (move-end-of-line nil) + (insert "")) + + (when (< current-line-number end-line-number) + (line-move 1)) + (setq current-line-number (line-number-at-pos))))))) (defun phps-mode-functions-uncomment-region (beg end &optional _arg) "Comment region from BEG to END with optional ARG." @@ -817,28 +833,47 @@ (goto-char beg) (let ((end-line-number (line-number-at-pos end t)) - (current-line-number (line-number-at-pos))) - - ;; Do this for every line in region - (while (< current-line-number end-line-number) - (move-beginning-of-line nil) - - ;; Does this line contain something other than white-space? - (unless (>= (+ (point) 3) (line-end-position)) - (when (looking-at-p "\/\/ ") - (delete-char 3)) - (when (looking-at-p "\/\\* ") - (delete-char 3)) - - (move-end-of-line nil) - - (backward-char 3) - (when (looking-at-p " \\*\/") - (delete-char 3))) - - (line-move 1) - (setq current-line-number (line-number-at-pos)))))) - + (current-line-number (line-number-at-pos)) + (first-line t)) + + ;; Does region start at beginning of line? + (if (not (= beg (line-beginning-position))) + (progn + (goto-char end) + (backward-char 3) + (when (looking-at-p " \\*\/") + (delete-char 3)) + + (goto-char beg) + (when (looking-at-p "\/\/ ") + (delete-char 3)) + (when (looking-at-p "\/\\* ") + (delete-char 3))) + + ;; Do this for every line in region + (while (or first-line + (< current-line-number end-line-number)) + (move-beginning-of-line nil) + + (when first-line + (setq first-line nil)) + + ;; Does this line contain something other than white-space? + (unless (>= (+ (point) 3) (line-end-position)) + (when (looking-at-p "\/\/ ") + (delete-char 3)) + (when (looking-at-p "\/\\* ") + (delete-char 3)) + + (move-end-of-line nil) + + (backward-char 3) + (when (looking-at-p " \\*\/") + (delete-char 3))) + + (when (< current-line-number end-line-number) + (line-move 1)) + (setq current-line-number (line-number-at-pos))))))) (defun phps-mode-functions-init () "PHP specific init-cleanup routines." diff --git a/phps-mode-test-functions.el b/phps-mode-test-functions.el index e146b73..ae9386b 100644 --- a/phps-mode-test-functions.el +++ b/phps-mode-test-functions.el @@ -793,12 +793,26 @@ (should (equal buffer-contents "// <?php\n// namespace myNamespace;\n// class myClass extends myAbstract implements myInterface {\n// public function myFunctionA($myArg = null) {}\n// protected function myFunctionB($myArg = 'abc') {}\n// }\n")))) (phps-mode-test-with-buffer + "<?php\nnamespace myNamespace;\nclass myClass extends myAbstract implements myInterface {\n public function myFunctionA($myArg = null) {}\n protected function myFunctionB($myArg = 'abc') {}\n}\n" + "Comment part of object-oriented file with bracket-less namespace, class that extends and implements and functions with optional arguments" + (comment-region 62 86) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal buffer-contents "<?php\nnamespace myNamespace;\nclass myClass extends myAbstract/* implements myInterface */{\n public function myFunctionA($myArg = null) {}\n protected function myFunctionB($myArg = 'abc') {}\n}\n")))) + + (phps-mode-test-with-buffer "// <?php\n// namespace myNamespace;\n// class myClass extends myAbstract implements myInterface {\n// public function myFunctionA($myArg = null) {}\n// protected function myFunctionB($myArg = 'abc') {}\n// }\n" "Uncomment object-oriented file with bracket-less namespace, class that extends and implements and functions with optional arguments" (uncomment-region (point-min) (point-max)) (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) (should (equal buffer-contents "<?php\nnamespace myNamespace;\nclass myClass extends myAbstract implements myInterface {\n public function myFunctionA($myArg = null) {}\n protected function myFunctionB($myArg = 'abc') {}\n}\n")))) + (phps-mode-test-with-buffer + "<?php\nnamespace myNamespace;\nclass myClass extends myAbstract/* implements myInterface */{\n public function myFunctionA($myArg = null) {}\n protected function myFunctionB($myArg = 'abc') {}\n}\n" + "Uncomment part of object-oriented file with bracket-less namespace, class that extends and implements and functions with optional arguments" + (uncomment-region 62 92) + (let ((buffer-contents (buffer-substring-no-properties (point-min) (point-max)))) + (should (equal buffer-contents "<?php\nnamespace myNamespace;\nclass myClass extends myAbstract implements myInterface {\n public function myFunctionA($myArg = null) {}\n protected function myFunctionB($myArg = 'abc') {}\n}\n")))) + ) (defun phps-mode-test-functions ()