src/extensions/default/JavaScriptRefactoring/WrapSelection.js
function createGettersAndSetters() {
        var editor = EditorManager.getActiveEditor();
        if (!editor) {
            return;
        }
        initializeRefactoringSession(editor);

        var startIndex = current.startIndex,
            endIndex = current.endIndex,
            selectedText = current.selectedText;

        if (selectedText.length >= 1) {
            var selectionDetails = RefactoringUtils.normalizeText(selectedText, startIndex, endIndex);
            selectedText = selectionDetails.text;
            startIndex = selectionDetails.start;
            endIndex = selectionDetails.end;
        }

        var token = TokenUtils.getTokenAt(current.cm, current.cm.posFromIndex(endIndex)),
            commaString = ",",
            isLastNode,
            templateParams,
            parentNode,
            propertyEndPos;

        //Create getters and setters only if selected reference is a property
        if (token.type !== "property") {
            current.editor.displayErrorMessageAtCursor(Strings.ERROR_GETTERS_SETTERS);
            return;
        }

        parentNode = current.getParentNode(current.ast, endIndex);
        // Check if selected propery is child of a object expression
        if (!parentNode || !parentNode.properties) {
            current.editor.displayErrorMessageAtCursor(Strings.ERROR_GETTERS_SETTERS);
            return;
        }


        var propertyNodeArray = parentNode.properties;
        // Find the last Propery Node before endIndex
        var properyNodeIndex = propertyNodeArray.findIndex(function (element) {
            return (endIndex >= element.start && endIndex < element.end);
        });

        var propertyNode = propertyNodeArray[properyNodeIndex];

        //Get Current Selected Property End Index;
        propertyEndPos = editor.posFromIndex(propertyNode.end);


        //We have to add ',' so we need to find position of current property selected
        isLastNode = current.isLastNodeInScope(current.ast, endIndex);
        var nextPropertNode, nextPropertyStartPos;
        if(!isLastNode && properyNodeIndex + 1 <= propertyNodeArray.length - 1) {
            nextPropertNode = propertyNodeArray[properyNodeIndex + 1];
            nextPropertyStartPos = editor.posFromIndex(nextPropertNode.start);

            if(propertyEndPos.line !== nextPropertyStartPos.line) {
                propertyEndPos = current.lineEndPosition(current.startPos.line);
            } else {
                propertyEndPos = nextPropertyStartPos;
                commaString = ", ";
            }
        }

        var getSetPos;
        if (isLastNode) {
            getSetPos = current.document.adjustPosForChange(propertyEndPos, commaString.split("\n"),
                                                            propertyEndPos, propertyEndPos);
        } else {
            getSetPos = propertyEndPos;
        }
        templateParams = {
            "getName": token.string,
            "setName": token.string,
            "tokenName": token.string
        };

        // Replace, setSelection, IndentLine
        // We need to call batchOperation as indentLine don't have option to add origin as like replaceRange
        current.document.batchOperation(function() {
            if (isLastNode) {
                //Add ',' in the end of current line
                current.document.replaceRange(commaString, propertyEndPos, propertyEndPos);
            }

            current.editor.setSelection(getSetPos); //Selection on line end

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

            if (!isLastNode) {
                // Add ',' at the end setter
                current.document.replaceRange(commaString, current.editor.getSelection().start, current.editor.getSelection().start);
            }
        });
    }
function createGettersAndSetters() {
        var editor = EditorManager.getActiveEditor();
        if (!editor) {
            return;
        }
        initializeRefactoringSession(editor);

        var startIndex = current.startIndex,
            endIndex = current.endIndex,
            selectedText = current.selectedText;

        if (selectedText.length >= 1) {
            var selectionDetails = RefactoringUtils.normalizeText(selectedText, startIndex, endIndex);
            selectedText = selectionDetails.text;
            startIndex = selectionDetails.start;
            endIndex = selectionDetails.end;
        }

        var token = TokenUtils.getTokenAt(current.cm, current.cm.posFromIndex(endIndex)),
            commaString = ",",
            isLastNode,
            templateParams,
            parentNode,
            propertyEndPos;

        //Create getters and setters only if selected reference is a property
        if (token.type !== "property") {
            current.editor.displayErrorMessageAtCursor(Strings.ERROR_GETTERS_SETTERS);
            return;
        }

        parentNode = current.getParentNode(current.ast, endIndex);
        // Check if selected propery is child of a object expression
        if (!parentNode || !parentNode.properties) {
            current.editor.displayErrorMessageAtCursor(Strings.ERROR_GETTERS_SETTERS);
            return;
        }


        var propertyNodeArray = parentNode.properties;
        // Find the last Propery Node before endIndex
        var properyNodeIndex = propertyNodeArray.findIndex(function (element) {
            return (endIndex >= element.start && endIndex < element.end);
        });

        var propertyNode = propertyNodeArray[properyNodeIndex];

        //Get Current Selected Property End Index;
        propertyEndPos = editor.posFromIndex(propertyNode.end);


        //We have to add ',' so we need to find position of current property selected
        isLastNode = current.isLastNodeInScope(current.ast, endIndex);
        var nextPropertNode, nextPropertyStartPos;
        if(!isLastNode && properyNodeIndex + 1 <= propertyNodeArray.length - 1) {
            nextPropertNode = propertyNodeArray[properyNodeIndex + 1];
            nextPropertyStartPos = editor.posFromIndex(nextPropertNode.start);

            if(propertyEndPos.line !== nextPropertyStartPos.line) {
                propertyEndPos = current.lineEndPosition(current.startPos.line);
            } else {
                propertyEndPos = nextPropertyStartPos;
                commaString = ", ";
            }
        }

        var getSetPos;
        if (isLastNode) {
            getSetPos = current.document.adjustPosForChange(propertyEndPos, commaString.split("\n"),
                                                            propertyEndPos, propertyEndPos);
        } else {
            getSetPos = propertyEndPos;
        }
        templateParams = {
            "getName": token.string,
            "setName": token.string,
            "tokenName": token.string
        };

        // Replace, setSelection, IndentLine
        // We need to call batchOperation as indentLine don't have option to add origin as like replaceRange
        current.document.batchOperation(function() {
            if (isLastNode) {
                //Add ',' in the end of current line
                current.document.replaceRange(commaString, propertyEndPos, propertyEndPos);
            }

            current.editor.setSelection(getSetPos); //Selection on line end

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

            if (!isLastNode) {
                // Add ',' at the end setter
                current.document.replaceRange(commaString, current.editor.getSelection().start, current.editor.getSelection().start);
            }
        });
    }
