angular-ui / ui-ace

This directive allows you to add ACE editor elements.
http://angular-ui.github.io/ui-ace
MIT License
578 stars 172 forks source link

Is it possible to format code? #114

Closed captainkovalsky closed 9 years ago

captainkovalsky commented 9 years ago

I've included ace and it works fine. I want to reformat code manually, has ui-ace this possibility?

I've found this soulution http://stackoverflow.com/a/11101769/1115430 But it doesn't work, here is used code

 $scope.aceLoaded = function(_editor) {
     var _session = _editor.getSession();
     var _renderer = _editor.renderer;

 $scope.formatCode = function() {
     var code = _editor.getSession().getValue();
     _editor.getSession().setValue(code);
 };
 };
captainkovalsky commented 9 years ago

I've found workaround

        $scope.formatCode = function() {
            try {
                var val = _editor.session.getValue();
                var o = JSON.parse(val);
                val = JSON.stringify(o, null, '\t');
                _editor.session.setValue(val);
            } catch (e) {
                console.log('something wrong with your json.');
                return; //show message that json is malformed
            }
        };