Skip to content

Commit

Permalink
Merge pull request #79 from futurist/master
Browse files Browse the repository at this point in the history
ADD: js2r-universal-expand-contract func to work with array,object,func
  • Loading branch information
NicolasPetton authored Oct 25, 2016
2 parents 1d15ffd + 186de64 commit bd73f03
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 44 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ A JavaScript refactoring library for emacs.
This is a collection of small refactoring functions to further the idea of a
JavaScript IDE in Emacs that started with js2-mode.

## Change in 0.8.0

Add `expand-node-at-point` and `contract-node-at-point` function to Expand / Contract bracketed list according to node type at point (array, object, function, call args).

Removed previous `ea` `ca` `eo` `co` `eu` `cu` `ec` `cc` key bindings.

## Breaking change in 0.7.0

js2-refactor.el is now a minor mode that has to be enabled, with
Expand Down Expand Up @@ -68,16 +74,12 @@ to pick and choose your own keybindings with a smattering of:

## Refactorings

* `ee` is `expand-node-at-point`: Expand bracketed list according to node type at point (array, object, function, call args).
* `cc` is `contract-node-at-point`: Contract bracketed list according to node type at point (array, object, function, call args).
* `ef` is `extract-function`: Extracts the marked expressions out into a new named function.
* `em` is `extract-method`: Extracts the marked expressions out into a new named method in an object literal.
* `ip` is `introduce-parameter`: Changes the marked expression to a parameter in a local function.
* `lp` is `localize-parameter`: Changes a parameter to a local var in a local function.
* `eo` is `expand-object`: Converts a one line object literal to multiline.
* `co` is `contract-object`: Converts a multiline object literal to one line.
* `eu` is `expand-function`: Converts a one line function to multiline (expecting semicolons as statement delimiters).
* `cu` is `contract-function`: Converts a multiline function to one line (expecting semicolons as statement delimiters).
* `ea` is `expand-array`: Converts a one line array to multiline.
* `ca` is `contract-array`: Converts a multiline array to one line.
* `wi` is `wrap-buffer-in-iife`: Wraps the entire buffer in an immediately invoked function expression
* `ig` is `inject-global-in-iife`: Creates a shortcut for a marked global by injecting it in the wrapping immediately invoked function expression
* `ag` is `add-to-globals-annotation`: Creates a `/*global */` annotation if it is missing, and adds the var at point to it.
Expand Down Expand Up @@ -106,8 +108,10 @@ There are also some minor conveniences bundled:

A list of some wanted improvements for the current refactorings.

