function handleExtractToFunction() {
        var editor = EditorManager.getActiveEditor();
        var result = new $.Deferred(); // used only for testing purpose

        if (editor.getSelections().length > 1) {
            editor.displayErrorMessageAtCursor(Strings.ERROR_EXTRACTTO_FUNCTION_MULTICURSORS);
            result.resolve(Strings.ERROR_EXTRACTTO_FUNCTION_MULTICURSORS);
            return;
        }
        initializeSession(editor);

        var selection = editor.getSelection(),
            doc       = editor.document,
            retObj    = RefactoringUtils.normalizeText(editor.getSelectedText(), editor.indexFromPos(selection.start), editor.indexFromPos(selection.end)),
            text      = retObj.text,
            start     = retObj.start,
            end       = retObj.end,
            ast,
            scopes,
            expns,
            inlineMenu;

        RefactoringUtils.getScopeData(session, editor.posFromIndex(start)).done(function(scope) {
            ast = RefactoringUtils.getAST(doc.getText());

            var isExpression = false;
            if (!RefactoringUtils.checkStatement(ast, start, end, doc.getText())) {
                isExpression = RefactoringUtils.getExpression(ast, start, end, doc.getText());
                if (!isExpression) {
                    editor.displayErrorMessageAtCursor(Strings.ERROR_EXTRACTTO_FUNCTION_NOT_VALID);
                    result.resolve(Strings.ERROR_EXTRACTTO_FUNCTION_NOT_VALID);
                    return;
                }
            }
            scopes = RefactoringUtils.getAllScopes(ast, scope, doc.getText());

            // if only one scope, extract without menu
            if (scopes.length === 1) {
                extract(ast, text, scopes, scopes[0], scopes[0], start, end, isExpression);
                result.resolve();
                return;
            }

            inlineMenu = new InlineMenu(editor, Strings.EXTRACTTO_FUNCTION_SELECT_SCOPE);

            inlineMenu.open(scopes.filter(RefactoringUtils.isFnScope));

            result.resolve(inlineMenu);

            inlineMenu.onSelect(function (scopeId) {
                extract(ast, text, scopes, scopes[0], scopes[scopeId], start, end, isExpression);
                inlineMenu.close();
            });

            inlineMenu.onClose(function(){
                inlineMenu.close();
            });
        }).fail(function() {
            editor.displayErrorMessageAtCursor(Strings.ERROR_TERN_FAILED);
            result.resolve(Strings.ERROR_TERN_FAILED);
        });

        return result.promise();
    }
function handleExtractToFunction() {
        var editor = EditorManager.getActiveEditor();
        var result = new $.Deferred(); // used only for testing purpose

        if (editor.getSelections().length > 1) {
            editor.displayErrorMessageAtCursor(Strings.ERROR_EXTRACTTO_FUNCTION_MULTICURSORS);
            result.resolve(Strings.ERROR_EXTRACTTO_FUNCTION_MULTICURSORS);
            return;
        }
        initializeSession(editor);

        var selection = editor.getSelection(),
            doc       = editor.document,
            retObj    = RefactoringUtils.normalizeText(editor.getSelectedText(), editor.indexFromPos(selection.start), editor.indexFromPos(selection.end)),
            text      = retObj.text,
            start     = retObj.start,
            end       = retObj.end,
            ast,
            scopes,
            expns,
            inlineMenu;

        RefactoringUtils.getScopeData(session, editor.posFromIndex(start)).done(function(scope) {
            ast = RefactoringUtils.getAST(doc.getText());

            var isExpression = false;
            if (!RefactoringUtils.checkStatement(ast, start, end, doc.getText())) {
                isExpression = RefactoringUtils.getExpression(ast, start, end, doc.getText());
                if (!isExpression) {
                    editor.displayErrorMessageAtCursor(Strings.ERROR_EXTRACTTO_FUNCTION_NOT_VALID);
                    result.resolve(Strings.ERROR_EXTRACTTO_FUNCTION_NOT_VALID);
                    return;
                }
            }
            scopes = RefactoringUtils.getAllScopes(ast, scope, doc.getText());

            // if only one scope, extract without menu
            if (scopes.length === 1) {
                extract(ast, text, scopes, scopes[0], scopes[0], start, end, isExpression);
                result.resolve();
                return;
            }

            inlineMenu = new InlineMenu(editor, Strings.EXTRACTTO_FUNCTION_SELECT_SCOPE);

            inlineMenu.open(scopes.filter(RefactoringUtils.isFnScope));

            result.resolve(inlineMenu);

            inlineMenu.onSelect(function (scopeId) {
                extract(ast, text, scopes, scopes[0], scopes[scopeId], start, end, isExpression);
                inlineMenu.close();
            });

            inlineMenu.onClose(function(){
                inlineMenu.close();
            });
        }).fail(function() {
            editor.displayErrorMessageAtCursor(Strings.ERROR_TERN_FAILED);
            result.resolve(Strings.ERROR_TERN_FAILED);
        });

        return result.promise();
    }
