@@ -37,19 +37,24 @@ define(function(require, exports, module) {
37
37
* Does the actual extraction. i.e Replacing the text, Creating a variable
38
38
* and multi select variable names
39
39
*/
40
- function extract ( scopes , parentStatement , expns , text ) {
40
+ function extract ( scopes , parentStatement , expns , text , insertPosition ) {
41
41
var varType = "var" ,
42
42
varName = RefactoringUtils . getUniqueIdentifierName ( scopes , "extracted" ) ,
43
43
varDeclaration = varType + " " + varName + " = " + text + ";\n" ,
44
- insertStartPos = session . editor . posFromIndex ( parentStatement . start ) ,
44
+ parentStatementStartPos = session . editor . posFromIndex ( parentStatement . start ) ,
45
+ insertStartPos = insertPosition || parentStatementStartPos ,
45
46
selections = [ ] ,
46
47
doc = session . editor . document ,
47
48
replaceExpnIndex = 0 ,
48
- posToIndent ;
49
+ posToIndent ,
50
+ edits = [ ] ;
49
51
50
52
// If parent statement is expression statement, then just append var declaration
51
53
// Ex: "add(1, 2)" will become "var extracted = add(1, 2)"
52
- if ( parentStatement . type === "ExpressionStatement" && RefactoringUtils . isEqual ( parentStatement . expression , expns [ 0 ] ) ) {
54
+ if ( parentStatement . type === "ExpressionStatement" &&
55
+ RefactoringUtils . isEqual ( parentStatement . expression , expns [ 0 ] ) &&
56
+ insertStartPos . line === parentStatementStartPos . line &&
57
+ insertStartPos . ch === parentStatementStartPos . ch ) {
53
58
varDeclaration = varType + " " + varName + " = " ;
54
59
replaceExpnIndex = 1 ;
55
60
}
@@ -63,25 +68,29 @@ define(function(require, exports, module) {
63
68
expns [ i ] . start = doc . adjustPosForChange ( expns [ i ] . start , varDeclaration . split ( "\n" ) , insertStartPos , insertStartPos ) ;
64
69
expns [ i ] . end = doc . adjustPosForChange ( expns [ i ] . end , varDeclaration . split ( "\n" ) , insertStartPos , insertStartPos ) ;
65
70
66
- selections . push ( {
67
- start : expns [ i ] . start ,
68
- end : { line : expns [ i ] . start . line , ch : expns [ i ] . start . ch + varName . length }
71
+ edits . push ( {
72
+ edit : {
73
+ text : varName ,
74
+ start : expns [ i ] . start ,
75
+ end : expns [ i ] . end
76
+ } ,
77
+ selection : {
78
+ start : expns [ i ] . start ,
79
+ end : { line : expns [ i ] . start . line , ch : expns [ i ] . start . ch + varName . length }
80
+ }
69
81
} ) ;
70
82
}
71
83
72
84
// Replace and multi-select
73
85
doc . batchOperation ( function ( ) {
74
86
doc . replaceRange ( varDeclaration , insertStartPos ) ;
75
87
76
- for ( var i = replaceExpnIndex ; i < expns . length ; ++ i ) {
77
- doc . replaceRange ( varName , expns [ i ] . start , expns [ i ] . end ) ;
78
- }
88
+ selections = doc . doMultipleEdits ( edits ) ;
79
89
selections . push ( {
80
90
start : { line : insertStartPos . line , ch : insertStartPos . ch + varType . length + 1 } ,
81
91
end : { line : insertStartPos . line , ch : insertStartPos . ch + varType . length + varName . length + 1 } ,
82
92
primary : true
83
93
} ) ;
84
-
85
94
session . editor . setSelections ( selections ) ;
86
95
session . editor . _codeMirror . indentLine ( posToIndent . line , "smart" ) ;
87
96
} ) ;
@@ -163,6 +172,7 @@ define(function(require, exports, module) {
163
172
*/
164
173
function extractToVariable ( ast , start , end , text , scopes ) {
165
174
var doc = session . editor . document ,
175
+ editor = EditorManager . getActiveEditor ( ) ,
166
176
parentExpn = RefactoringUtils . getExpression ( ast , start , end , doc . getText ( ) ) ,
167
177
expns = [ ] ,
168
178
parentBlockStatement ,
@@ -178,8 +188,26 @@ define(function(require, exports, module) {
178
188
if ( doc . getText ( ) . substr ( parentExpn . start , parentExpn . end - parentExpn . start ) === text ) {
179
189
parentBlockStatement = RefactoringUtils . findSurroundASTNode ( ast , parentExpn , [ "BlockStatement" , "Program" ] ) ;
180
190
expns = findAllExpressions ( parentBlockStatement , parentExpn , text ) ;
181
- parentStatement = RefactoringUtils . findSurroundASTNode ( ast , expns [ 0 ] , [ "Statement" ] ) ;
182
- extract ( scopes , parentStatement , expns , text ) ;
191
+
192
+ RefactoringUtils . getScopeData ( session , editor . posFromIndex ( expns [ 0 ] . start ) ) . done ( function ( scope ) {
193
+ var firstExpnsScopes = RefactoringUtils . getAllScopes ( ast , scope , doc . getText ( ) ) ,
194
+ insertPostion ;
195
+ parentStatement = RefactoringUtils . findSurroundASTNode ( ast , expns [ 0 ] , [ "Statement" ] ) ;
196
+ if ( scopes . length < firstExpnsScopes . length ) {
197
+ var parentScope ;
198
+ if ( expns [ 0 ] . body && expns [ 0 ] . body . type === "BlockStatement" ) {
199
+ parentScope = firstExpnsScopes [ firstExpnsScopes . length - scopes . length ] ;
200
+ } else {
201
+ parentScope = firstExpnsScopes [ firstExpnsScopes . length - scopes . length - 1 ] ;
202
+ }
203
+
204
+ var insertNode = RefactoringUtils . findSurroundASTNode ( ast , parentScope . originNode , [ "Statement" ] ) ;
205
+ if ( insertNode ) {
206
+ insertPostion = session . editor . posFromIndex ( insertNode . start ) ;
207
+ }
208
+ }
209
+ extract ( scopes , parentStatement , expns , text , insertPostion ) ;
210
+ } ) ;
183
211
} else {
184
212
parentStatement = RefactoringUtils . findSurroundASTNode ( ast , parentExpn , [ "Statement" ] ) ;
185
213
extract ( scopes , parentStatement , [ { start : start , end : end } ] , text ) ;
@@ -212,6 +240,16 @@ define(function(require, exports, module) {
212
240
expns ,
213
241
inlineMenu ;
214
242
243
+ function callExtractToVariable ( startPos , endPos , value ) {
244
+ RefactoringUtils . getScopeData ( session , editor . posFromIndex ( startPos ) )
245
+ . done ( function ( expnscope ) {
246
+ scopes = RefactoringUtils . getAllScopes ( ast , expnscope , doc . getText ( ) ) ;
247
+ extractToVariable ( ast , startPos , endPos , value , scopes ) ;
248
+ } ) . fail ( function ( ) {
249
+ editor . displayErrorMessageAtCursor ( Strings . ERROR_TERN_FAILED ) ;
250
+ } ) ;
251
+ }
252
+
215
253
RefactoringUtils . getScopeData ( session , editor . posFromIndex ( start ) ) . done ( function ( scope ) {
216
254
ast = RefactoringUtils . getAST ( doc . getText ( ) ) ;
217
255
scopes = RefactoringUtils . getAllScopes ( ast , scope , doc . getText ( ) ) ;
@@ -253,7 +291,7 @@ define(function(require, exports, module) {
253
291
254
292
// If only one surround expression, extract
255
293
if ( expns . length === 1 ) {
256
- extractToVariable ( ast , expns [ 0 ] . start , expns [ 0 ] . end , expns [ 0 ] . value , scopes ) ;
294
+ callExtractToVariable ( expns [ 0 ] . start , expns [ 0 ] . end , expns [ 0 ] . value ) ;
257
295
return ;
258
296
}
259
297
@@ -271,7 +309,7 @@ define(function(require, exports, module) {
271
309
inlineMenu . open ( expns ) ;
272
310
273
311
inlineMenu . onSelect ( function ( expnId ) {
274
- extractToVariable ( ast , expns [ expnId ] . start , expns [ expnId ] . end , expns [ expnId ] . value , scopes ) ;
312
+ callExtractToVariable ( expns [ expnId ] . start , expns [ expnId ] . end , expns [ expnId ] . value ) ;
275
313
inlineMenu . close ( ) ;
276
314
} ) ;
277
315
0 commit comments