From db2bff47e8b009e78a2a85a839b799c4a6711f5b Mon Sep 17 00:00:00 2001 From: "Matthew M. Keeler" Date: Sun, 18 May 2014 07:29:00 -0400 Subject: [PATCH] Setup tests and define the minor mode --- .gitignore | 1 + Cask | 7 ++ README.md | 22 +++++- emacs-php-refactoring.el | 55 -------------- features/php-refactor.feature | 94 ++++++++++++++++++++++++ features/step-definitions/basic-steps.el | 3 + features/support/env.el | 22 ++++++ php-refactor-mode.el | 94 ++++++++++++++++++++++++ 8 files changed, 241 insertions(+), 57 deletions(-) create mode 100644 .gitignore create mode 100644 Cask delete mode 100644 emacs-php-refactoring.el create mode 100644 features/php-refactor.feature create mode 100644 features/step-definitions/basic-steps.el create mode 100644 features/support/env.el create mode 100644 php-refactor-mode.el diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64b1053 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.cask/ diff --git a/Cask b/Cask new file mode 100644 index 0000000..1ce04ff --- /dev/null +++ b/Cask @@ -0,0 +1,7 @@ +(source melpa) + +(package "php-refactor-mode" "0.0.1" "Execute some common PHP refactorings") + +(development + (depends-on "ecukes") + (depends-on "espuds")) diff --git a/README.md b/README.md index 594df65..689d633 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Emacs PHP Refactoring +# php-refactor-mode This is a minor mode which provides convenient access to the refactoring methods provided by @@ -6,4 +6,22 @@ methods provided by ## Keybindings -All keybindings will start with `C-c r` +All keybindings in php-refactor-mode start with 'C-c r' followed by some +mnemonic shortcut. + +* `lv`: Convert a local variable to an instance variable +* `rv`: Rename a local variable +* `em`: Select a region and extract it to a new method +* `ou`: Optimize use statments for FQCNs + +## Development + +To fetch the test dependencies, install +[cask](https://github.com/rejeep/cask.el) if you haven't already, then: + + $ cd /path/to/php-refactor-mode + $ cask + +Run the tests with: + + $ cask exec ecukes diff --git a/emacs-php-refactoring.el b/emacs-php-refactoring.el deleted file mode 100644 index 3576181..0000000 --- a/emacs-php-refactoring.el +++ /dev/null @@ -1,55 +0,0 @@ -;;; package -- summary -;;; Commentary: - -;;; Code: -(defvar php-refactoring-command "refactor.phar") -(defvar php-refactoring-patch-command "patch -p1") - -(defun php-refactoring--convert-local-to-instance-variable () - "Convert a local variable into an instance variable of the class." - (interactive) - (php-refactoring--run-command - "convert-local-to-instance-variable" - (list (buffer-file-name) (number-to-string (locate-current-line-number)) (concat "'" (word-at-point) "'")))) - -(defun php-refactoring--optimize-use () - "Optimizes the use of Fully qualified names in a file." - (interactive) - (php-refactoring--run-command "optimize-use" (list (buffer-file-name)))) - -(defun php-refactoring--extract-method (begin end) - "Extract the selected region into a separate method. - -BEGIN is the starting position of the selected region. -END is the ending position of the selected region." - (interactive "r") - (let ((region-start (number-to-string (line-number-at-pos begin))) - (region-end (number-to-string (line-number-at-pos end)))) - (php-refactoring--run-command - "extract-method" - (list (buffer-file-name) (concat region-start "-" region-end) "newMethomdName")))) - -(defun php-refactoring--rename-local-variable () - "Rename an existing local variable to the specified new name." - (interactive) - (php-refactoring--run-command - "rename-local-variable" - (list (buffer-file-name) (number-to-string (locate-current-line-number)) (concat "'" (word-at-point) "'") "renamed"))) - -(defun php-refactoring--run-command (method args) - "Execute the given refactoring command and apply the resulting patch. - -METHOD specifies the refactoring method to run. -ARGS contains a list of flags required for the specific method to run." - - ;; TODO We need to make sure the file is saved first. Otherwise, we can't do this. - (shell-command - (mapconcat 'identity - (list php-refactoring-command method - (mapconcat 'identity args " ") "| tee ~/patch |" php-refactoring-patch-command) " ") - nil) - (revert-buffer nil t)) - -(provide 'emacs-php-refactoring) - -;;; emacs-php-refactoring ends here diff --git a/features/php-refactor.feature b/features/php-refactor.feature new file mode 100644 index 0000000..b2fe48f --- /dev/null +++ b/features/php-refactor.feature @@ -0,0 +1,94 @@ +Feature: PHP Refactor + + Scenario: Convert Local to Instance Variable + When I open temp file "convert-local-to-instance-variable" + And I insert: + """ + newMethomdName(); + + echo $localVariable; + } + + private function newMethomdName() + { + $localVariable = "Hello"; + $localVariable += " World"; + + return $localVariable; + } + } + """ diff --git a/features/step-definitions/basic-steps.el b/features/step-definitions/basic-steps.el new file mode 100644 index 0000000..77faaa2 --- /dev/null +++ b/features/step-definitions/basic-steps.el @@ -0,0 +1,3 @@ +(And "^I save the buffer$" + (lambda () + (save-buffer))) diff --git a/features/support/env.el b/features/support/env.el new file mode 100644 index 0000000..d3993f4 --- /dev/null +++ b/features/support/env.el @@ -0,0 +1,22 @@ +(let* ((current-directory (file-name-directory load-file-name)) + (features-directory (expand-file-name ".." current-directory)) + (project-directory (expand-file-name ".." features-directory))) + (setq php-refactor-root-path project-directory)) + +(add-to-list 'load-path php-refactor-root-path) + +(require 'php-refactor-mode) +(require 'espuds) +(require 'ert) + +(Before + (switch-to-buffer + (get-buffer-create "*php-refactor*")) + (erase-buffer) + (transient-mark-mode 1) + (cua-mode 0) + (php-refactor-mode 0) + (setq set-mark-default-inactive nil) + (deactivate-mark)) + +(After) diff --git a/php-refactor-mode.el b/php-refactor-mode.el new file mode 100644 index 0000000..05b528a --- /dev/null +++ b/php-refactor-mode.el @@ -0,0 +1,94 @@ +;;; package -- summary +;;; Commentary: + +;;; Code: + +(require 'thingatpt) +(require 'locate) + +;; (defvar php-refactor-command "refactor.phar") +;; (defvar php-refactor-patch-command "patch -p1") + +(defgroup php-refactor nil + "Quickly and safely perform common refactorings." + :group 'tools + :group 'convenience) + +(defcustom php-refactor-command "refactor.phar" + "Define the command used for executing the refactoring." + :group 'php-refactor + :type 'symbol) + +(defcustom php-refactor-patch-command "patch -p1" + "Define the command used for applying the patch." + :group 'php-refactor + :type 'symbol) + +(defcustom php-refactor-keymap-prefix (kbd "C-c r") + "php-refactor keymap prefix." + :group 'php-refactor + :type 'string) + +(defvar php-refactor-mode-map + (let ((map (make-sparse-keymap))) + (let ((prefix-map (make-sparse-keymap))) + (define-key prefix-map (kbd "lv") 'php-refactor--convert-local-to-instance-variable) + (define-key prefix-map (kbd "rv") 'php-refactor--rename-local-variable) + (define-key prefix-map (kbd "em") 'php-refactor--extract-method) + (define-key prefix-map (kbd "ou") 'php-refactor--optimize-use) + (define-key map php-refactor-keymap-prefix prefix-map)) + map) + "Keymap for php-refactor mode.") + +(define-minor-mode php-refactor-mode + "Minor more to quickly and safely perform common refactorings." + nil " Refactor" php-refactor-mode-map) + +(defun php-refactor--convert-local-to-instance-variable () + "Convert a local variable into an instance variable of the class." + (interactive) + (php-refactor--run-command + "convert-local-to-instance-variable" + (list (buffer-file-name) (number-to-string (locate-current-line-number)) (concat "'" (word-at-point) "'")))) + +(defun php-refactor--optimize-use () + "Optimizes the use of Fully qualified names in a file." + (interactive) + (php-refactor--run-command "optimize-use" (list (buffer-file-name)))) + +(defun php-refactor--extract-method (begin end) + "Extract the selected region into a separate method. + +BEGIN is the starting position of the selected region. +END is the ending position of the selected region." + (interactive "r") + (let ((region-start (number-to-string (line-number-at-pos begin))) + (region-end (number-to-string (line-number-at-pos end)))) + (php-refactor--run-command + "extract-method" + (list (buffer-file-name) (concat region-start "-" region-end) "newMethomdName")))) + +(defun php-refactor--rename-local-variable () + "Rename an existing local variable to the specified new name." + (interactive) + (php-refactor--run-command + "rename-local-variable" + (list (buffer-file-name) (number-to-string (locate-current-line-number)) (concat "'" (word-at-point) "'") "renamed"))) + +(defun php-refactor--run-command (method args) + "Execute the given refactoring command and apply the resulting patch. + +METHOD specifies the refactoring method to run. +ARGS contains a list of flags required for the specific method to run." + + ;; TODO We need to make sure the file is saved first. Otherwise, we can't do this. + (shell-command + (mapconcat 'identity + (list php-refactor-command method + (mapconcat 'identity args " ") "| tee ~/patch |" php-refactor-patch-command) " ") + nil) + (revert-buffer nil t)) + +(provide 'php-refactor-mode) + +;;; emacs-php-refactor ends here