function handleExtractToFunction ( ) { var editor = EditorManager . getActiveEditor ( ) ; var result = new $ . Deferred ( ) ; // used only for testing purpose if ( editor . getSelections ( ) . length > 1 ) { editor . displayErrorMessageAtCursor ( Strings . ERROR_EXTRACTTO_FUNCTION_MULTICURSORS ) ; result . resolve ( Strings . ERROR_EXTRACTTO_FUNCTION_MULTICURSORS ) ; return ; } initializeSession ( editor ) ; var selection = editor . getSelection ( ) , doc = editor . document , retObj = RefactoringUtils . normalizeText ( editor . getSelectedText ( ) , editor . indexFromPos ( selection . start ) , editor . indexFromPos ( selection . end ) ) , text = retObj . text , start = retObj . start , end = retObj . end , ast , scopes , expns , inlineMenu ; RefactoringUtils . getScopeData ( session , editor . posFromIndex ( start ) ) . done ( function ( scope ) { ast = RefactoringUtils . getAST ( doc . getText ( ) ) ; var isExpression = false ; if ( ! RefactoringUtils . checkStatement ( ast , start , end , doc . getText ( ) ) ) { isExpression = RefactoringUtils . getExpression ( ast , start , end , doc . getText ( ) ) ; if ( ! isExpression ) { editor . displayErrorMessageAtCursor ( Strings . ERROR_EXTRACTTO_FUNCTION_NOT_VALID ) ; result . resolve ( Strings . ERROR_EXTRACTTO_FUNCTION_NOT_VALID ) ; return ; } } scopes = RefactoringUtils . getAllScopes ( ast , scope , doc . getText ( ) ) ; // if only one scope, extract without menu if ( scopes . length === 1 ) { extract ( ast , text , scopes , scopes [ 0 ] , scopes [ 0 ] , start , end , isExpression ) ; result . resolve ( ) ; return ; } inlineMenu = new InlineMenu ( editor , Strings . EXTRACTTO_FUNCTION_SELECT_SCOPE ) ; inlineMenu . open ( scopes . filter ( RefactoringUtils . isFnScope ) ) ; result . resolve ( inlineMenu ) ; inlineMenu . onSelect ( function ( scopeId ) { extract ( ast , text , scopes , scopes [ 0 ] , scopes [ scopeId ] , start , end , isExpression ) ; inlineMenu . close ( ) ; } ) ; inlineMenu . onClose ( function ( ) { inlineMenu . close ( ) ; } ) ; } ) . fail ( function ( ) { editor . displayErrorMessageAtCursor ( Strings . ERROR_TERN_FAILED ) ; result . resolve ( Strings . ERROR_TERN_FAILED ) ; } ) ; return result . promise ( ) ; }
Main function that handles extract to function
Main function that handles extract to function
handleExtractToFunction - Extracts to function