Skip to content

Commit

Permalink
Normalize region before indent/dedent a region
Browse files Browse the repository at this point in the history
If the first or last line are not fully selected, select them completely
then indent/dedent that region.
  • Loading branch information
ChillarAnand committed Aug 30, 2015
1 parent d0138f1 commit 45d3236
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/editing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ Moving the Current Region
:kbd: M-down
.. command:: elpy-nav-move-line-or-region-up
:kbd: M-up
.. command:: python-indent-shift-left
.. command:: elpy-nav-indent-shift-left
:kbd: M-left
.. command:: python-indent-shift-right
.. command:: elpy-nav-indent-shift-right
:kbd: M-right

Elpy can move the selected region (or the current line if no region is
Expand Down
45 changes: 43 additions & 2 deletions elpy.el
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ edited instead. Setting this variable to nil disables this feature."

(define-key map (kbd "<M-down>") 'elpy-nav-move-line-or-region-down)
(define-key map (kbd "<M-up>") 'elpy-nav-move-line-or-region-up)
(define-key map (kbd "<M-left>") 'python-indent-shift-left)
(define-key map (kbd "<M-right>") 'python-indent-shift-right)
(define-key map (kbd "<M-left>") 'elpy-nav-indent-shift-left)
(define-key map (kbd "<M-right>") 'elpy-nav-indent-shift-right)

(define-key map (kbd "M-.") 'elpy-goto-definition)
(define-key map (kbd "M-TAB") 'elpy-company-backend)
Expand Down Expand Up @@ -1707,6 +1707,41 @@ indentation levels."
(forward-line 1))
(backward-char)))

(defun elpy-nav-normalize-region ()
"If the first or last line are not fully selected, select them completely."
(let ((beg (region-beginning))
(end (region-end)))
(goto-char beg)
(beginning-of-line)
(push-mark (point) nil t)
(goto-char end)
(end-of-line)))

(defun elpy-nav-indent-shift-right (&optional count)
"Shift current line by COUNT columns to the right.
COUNT defaults to `python-indent-offset'.
If region is active, normalize the region and shift."
(interactive)
(if (use-region-p)
(progn
(elpy-nav-normalize-region)
(python-indent-shift-right (region-beginning) (region-end) current-prefix-arg))
(python-indent-shift-right (line-beginning-position) (line-end-position) current-prefix-arg)))

(defun elpy-nav-indent-shift-left (&optional count)
"Shift current line by COUNT columns to the left.
COUNT defaults to `python-indent-offset'.
If region is active, normalize the region and shift."
(interactive)
(if (use-region-p)
(progn
(elpy-nav-normalize-region)
(python-indent-shift-left (region-beginning) (region-end) current-prefix-arg))
(python-indent-shift-left (line-beginning-position) (line-end-position) current-prefix-arg)))


;;;;;;;;;;;;;;;;
;;; Test running

Expand Down Expand Up @@ -3378,6 +3413,12 @@ description."
(when (not (fboundp 'python-shell-send-string))
(defalias 'python-shell-send-string 'python-send-string))

(when (not (fboundp 'python-indent-shift-right))
(defalias 'python-indent-shift-right 'python-shift-right))

(when (not (fboundp 'python-indent-shift-left))
(defalias 'python-indent-shift-left 'python-shift-left))

;; Emacs 24.2 made `locate-dominating-file' accept a predicate instead
;; of a string. Simply overwrite the current one, it's
;; backwards-compatible. The code below is taken from Emacs 24.3.
Expand Down
38 changes: 38 additions & 0 deletions test/elpy-nav-indent-shift-left-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ert-deftest elpy-nav-indent-shift-left-should-indent-left-single-statement ()
(elpy-testcase ()
(set-buffer-string-with-point
" print_|_('one')")

(elpy-nav-indent-shift-left)

(should
(buffer-be
"print_|_('one')"))))

(ert-deftest elpy-nav-indent-shift-left-should-indent-left-region ()
(elpy-testcase ()
(set-buffer-string-with-point
" print_|_('one')"
" print('two')"
)

(elpy/mark-region (point)
(- (point-max) 5))
(elpy-nav-indent-shift-left)

(should
(buffer-be
"print('one')"
"print('two')_|_"))))

(ert-deftest elpy-nav-indent-shift-left-should-indent-left-two-spaces ()
(elpy-testcase ()
(set-buffer-string-with-point
" print_|_('one')")

(let ((current-prefix-arg '(2)))
(call-interactively 'elpy-nav-indent-shift-left))

(should
(buffer-be
" print_|_('one')"))))
38 changes: 38 additions & 0 deletions test/elpy-nav-indent-shift-right-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ert-deftest elpy-nav-indent-shift-right-should-indent-right-single-statement ()
(elpy-testcase ()
(set-buffer-string-with-point
"print_|_('one')")

(elpy-nav-indent-shift-right)

(should
(buffer-be
" print_|_('one')"))))

(ert-deftest elpy-nav-indent-shift-right-should-indent-right-region ()
(elpy-testcase ()
(set-buffer-string-with-point
"print_|_('one')"
"print('two')"
)

(elpy/mark-region (point)
(- (point-max) 5))
(elpy-nav-indent-shift-right)

(should
(buffer-be
" print('one')"
" print('two')_|_"))))

(ert-deftest elpy-nav-indent-shift-right-should-indent-right-two-spaces ()
(elpy-testcase ()
(set-buffer-string-with-point
"print_|_('one')")

(let ((current-prefix-arg '(2)))
(call-interactively 'elpy-nav-indent-shift-right))

(should
(buffer-be
" print_|_('one')"))))

0 comments on commit 45d3236

Please sign in to comment.