Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit fa00beb

Browse files
niteskumboopeshmahendran
authored andcommitted
Fixed Getter Setter Redo Issue (#14508)
* Fixed Getter Setter Redo Issue * Addressed Review Comments
1 parent 0cee7d4 commit fa00beb

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/extensions/default/JavaScriptRefactoring/WrapSelection.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ define(function (require, exports, module) {
246246
}
247247

248248
var token = TokenUtils.getTokenAt(current.cm, current.cm.posFromIndex(endIndex)),
249+
commaString = ",",
249250
isLastNode,
250251
templateParams,
251252
parentNode,
@@ -267,16 +268,38 @@ define(function (require, exports, module) {
267268

268269
var propertyNodeArray = parentNode.properties;
269270
// Find the last Propery Node before endIndex
270-
var properyEndNode = propertyNodeArray.find(function (element) {
271+
var properyNodeIndex = propertyNodeArray.findIndex(function (element) {
271272
return (endIndex >= element.start && endIndex < element.end);
272273
});
273274

275+
var propertyNode = propertyNodeArray[properyNodeIndex];
276+
274277
//Get Current Selected Property End Index;
275-
propertyEndPos = editor.posFromIndex(properyEndNode.end);
278+
propertyEndPos = editor.posFromIndex(propertyNode.end);
276279

277280

278281
//We have to add ',' so we need to find position of current property selected
279282
isLastNode = current.isLastNodeInScope(current.ast, endIndex);
283+
var nextPropertNode, nextPropertyStartPos;
284+
if(!isLastNode && properyNodeIndex + 1 <= propertyNodeArray.length - 1) {
285+
nextPropertNode = propertyNodeArray[properyNodeIndex + 1];
286+
nextPropertyStartPos = editor.posFromIndex(nextPropertNode.start);
287+
288+
if(propertyEndPos.line !== nextPropertyStartPos.line) {
289+
propertyEndPos = current.lineEndPosition(current.startPos.line);
290+
} else {
291+
propertyEndPos = nextPropertyStartPos;
292+
commaString = ", ";
293+
}
294+
}
295+
296+
var getSetPos;
297+
if (isLastNode) {
298+
getSetPos = current.document.adjustPosForChange(propertyEndPos, commaString.split("\n"),
299+
propertyEndPos, propertyEndPos);
300+
} else {
301+
getSetPos = propertyEndPos;
302+
}
280303
templateParams = {
281304
"getName": token.string,
282305
"setName": token.string,
@@ -288,18 +311,17 @@ define(function (require, exports, module) {
288311
current.document.batchOperation(function() {
289312
if (isLastNode) {
290313
//Add ',' in the end of current line
291-
current.document.replaceRange(",", propertyEndPos, propertyEndPos);
314+
current.document.replaceRange(commaString, propertyEndPos, propertyEndPos);
292315
}
293-
propertyEndPos.ch++;
294316

295-
current.editor.setSelection(propertyEndPos); //Selection on line end
317+
current.editor.setSelection(getSetPos); //Selection on line end
296318

297319
// Add getters and setters for given token using template at current cursor position
298320
current.replaceTextFromTemplate(GETTERS_SETTERS, templateParams);
299321

300322
if (!isLastNode) {
301323
// Add ',' at the end setter
302-
current.document.replaceRange(",", current.editor.getSelection().start, current.editor.getSelection().start);
324+
current.document.replaceRange(commaString, current.editor.getSelection().start, current.editor.getSelection().start);
303325
}
304326
});
305327
}

0 commit comments

Comments
 (0)