function createGettersAndSetters ( ) { var editor = EditorManager . getActiveEditor ( ) ; if ( ! editor ) { return ; } initializeRefactoringSession ( editor ) ; var startIndex = current . startIndex , endIndex = current . endIndex , selectedText = current . selectedText ; if ( selectedText . length >= 1 ) { var selectionDetails = RefactoringUtils . normalizeText ( selectedText , startIndex , endIndex ) ; selectedText = selectionDetails . text ; startIndex = selectionDetails . start ; endIndex = selectionDetails . end ; } var token = TokenUtils . getTokenAt ( current . cm , current . cm . posFromIndex ( endIndex ) ) , commaString = "," , isLastNode , templateParams , parentNode , propertyEndPos ; //Create getters and setters only if selected reference is a property if ( token . type !== "property" ) { current . editor . displayErrorMessageAtCursor ( Strings . ERROR_GETTERS_SETTERS ) ; return ; } parentNode = current . getParentNode ( current . ast , endIndex ) ; // Check if selected propery is child of a object expression if ( ! parentNode || ! parentNode . properties ) { current . editor . displayErrorMessageAtCursor ( Strings . ERROR_GETTERS_SETTERS ) ; return ; } var propertyNodeArray = parentNode . properties ; // Find the last Propery Node before endIndex var properyNodeIndex = propertyNodeArray . findIndex ( function ( element ) { return ( endIndex >= element . start && endIndex < element . end ) ; } ) ; var propertyNode = propertyNodeArray [ properyNodeIndex ] ; //Get Current Selected Property End Index; propertyEndPos = editor . posFromIndex ( propertyNode . end ) ; //We have to add ',' so we need to find position of current property selected isLastNode = current . isLastNodeInScope ( current . ast , endIndex ) ; var nextPropertNode , nextPropertyStartPos ; if ( ! isLastNode && properyNodeIndex + 1 <= propertyNodeArray . length - 1 ) { nextPropertNode = propertyNodeArray [ properyNodeIndex + 1 ] ; nextPropertyStartPos = editor . posFromIndex ( nextPropertNode . start ) ; if ( propertyEndPos . line !== nextPropertyStartPos . line ) { propertyEndPos = current . lineEndPosition ( current . startPos . line ) ; } else { propertyEndPos = nextPropertyStartPos ; commaString = ", " ; } } var getSetPos ; if ( isLastNode ) { getSetPos = current . document . adjustPosForChange ( propertyEndPos , commaString . split ( "\n" ) , propertyEndPos , propertyEndPos ) ; } else { getSetPos = propertyEndPos ; } templateParams = { "getName" : token . string , "setName" : token . string , "tokenName" : token . string } ; // Replace, setSelection, IndentLine // We need to call batchOperation as indentLine don't have option to add origin as like replaceRange current . document . batchOperation ( function ( ) { if ( isLastNode ) { //Add ',' in the end of current line current . document . replaceRange ( commaString , propertyEndPos , propertyEndPos ) ; } current . editor . setSelection ( getSetPos ) ; //Selection on line end // Add getters and setters for given token using template at current cursor position current . replaceTextFromTemplate ( GETTERS_SETTERS , templateParams ) ; if ( ! isLastNode ) { // Add ',' at the end setter current . document . replaceRange ( commaString , current . editor . getSelection ( ) . start , current . editor . getSelection ( ) . start ) ; } } ) ; }
Create gtteres and setters for a property
Create gtteres and setters for a property