Skip to content

Commit bd73f03

Browse files
Merge pull request #79 from futurist/master
ADD: js2r-universal-expand-contract func to work with array,object,func
2 parents 1d15ffd + 186de64 commit bd73f03

File tree

5 files changed

+182
-44
lines changed

5 files changed

+182
-44
lines changed

‎README.md‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ A JavaScript refactoring library for emacs.
55
This is a collection of small refactoring functions to further the idea of a
66
JavaScript IDE in Emacs that started with js2-mode.
77

8+
## Change in 0.8.0
9+
10+
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).
11+
12+
Removed previous `ea` `ca` `eo` `co` `eu` `cu` `ec` `cc` key bindings.
13+
814
## Breaking change in 0.7.0
915

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

6975
## Refactorings
7076

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

107109
A list of some wanted improvements for the current refactorings.
108110

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

124129

125130
Thanks!

‎features/js2r-expand-collapse.feature‎

Lines changed: 110 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Feature: Expand and collapse things
44
When I insert "var a = { b: 1, c: 'def' };"
55
And I turn on js2-mode and js2-refactor-mode
66
And I go to the front of the word "b"
7-
And I press "C-c C-m eo"
7+
And I press "C-c C-m ee"
88
Then I should see:
99
"""
1010
var a = {
@@ -17,7 +17,7 @@ Feature: Expand and collapse things
1717
When I insert "var a = { b: 1, c: 'def, ghi' };"
1818
And I turn on js2-mode and js2-refactor-mode
1919
And I go to the front of the word "b"
20-
And I press "C-c C-m eo"
20+
And I press "C-c C-m ee"
2121
Then I should see:
2222
"""
2323
var a = {
@@ -36,7 +36,7 @@ Feature: Expand and collapse things
3636
"""
3737
And I turn on js2-mode and js2-refactor-mode
3838
And I go to the front of the word "b"
39-
And I press "C-c C-m co"
39+
And I press "C-c C-m cc"
4040
Then I should see "var a = { b: 1, c: 'def' };"
4141

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

5555
Scenario: Expanding functions
5656
When I insert "function f (a, b, c) { var t = a + b + c; return t; }"
5757
And I turn on js2-mode and js2-refactor-mode
5858
And I go to the front of the word "var"
59-
And I press "C-c C-m eu"
59+
And I press "C-c C-m ee"
6060
Then I should see:
6161
"""
6262
function f (a, b, c) {
@@ -69,7 +69,7 @@ Feature: Expand and collapse things
6969
When I insert "function f (a, b, c) { var t = a + b + c; var arr = [1, 2, 3, a, b]; return [t, arr]; }"
7070
And I turn on js2-mode and js2-refactor-mode
7171
And I go to the front of the word "var"
72-
And I press "C-c C-m eu"
72+
And I press "C-c C-m ee"
7373
Then I should see:
7474
"""
7575
function f (a, b, c) {
@@ -83,7 +83,7 @@ Feature: Expand and collapse things
8383
When I insert "function f (a, b, c) { var t = a + b + c; var o = {e1: a, e2: b + 1, e3: 'xyzzy'}; return o; }"
8484
And I turn on js2-mode and js2-refactor-mode
8585
And I go to the front of the word "var"
86-
And I press "C-c C-m eu"
86+
And I press "C-c C-m ee"
8787
Then I should see:
8888
"""
8989
function f (a, b, c) {
@@ -92,18 +92,18 @@ Feature: Expand and collapse things
9292
return o;
9393
}
9494
"""
95-
95+
9696
Scenario: Expanding arrow functions
9797
When I insert "var arrowFunc = (a, b, c) => { return a + b + c; }"
9898
And I turn on js2-mode and js2-refactor-mode
9999
And I go to the front of the word "return"
100-
And I press "C-c C-m eu"
100+
And I press "C-c C-m ee"
101101
Then I should see:
102102
"""
103103
var arrowFunc = (a, b, c) => {
104104
return a + b + c;
105105
}
106-
"""
106+
"""
107107

108108
Scenario: Contracting arrow functions
109109
When I insert:
@@ -114,7 +114,7 @@ Feature: Expand and collapse things
114114
"""
115115
And I turn on js2-mode and js2-refactor-mode
116116
And I go to the front of the word "return"
117-
And I press "C-c C-m cu"
117+
And I press "C-c C-m cc"
118118
Then I should see:
119119
"""
120120
var arrowFunc = (a, b, c) => { return a + b + c; }
@@ -130,7 +130,7 @@ Feature: Expand and collapse things
130130
"""
131131
And I turn on js2-mode and js2-refactor-mode
132132
And I go to the front of the word "var"
133-
And I press "C-c C-m cu"
133+
And I press "C-c C-m cc"
134134
Then I should see "function f (a, b, c) { var t = a + b + c; return t; }"
135135

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

150150
Scenario: Contracting functions containing object literals
@@ -158,7 +158,7 @@ Feature: Expand and collapse things
158158
"""
159159
And I turn on js2-mode and js2-refactor-mode
160160
And I go to the front of the word "var"
161-
And I press "C-c C-m cu"
161+
And I press "C-c C-m cc"
162162
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; }"
163163

164164
Scenario: Expanding function call arguments
@@ -168,7 +168,7 @@ Feature: Expand and collapse things
168168
"""
169169
And I turn on js2-mode and js2-refactor-mode
170170
And I go to the front of the word "overlay"
171-
And I press "C-c C-m ec"
171+
And I press "C-c C-m ee"
172172
Then I should see:
173173
"""
174174
m(
@@ -178,7 +178,7 @@ Feature: Expand and collapse things
178178
);
179179
"""
180180
And I go to the front of the word "tr"
181-
And I press "C-c C-m ec"
181+
And I press "C-c C-m ee"
182182
Then I should see:
183183
"""
184184
m(
@@ -248,7 +248,7 @@ Feature: Expand and collapse things
248248
When I insert "var a = [ b, 1, c, 3.1415927 ];"
249249
And I turn on js2-mode and js2-refactor-mode
250250
And I go to the front of the word "b"
251-
And I press "C-c C-m ea"
251+
And I press "C-c C-m ee"
252252
Then I should see:
253253
"""
254254
var a = [
@@ -271,7 +271,7 @@ Feature: Expand and collapse things
271271
"""
272272
And I turn on js2-mode and js2-refactor-mode
273273
And I go to the front of the word "b"
274-
And I press "C-c C-m ca"
274+
And I press "C-c C-m cc"
275275
Then I should see "var a = [ b, 1, c, 3.1415927 ];"
276276

277277
Scenario: Expanding arrays with comment
@@ -284,7 +284,7 @@ Feature: Expand and collapse things
284284
"""
285285
And I turn on js2-mode and js2-refactor-mode
286286
And I go to character "b"
287-
And I press "C-c C-m ea"
287+
And I press "C-c C-m ee"
288288
Then I should see:
289289
"""
290290
var a = [
@@ -293,3 +293,94 @@ Feature: Expand and collapse things
293293
4
294294
];
295295
"""
296+
297+
Scenario: Expanding and contracting node at point
298+
When I insert:
299+
"""
300+
var a = [1, 2, 3];
301+
var b = {c:4, d:5};
302+
function abc(x,y){x+=z; return x+y;}
303+
func(6,7);
304+
"""
305+
And I turn on js2-mode and js2-refactor-mode
306+
And I go to the front of the word "1"
307+
And I press "C-c C-m ee"
308+
Then I should see:
309+
"""
310+
var a = [
311+
1,
312+
2,
313+
3
314+
];
315+
var b = {c:4, d:5};
316+
function abc(x,y){x+=z; return x+y;}
317+
func(6,7);
318+
"""
319+
When I press "C-c C-m cc"
320+
Then I should see:
321+
"""
322+
var a = [ 1, 2, 3 ];
323+
var b = {c:4, d:5};
324+
function abc(x,y){x+=z; return x+y;}
325+
func(6,7);
326+
"""
327+
When I go to the front of the word "4"
328+
And I press "C-c C-m ee"
329+
Then I should see:
330+
"""
331+
var a = [ 1, 2, 3 ];
332+
var b = {
333+
c:4,
334+
d:5
335+
};
336+
function abc(x,y){x+=z; return x+y;}
337+
func(6,7);
338+
"""
339+
When I press "C-c C-m cc"
340+
Then I should see:
341+
"""
342+
var a = [ 1, 2, 3 ];
343+
var b = { c:4, d:5 };
344+
function abc(x,y){x+=z; return x+y;}
345+
func(6,7);
346+
"""
347+
When I go to the front of the word "z"
348+
And I press "C-c C-m ee"
349+
Then I should see:
350+
"""
351+
var a = [ 1, 2, 3 ];
352+
var b = { c:4, d:5 };
353+
function abc(x,y){
354+
x+=z;
355+
return x+y;
356+
}
357+
func(6,7);
358+
"""
359+
When I press "C-c C-m cc"
360+
Then I should see:
361+
"""
362+
var a = [ 1, 2, 3 ];
363+
var b = { c:4, d:5 };
364+
function abc(x,y){ x+=z; return x+y; }
365+
func(6,7);
366+
"""
367+
When I go to the front of the word "6"
368+
And I press "C-c C-m ee"
369+
Then I should see:
370+
"""
371+
var a = [ 1, 2, 3 ];
372+
var b = { c:4, d:5 };
373+
function abc(x,y){ x+=z; return x+y; }
374+
func(
375+
6,
376+
7
377+
);
378+
"""
379+
When I press "C-c C-m cc"
380+
Then I should see:
381+
"""
382+
var a = [ 1, 2, 3 ];
383+
var b = { c:4, d:5 };
384+
function abc(x,y){ x+=z; return x+y; }
385+
func( 6, 7 );
386+
"""

‎js2-refactor-pkg.el‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
(define-package "js2-refactor" "0.7.1"
1+
(define-package "js2-refactor" "0.8.0"
22
"A JavaScript refactoring library for emacs."
33
'((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")))

‎js2-refactor.el‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,12 @@
5151

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

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

164158
(defun js2r--add-keybindings (key-fn)
165159
"Add js2r refactoring keybindings to `js2-mode-map' using KEY-FN to create each keybinding."
166-
(define-key js2-refactor-mode-map (funcall key-fn "eo") #'js2r-expand-object)
167-
(define-key js2-refactor-mode-map (funcall key-fn "co") #'js2r-contract-object)
168-
(define-key js2-refactor-mode-map (funcall key-fn "eu") #'js2r-expand-function)
169-
(define-key js2-refactor-mode-map (funcall key-fn "cu") #'js2r-contract-function)
170-
(define-key js2-refactor-mode-map (funcall key-fn "ec") #'js2r-expand-call-args)
171-
(define-key js2-refactor-mode-map (funcall key-fn "cc") #'js2r-contract-call-args)
172-
(define-key js2-refactor-mode-map (funcall key-fn "ea") #'js2r-expand-array)
173-
(define-key js2-refactor-mode-map (funcall key-fn "ca") #'js2r-contract-array)
160+
(define-key js2-refactor-mode-map (funcall key-fn "ee") #'js2r-expand-node-at-point)
161+
(define-key js2-refactor-mode-map (funcall key-fn "cc") #'js2r-contract-node-at-point)
174162
(define-key js2-refactor-mode-map (funcall key-fn "wi") #'js2r-wrap-buffer-in-iife)
175163
(define-key js2-refactor-mode-map (funcall key-fn "ig") #'js2r-inject-global-in-iife)
176164
(define-key js2-refactor-mode-map (funcall key-fn "ev") #'js2r-extract-var)

0 commit comments

Comments
 (0)