* expand- and contract-array: should work recursively with nested
object literals and nested arrays.
* ~~expand- and contract-array: should work recursively with nested
object literals and nested arrays.~~
Now the `expand-node-at-point` and `contract-node-at-point` should work,
by moving point into right place.
* expand- and contract-function: should deal better with nested
object literals, array declarations, and statements terminated only
by EOLs (without semicolons).
Expand All @@ -120,6 +124,7 @@ A list of some wanted improvements for the current refactorings.
* [Alex Chamberlain](https://github.com/apchamberlain) contributed contracting and expanding arrays and functions.
* [Nicolas Petton](https://github.com/NicolasPetton) contributed lots of stuff and is now a co-maintainer of the project.
* [Brian J Brennan](https://github.com/brianloveswords) added support for `const` and `let` to inline-var.
* [James Yang](https://github.com/futurist) added `expand-node-at-point` and `contract-node-at-point` functions.


Thanks!
Expand Down
129 changes: 110 additions & 19 deletions features/js2r-expand-collapse.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Feature: Expand and collapse things
When I insert "var a = { b: 1, c: 'def' };"
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "b"
And I press "C-c C-m eo"
And I press "C-c C-m ee"
Then I should see:
"""
var a = {
Expand All @@ -17,7 +17,7 @@ Feature: Expand and collapse things
When I insert "var a = { b: 1, c: 'def, ghi' };"
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "b"
And I press "C-c C-m eo"
And I press "C-c C-m ee"
Then I should see:
"""
var a = {
Expand All @@ -36,7 +36,7 @@ Feature: Expand and collapse things
"""
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "b"
And I press "C-c C-m co"
And I press "C-c C-m cc"
Then I should see "var a = { b: 1, c: 'def' };"

Scenario: Contracting objects with comma
Expand All @@ -49,14 +49,14 @@ Feature: Expand and collapse things
"""
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "b"
And I press "C-c C-m co"
And I press "C-c C-m cc"
Then I should see "var a = { b: 1, c: 'def, ghi' };"

Scenario: Expanding functions
When I insert "function f (a, b, c) { var t = a + b + c; return t; }"
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "var"
And I press "C-c C-m eu"
And I press "C-c C-m ee"
Then I should see:
"""
function f (a, b, c) {
Expand All @@ -69,7 +69,7 @@ Feature: Expand and collapse things
When I insert "function f (a, b, c) { var t = a + b + c; var arr = [1, 2, 3, a, b]; return [t, arr]; }"
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "var"
And I press "C-c C-m eu"
And I press "C-c C-m ee"
Then I should see:
"""
function f (a, b, c) {
Expand All @@ -83,7 +83,7 @@ Feature: Expand and collapse things
When I insert "function f (a, b, c) { var t = a + b + c; var o = {e1: a, e2: b + 1, e3: 'xyzzy'}; return o; }"
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "var"
And I press "C-c C-m eu"
And I press "C-c C-m ee"
Then I should see:
"""
function f (a, b, c) {
Expand All @@ -92,18 +92,18 @@ Feature: Expand and collapse things
return o;
}
"""

Scenario: Expanding arrow functions
When I insert "var arrowFunc = (a, b, c) => { return a + b + c; }"
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "return"
And I press "C-c C-m eu"
And I press "C-c C-m ee"
Then I should see:
"""
var arrowFunc = (a, b, c) => {
return a + b + c;
}
"""
"""

Scenario: Contracting arrow functions
When I insert:
Expand All @@ -114,7 +114,7 @@ Feature: Expand and collapse things
"""
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "return"
And I press "C-c C-m cu"
And I press "C-c C-m cc"
Then I should see:
"""
var arrowFunc = (a, b, c) => { return a + b + c; }
Expand All @@ -130,7 +130,7 @@ Feature: Expand and collapse things
"""
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "var"
And I press "C-c C-m cu"
And I press "C-c C-m cc"
Then I should see "function f (a, b, c) { var t = a + b + c; return t; }"

Scenario: Contracting functions containing arrays
Expand All @@ -144,7 +144,7 @@ Feature: Expand and collapse things
"""
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "var"
And I press "C-c C-m cu"
And I press "C-c C-m cc"
Then I should see "function f (a, b, c) { var t = a + b + c; var arr = [1, 2, 3, a, b]; return [t, arr]; }"

Scenario: Contracting functions containing object literals
Expand All @@ -158,7 +158,7 @@ Feature: Expand and collapse things
"""
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "var"
And I press "C-c C-m cu"
And I press "C-c C-m cc"
Then I should see "function f (a, b, c) { var t = a + b + c; var o = {e1: a, e2: b + 1, e3: 'xyzzy'}; return o; }"

Scenario: Expanding function call arguments
Expand All @@ -168,7 +168,7 @@ Feature: Expand and collapse things
"""
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "overlay"
And I press "C-c C-m ec"
And I press "C-c C-m ee"
Then I should see:
"""
m(
Expand All @@ -178,7 +178,7 @@ Feature: Expand and collapse things
);
"""
And I go to the front of the word "tr"
And I press "C-c C-m ec"
And I press "C-c C-m ee"
Then I should see:
"""
m(
Expand Down Expand Up @@ -248,7 +248,7 @@ Feature: Expand and collapse things
When I insert "var a = [ b, 1, c, 3.1415927 ];"
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "b"
And I press "C-c C-m ea"
And I press "C-c C-m ee"
Then I should see:
"""
var a = [
Expand All @@ -271,7 +271,7 @@ Feature: Expand and collapse things
"""
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "b"
And I press "C-c C-m ca"
And I press "C-c C-m cc"
Then I should see "var a = [ b, 1, c, 3.1415927 ];"

Scenario: Expanding arrays with comment
Expand All @@ -284,7 +284,7 @@ Feature: Expand and collapse things
"""
And I turn on js2-mode and js2-refactor-mode
And I go to character "b"
And I press "C-c C-m ea"
And I press "C-c C-m ee"
Then I should see:
"""
var a = [
Expand All @@ -293,3 +293,94 @@ Feature: Expand and collapse things
4
];
"""

Scenario: Expanding and contracting node at point
When I insert:
"""
var a = [1, 2, 3];
var b = {c:4, d:5};
function abc(x,y){x+=z; return x+y;}
func(6,7);
"""
And I turn on js2-mode and js2-refactor-mode
And I go to the front of the word "1"
And I press "C-c C-m ee"
Then I should see:
"""
var a = [
1,
2,
3
];
var b = {c:4, d:5};
function abc(x,y){x+=z; return x+y;}
func(6,7);
"""
When I press "C-c C-m cc"
Then I should see:
"""
var a = [ 1, 2, 3 ];
var b = {c:4, d:5};
function abc(x,y){x+=z; return x+y;}
func(6,7);
"""
When I go to the front of the word "4"
And I press "C-c C-m ee"
Then I should see:
"""
var a = [ 1, 2, 3 ];
var b = {
c:4,
d:5
};
function abc(x,y){x+=z; return x+y;}
func(6,7);
"""
When I press "C-c C-m cc"
Then I should see:
"""
var a = [ 1, 2, 3 ];
var b = { c:4, d:5 };
function abc(x,y){x+=z; return x+y;}
func(6,7);
"""
When I go to the front of the word "z"
And I press "C-c C-m ee"
Then I should see:
"""
var a = [ 1, 2, 3 ];
var b = { c:4, d:5 };
function abc(x,y){
x+=z;
return x+y;
}
func(6,7);
"""
When I press "C-c C-m cc"
Then I should see:
"""
var a = [ 1, 2, 3 ];
var b = { c:4, d:5 };
function abc(x,y){ x+=z; return x+y; }
func(6,7);
"""
When I go to the front of the word "6"
And I press "C-c C-m ee"
Then I should see:
"""
var a = [ 1, 2, 3 ];
var b = { c:4, d:5 };
function abc(x,y){ x+=z; return x+y; }
func(
6,
7
);
"""
When I press "C-c C-m cc"
Then I should see:
"""
var a = [ 1, 2, 3 ];
var b = { c:4, d:5 };
function abc(x,y){ x+=z; return x+y; }
func( 6, 7 );
"""
2 changes: 1 addition & 1 deletion js2-refactor-pkg.el
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(define-package "js2-refactor" "0.7.1"
(define-package "js2-refactor" "0.8.0"
"A JavaScript refactoring library for emacs."
'((js2-mode "20101228") (s "1.9.0") (multiple-cursors "1.0.0") (dash "1.0.0") (s "1.0.0") (yasnippet "0.9.0.1")))
20 changes: 4 additions & 16 deletions js2-refactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,12 @@

;; All refactorings start with `C-c C-m` and then a two-letter mnemonic shortcut.

;; * `ee` is `expand-node-at-point`: Expand bracketed list according to node type at point (array, object, function, call args).
;; * `cc` is `contract-node-at-point`: Contract bracketed list according to node type at point (array, object, function, call args).
;; * `ef` is `extract-function`: Extracts the marked expressions out into a new named function.
;; * `em` is `extract-method`: Extracts the marked expressions out into a new named method in an object literal.
;; * `ip` is `introduce-parameter`: Changes the marked expression to a parameter in a local function.
;; * `lp` is `localize-parameter`: Changes a parameter to a local var in a local function.
;; * `eo` is `expand-object`: Converts a one line object literal to multiline.
;; * `co` is `contract-object`: Converts a multiline object literal to one line.
;; * `eu` is `expand-function`: Converts a one line function to multiline (expecting semicolons as statement delimiters).
;; * `cu` is `contract-function`: Converts a multiline function to one line (expecting semicolons as statement delimiters).
;; * `ec` is `expand-call-args`: Converts a one line function call args to multiline.
;; * `cc` is `contract-call-args`: Converts a multiline function call args to one line.
;; * `ea` is `expand-array`: Converts a one line array to multiline.
;; * `ca` is `contract-array`: Converts a multiline array to one line.
;; * `wi` is `wrap-buffer-in-iife`: Wraps the entire buffer in an immediately invoked function expression
;; * `ig` is `inject-global-in-iife`: Creates a shortcut for a marked global by injecting it in the wrapping immediately invoked function expression
;; * `ag` is `add-to-globals-annotation`: Creates a `/*global */` annotation if it is missing, and adds the var at point to it.
Expand Down Expand Up @@ -163,14 +157,8 @@

(defun js2r--add-keybindings (key-fn)
"Add js2r refactoring keybindings to `js2-mode-map' using KEY-FN to create each keybinding."
(define-key js2-refactor-mode-map (funcall key-fn "eo") #'js2r-expand-object)
(define-key js2-refactor-mode-map (funcall key-fn "co") #'js2r-contract-object)
(define-key js2-refactor-mode-map (funcall key-fn "eu") #'js2r-expand-function)
(define-key js2-refactor-mode-map (funcall key-fn "cu") #'js2r-contract-function)
(define-key js2-refactor-mode-map (funcall key-fn "ec") #'js2r-expand-call-args)
(define-key js2-refactor-mode-map (funcall key-fn "cc") #'js2r-contract-call-args)
(define-key js2-refactor-mode-map (funcall key-fn "ea") #'js2r-expand-array)
(define-key js2-refactor-mode-map (funcall key-fn "ca") #'js2r-contract-array)
(define-key js2-refactor-mode-map (funcall key-fn "ee") #'js2r-expand-node-at-point)
(define-key js2-refactor-mode-map (funcall key-fn "cc") #'js2r-contract-node-at-point)
(define-key js2-refactor-mode-map (funcall key-fn "wi") #'js2r-wrap-buffer-in-iife)
(define-key js2-refactor-mode-map (funcall key-fn "ig") #'js2r-inject-global-in-iife)
(define-key js2-refactor-mode-map (funcall key-fn "ev") #'js2r-extract-var)
Expand Down
Loading

0 comments on commit bd73f03

Please sign in to comment.