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

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

        if (selectedText.length === 0) {
            var statementNode = RefactoringUtils.findSurroundASTNode(current.ast, {start: startIndex}, ["Statement"]);
            if (!statementNode) {
                current.editor.displayErrorMessageAtCursor(err);
                return;
            }
            selectedText = current.text.substr(statementNode.start, statementNode.end - statementNode.start);
            startIndex = statementNode.start;
            endIndex = statementNode.end;
        } else {
            var selectionDetails = RefactoringUtils.normalizeText(selectedText, startIndex, endIndex);
            selectedText = selectionDetails.text;
            startIndex = selectionDetails.start;
            endIndex = selectionDetails.end;
        }

        if (!RefactoringUtils.checkStatement(current.ast, startIndex, endIndex, selectedText)) {
            current.editor.displayErrorMessageAtCursor(err);
            return;
        }

        pos = {
            "start": current.cm.posFromIndex(startIndex),
            "end": current.cm.posFromIndex(endIndex)
        };

        current.document.batchOperation(function() {
            current.replaceTextFromTemplate(wrapperName, {body: selectedText}, pos);
        });

        if (wrapperName === TRY_CATCH) {
            var cursorLine = current.editor.getSelection().end.line - 1,
                startCursorCh = current.document.getLine(cursorLine).indexOf("\/\/"),
                endCursorCh = current.document.getLine(cursorLine).length;

            current.editor.setSelection({"line": cursorLine, "ch": startCursorCh}, {"line": cursorLine, "ch": endCursorCh});
        } else if (wrapperName === WRAP_IN_CONDITION) {
            current.editor.setSelection({"line": pos.start.line, "ch": pos.start.ch + 4}, {"line": pos.start.line, "ch": pos.start.ch + 13});
        }
    }
function _wrapSelectedStatements (wrapperName, err) {
        var editor = EditorManager.getActiveEditor();
        if (!editor) {
            return;
        }
        initializeRefactoringSession(editor);

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

        if (selectedText.length === 0) {
            var statementNode = RefactoringUtils.findSurroundASTNode(current.ast, {start: startIndex}, ["Statement"]);
            if (!statementNode) {
                current.editor.displayErrorMessageAtCursor(err);
                return;
            }
            selectedText = current.text.substr(statementNode.start, statementNode.end - statementNode.start);
            startIndex = statementNode.start;
            endIndex = statementNode.end;
        } else {
            var selectionDetails = RefactoringUtils.normalizeText(selectedText, startIndex, endIndex);
            selectedText = selectionDetails.text;
            startIndex = selectionDetails.start;
            endIndex = selectionDetails.end;
        }

        if (!RefactoringUtils.checkStatement(current.ast, startIndex, endIndex, selectedText)) {
            current.editor.displayErrorMessageAtCursor(err);
            return;
        }

        pos = {
            "start": current.cm.posFromIndex(startIndex),
            "end": current.cm.posFromIndex(endIndex)
        };

        current.document.batchOperation(function() {
            current.replaceTextFromTemplate(wrapperName, {body: selectedText}, pos);
        });

        if (wrapperName === TRY_CATCH) {
            var cursorLine = current.editor.getSelection().end.line - 1,
                startCursorCh = current.document.getLine(cursorLine).indexOf("\/\/"),
                endCursorCh = current.document.getLine(cursorLine).length;

            current.editor.setSelection({"line": cursorLine, "ch": startCursorCh}, {"line": cursorLine, "ch": endCursorCh});
        } else if (wrapperName === WRAP_IN_CONDITION) {
            current.editor.setSelection({"line": pos.start.line, "ch": pos.start.ch + 4}, {"line": pos.start.line, "ch": pos.start.ch + 13});
        }
    }
function _wrapSelectedStatements ( wrapperName , err ) { var editor = EditorManager . getActiveEditor ( ) ; if ( ! editor ) { return ; } initializeRefactoringSession ( editor ) ; var startIndex = current . startIndex , endIndex = current . endIndex , selectedText = current . selectedText , pos ; if ( selectedText . length === 0 ) { var statementNode = RefactoringUtils . findSurroundASTNode ( current . ast , { start : startIndex } , [ "Statement" ] ) ; if ( ! statementNode ) { current . editor . displayErrorMessageAtCursor ( err ) ; return ; } selectedText = current . text . substr ( statementNode . start , statementNode . end - statementNode . start ) ; startIndex = statementNode . start ; endIndex = statementNode . end ; } else { var selectionDetails = RefactoringUtils . normalizeText ( selectedText , startIndex , endIndex ) ; selectedText = selectionDetails . text ; startIndex = selectionDetails . start ; endIndex = selectionDetails . end ; } if ( ! RefactoringUtils . checkStatement ( current . ast , startIndex , endIndex , selectedText ) ) { current . editor . displayErrorMessageAtCursor ( err ) ; return ; } pos = { "start" : current . cm . posFromIndex ( startIndex ) , "end" : current . cm . posFromIndex ( endIndex ) } ; current . document . batchOperation ( function ( ) { current . replaceTextFromTemplate ( wrapperName , { body : selectedText } , pos ) ; } ) ; if ( wrapperName === TRY_CATCH ) { var cursorLine = current . editor . getSelection ( ) . end . line - 1 , startCursorCh = current . document . getLine ( cursorLine ) . indexOf ( "\/\/" ) , endCursorCh = current . document . getLine ( cursorLine ) . length ; current . editor . setSelection ( { "line" : cursorLine , "ch" : startCursorCh } , { "line" : cursorLine , "ch" : endCursorCh } ) ; } else if ( wrapperName === WRAP_IN_CONDITION ) { current . editor . setSelection ( { "line" : pos . start . line , "ch" : pos . start . ch + 4 } , { "line" : pos . start . line , "ch" : pos . start . ch + 13 } ) ; } }
Wrap selected statements

@param {string} wrapperName - template name where we want wrap selected statements
@param {string} err- error message if we can't wrap selected code
Wrap selected statements