Skip to content

Commit

Permalink
Merge pull request #19 from gregersson/master
Browse files Browse the repository at this point in the history
Added opt argument for js2r-forward-slurp/barf.
  • Loading branch information
NicolasPetton committed Feb 12, 2015
2 parents eb143fd + e8760aa commit 3d30c0b
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 6 deletions.
74 changes: 74 additions & 0 deletions features/js2r-barf.feature
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,77 @@ Feature: JS Barf
ghi();
jkl();
"""

Scenario: Barfing with negative prefix does a single barf
When I insert:
"""
function abc() {
def();
ghi();
}
jkl();
"""
And I turn on js2-mode
And I go to the front of the word "abc"
And I press "M-- 1 C-c C-m ba"
Then I should see:
"""
function abc() {
def();
}
ghi();
jkl();
"""

Scenario: Barfing several statements using number prefix
When I insert:
"""
function abc() {
def();
ghi();
jkl();
}
mno();
"""

And I turn on js2-mode
And I go to the front of the word "abc"
And I press "M-2 C-c C-m ba"
Then I should see:
"""
function abc() {
def();
}
ghi();
jkl();
mno();
"""

Scenario: Barfing multiline statement followed by single line statement
When I insert:
"""
function abc() {
def();
ghi({
jkl: 1,
mno: 2
});
pqr();
}
jkl();
"""
And I turn on js2-mode
And I go to the front of the word "abc"
And I press "M-2 C-c C-m ba"
Then I should see:
"""
function abc() {
def();
}
ghi({
jkl: 1,
mno: 2
});
pqr();
jkl();
"""
73 changes: 73 additions & 0 deletions features/js2r-slurp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,76 @@ Feature: JS Slurp
}
jkl();
"""
Scenario: Slurping with negative prefix does a single slurp
When I insert:
"""
if (abc) {
}
ghi();
jkl();
"""
And I turn on js2-mode
And I go to the front of the word "abc"
And I press "M-- 1 C-c C-m sl"
Then I should see:
"""
if (abc) {
ghi();
}
jkl();
"""

Scenario: Slurping several statements using number prefix
When I insert:
"""
function abc() {
def();
}
jkl();
mno();
pqr();
stu();
"""
And I turn on js2-mode
And I go to the front of the word "abc"
And I press "M-3 C-c C-m sl"
Then I should see:
"""
function abc() {
def();
jkl();
mno();
pqr();
}
stu();
"""

Scenario: Slurping multiline statement followed by single line statement
When I insert:
"""
if (abc) {
def();
} else {}
ghi({
jkl: 1,
mno: 2
});
jkl();
pqr();
"""
And I turn on js2-mode
And I go to the front of the word "else"
And I press "M-2 C-c C-m sl"
Then I should see:
"""
if (abc) {
def();
} else {
ghi({
jkl: 1,
mno: 2
});
jkl();
}
pqr();
"""
28 changes: 22 additions & 6 deletions js2r-paredit.el
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,24 @@ When at the beginning of the node, kill from outside of it."
(js2r--kill-line-in-sexp)
(kill-region beg (1- node-end)))))

(defun js2r-forward-slurp ()
(interactive)
(defun js2r-forward-slurp (&optional arg)
(interactive "p")
(js2r--guard)
(let* ((nesting (js2r--closest 'js2r--nesting-node-p))
(standalone (if (js2r--standalone-node-p nesting)
nesting
(js2-node-parent-stmt nesting)))
(next-sibling (js2-node-next-sibling standalone))
(beg (js2-node-abs-pos next-sibling))
(end (1+ (js2-node-abs-end next-sibling))) ;; include whitespace after statement
(last-sibling (if (wholenump arg)
(let ((num arg)
(iter-sibling next-sibling))
(while (> num 1) ;; Do next-sibling arg nbr of times
(setq iter-sibling (js2-node-next-sibling iter-sibling))
(setq num (1- num)))
iter-sibling)
next-sibling)) ;; No optional arg. Just use next-sibling
(end (1+ (js2-node-abs-end last-sibling))) ;; include whitespace after statement
(text (buffer-substring beg end)))
(save-excursion
(delete-region beg end)
Expand All @@ -150,8 +158,8 @@ When at the beginning of the node, kill from outside of it."
(insert text)
(indent-region beg end))))

(defun js2r-forward-barf ()
(interactive)
(defun js2r-forward-barf (&optional arg)
(interactive "p")
(js2r--guard)
(let* ((nesting (js2r--closest 'js2r--nesting-node-p))
(standalone (if (js2r--standalone-node-p nesting)
Expand All @@ -161,8 +169,16 @@ When at the beginning of the node, kill from outside of it."
(last-child (car (last (if (js2-if-node-p nesting)
(js2-scope-kids (js2r--closest 'js2-scope-p))
(js2r--node-kids nesting)))))
(first-barf-child (if (wholenump arg)
(let ((num arg)
(iter-child last-child))
(while (> num 1) ;; Do prev-sibling arg nbr of times
(setq iter-child (js2-node-prev-sibling iter-child))
(setq num (1- num)))
iter-child)
last-child)); No optional arg. Just use last-child
(last-child-beg (save-excursion
(goto-char (js2-node-abs-pos last-child))
(goto-char (js2-node-abs-pos first-barf-child))
(skip-syntax-backward " ")
(while (looking-back "\n") (backward-char))
(point)))
Expand Down

0 comments on commit 3d30c0b

Please sign in to comment.